Browse Source

Add day two solution

Olyol95 5 years ago
parent
commit
c2e64bd097
4 changed files with 72 additions and 0 deletions
  1. 5 0
      input/day_2
  2. 61 0
      src/day2.rs
  3. 2 0
      src/main.rs
  4. 4 0
      test/day_2

+ 5 - 0
input/day_2

@@ -0,0 +1,5 @@
+RDLRUUULRRDLRLLRLDDUDLULULDDULUDRRUURLRLLUULDURRULLRULDRRDLLULLRLLDRLDDRRRRLLRLURRRDRDULRDUULDDDULURUDDRRRUULUDRLLUUURLUDRUUUDRDUULLRLLUDDRURRDDDRDLUUURLRLLUDRURDUDUULDDLLRDURULLLURLDURLUUULDULDDULULLLRRUDLRUURDRDLLURLUDULDUUUURRLDLUDRULUDLDLLDRLDDDRRLLDUDLLRRDDDRLUDURLLLDRUDDLDDRRLUDRRDUDLRRLULDULURULDULUULDRLLDRUUDDRLLUDRULLRRRLRDLRLUDLRULDRDLRDRLRULUDUURRUUULLDDDDUDDLDDDDRRULRDLRDDULLDLDLLDLLDLLDRRDDDRDDLRRDDDRLLLLURRDLRRLDRURDDURDULDDRUURUDUDDDRDRDDRLRRLRULLDRLDLURLRLRUDURRRDLLLUDRLRDLLDDDLLUDRLDRRUUDUUDULDULLRDLUDUURLDDRUDR
+URULDDLDDUDLLURLUUUUUULUDRRRDDUDURDRUURLLDRURLUULUDRDRLLDRLDULRULUURUURRLRRDRUUUDLLLLRUDDLRDLLDUDLLRRURURRRUDLRLRLLRULRLRLRDLRLLRRUDDRLRUDULDURDLDLLLRDRURURRULLLDLLRRDRLLDUUDLRUUDDURLLLDUUDLRDDURRDRRULLDRLRDULRRLLRLLLLUDDDRDRULRRULLRRUUDULRRRUDLLUUURDUDLLLURRDDUDLDLRLURDDRRRULRRUDRDRDULURULRUDULRRRLRUDLDDDDRUULURDRRDUDLULLRUDDRRRLUDLRURUURDLDURRDUUULUURRDULLURLRUUUUULULLDRURULDURDDRRUDLRLRRLLLLDDUURRULLURURRLLDRRDDUUDLLUURRDRLLLLRLUDUUUDLRLRRLDURDRURLRLRULURLDULLLRRUUUDLLRRDUUULULDLLDLRRRDUDDLRULLULLULLULRU
+DURUUDULRRLULLLDDUDDLRRDURURRRDDRRURDRURDRLULDUDUDUULULDDUURDDULRDUDUDRRURDRDDRLDRDRLDULDDULRULLDULURLUUDUDULRDDRRLURLLRRDLLDLDURULUDUDULDRLLRRRUDRRDDDRRDRUUURLDLURDLRLLDUULLRULLDDDDRULRRLRDLDLRLUURUUULRDUURURLRUDRDDDRRLLRLLDLRULUULULRUDLUDULDLRDDDDDRURDRLRDULRRULRDURDDRRUDRUDLUDLDLRUDLDDRUUULULUULUUUDUULDRRLDUDRRDDLRUULURLRLULRURDDLLULLURLUDLULRLRRDDDDDRLURURURDRURRLLLLURLDDURLLURDULURUUDLURUURDLUUULLLLLRRDUDLLDLUUDURRRURRUUUDRULDDLURUDDRRRDRDULURURLLDULLRDDDRRLLRRRDRLUDURRDLLLLDDDDLUUURDDDDDDLURRURLLLUURRUDLRLRRRURULDRRLULD
+LLUUURRDUUDRRLDLRUDUDRLRDLLRDLLDRUULLURLRRLLUDRLDDDLLLRRRUDULDLLLDRLURDRLRRLURUDULLRULLLURRRRRDDDLULURUDLDUDULRRLUDDURRLULRRRDDUULRURRUULUURDRLLLDDRDDLRRULRDRDRLRURULDULRRDRLDRLLDRDURUUULDLLLRDRRRLRDLLUDRDRLURUURDLRDURRLUDRUDLURDRURLRDLULDURDDURUUDRLULLRLRLDDUDLLUUUURLRLRDRLRRRURLRULDULLLLDLRRRULLUUDLDURUUUDLULULRUDDLLDLDLRLDDUDURDRLLRRLRRDDUDRRRURDLRLUUURDULDLURULUDULRRLDUDLDDDUUDRDUULLDDRLRLLRLLLLURDDRURLDDULLULURLRDUDRDDURLLLUDLLLLLUDRDRDLURRDLUDDLDLLDDLUDRRDDLULRUURDRULDDDLLRLDRULURLRURRDDDRLUUDUDRLRRUDDLRDLDULULDDUDURRRURULRDDDUUDULLULDDRDUDRRDRDRDLRRDURURRRRURULLLRRLR
+URLULLLDRDDULRRLRLUULDRUUULDRRLLDDDLDUULLDRLULRRDRRDDDRRDLRRLLDDRDULLRRLLUDUDDLDRDRLRDLRDRDDUUDRLLRLULLULRDRDDLDDDRLURRLRRDLUDLDDDLRDLDLLULDDRRDRRRULRUUDUULDLRRURRLLDRDRRDDDURUDRURLUDDDDDDLLRLURULURUURDDUDRLDRDRLUUUULURRRRDRDULRDDDDRDLLULRURLLRDULLUUDULULLLLRDRLLRRRLLRUDUUUULDDRULUDDDRRRULUDURRLLDURRDULUDRUDDRUURURURLRDULURDDDLURRDLDDLRUDUUDULLURURDLDURRDRDDDLRRDLLULUDDDRDLDRDRRDRURRDUDRUURLRDDUUDLURRLDRRDLUDRDLURUDLLRRDUURDUDLUDRRL

+ 61 - 0
src/day2.rs

@@ -0,0 +1,61 @@
+use std::process::exit;
+use std::collections::HashMap;
+
+pub fn main( input: &str ) {
+    find_combination( &input, &keypad_one(), (1, 1) );
+    find_combination( &input, &keypad_two(), (0, 2) );
+}
+
+fn find_combination( input: &str, keypad: &HashMap<(i32,i32),String>, start: (i32, i32) ) {
+    let mut point = (start.0, start.1);
+    for line in input.trim().split( "\n" ) {
+        for instruction in line.chars() {
+            match instruction {
+                'U' => { if keypad.contains_key( &(point.0, point.1 - 1) ) {
+                            point.1 = point.1 - 1; } },
+                'D' => { if keypad.contains_key( &(point.0, point.1 + 1) ) { 
+                            point.1 = point.1 + 1; } },
+                'L' => { if keypad.contains_key( &(point.0 - 1, point.1) ) { 
+                            point.0 = point.0 - 1; } },
+                'R' => { if keypad.contains_key( &(point.0 + 1, point.1) ) { 
+                            point.0 = point.0 + 1; } },
+                _   => {
+                    println!( "Unknown instruction encountered {:?} in line {:?}", instruction, line );
+                    exit( 1 );
+                },
+            }
+        }
+        print!( "{}", keypad.get( &point ).unwrap() );
+    }
+    println!( "" );
+}
+
+fn keypad_one() -> HashMap<(i32,i32),String> {
+    let mut keypad = HashMap::new();
+    let mut number = 0; 
+    for y in 0..3 {
+        for x in 0..3 {
+            number += 1;
+            keypad.insert( (x, y), number.to_string() );
+        }
+    }
+    keypad
+}
+
+fn keypad_two() -> HashMap<(i32,i32),String> {
+    let mut keypad = HashMap::new();
+    keypad.insert( (2, 0), format!( "1" ) );
+    keypad.insert( (1, 1), format!( "2" ) );
+    keypad.insert( (2, 1), format!( "3" ) );
+    keypad.insert( (3, 1), format!( "4" ) );
+    keypad.insert( (0, 2), format!( "5" ) );
+    keypad.insert( (1, 2), format!( "6" ) );
+    keypad.insert( (2, 2), format!( "7" ) );
+    keypad.insert( (3, 2), format!( "8" ) );
+    keypad.insert( (4, 2), format!( "9" ) );
+    keypad.insert( (1, 3), format!( "A" ) );
+    keypad.insert( (2, 3), format!( "B" ) );
+    keypad.insert( (3, 3), format!( "C" ) );
+    keypad.insert( (2, 4), format!( "D" ) );
+    keypad
+}

+ 2 - 0
src/main.rs

@@ -9,6 +9,7 @@ use std::path::Path;
 use std::process::exit;
 
 mod day1;
+mod day2;
 
 fn main() {
     let args: Vec<String> = env::args().collect();
@@ -48,6 +49,7 @@ fn parse_opt( args: &Vec<String> ) {
         Some( x ) => {
             match x.as_ref() {
                 "1"  => day1::main( &input ),
+                "2"  => day2::main( &input ),
                 _    => { 
                     println!( "Unknown day number {}", x );
                     exit( 1 );

+ 4 - 0
test/day_2

@@ -0,0 +1,4 @@
+ULL
+RRDDD
+LURDL
+UUUUD