summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-12-03 23:37:44 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:13:50 -0500
commit340d605cbccf2364c9489ba7a4d48a66b74ad183 (patch)
tree1b702e7bbb047e94b8f27ed4e2463962b93935d8
parentAdd test to day 2 (diff)
downloadaoc-340d605cbccf2364c9489ba7a4d48a66b74ad183.tar.gz
aoc-340d605cbccf2364c9489ba7a4d48a66b74ad183.zip
Day 2 part 2
-rw-r--r--src/bin/day02.rs83
1 files changed, 79 insertions, 4 deletions
diff --git a/src/bin/day02.rs b/src/bin/day02.rs
index 21d101b..f7cc624 100644
--- a/src/bin/day02.rs
+++ b/src/bin/day02.rs
@@ -1,5 +1,12 @@
 use std::io::{self, Read};
 
+trait Pad: Copy + Default {
+    fn up(self) -> Self;
+    fn down(self) -> Self;
+    fn left(self) -> Self;
+    fn right(self) -> Self;
+}
+
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
 enum Keypad {
     K1, K2, K3,
@@ -7,7 +14,11 @@ enum Keypad {
     K7, K8, K9,
 }
 
-impl Keypad {
+impl Default for Keypad {
+    fn default() -> Self { Keypad::K5 }
+}
+
+impl Pad for Keypad {
     fn up(self) -> Self {
         use Keypad::*;
         match self {
@@ -47,9 +58,66 @@ impl Keypad {
     }
 }
 
-fn solve(input: &str) -> Vec<Keypad> {
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum Wackypad {
+            K1,
+        K2, K3, K4,
+    K5, K6, K7, K8, K9,
+        KA, KB, KC,
+            KD,
+}
+
+impl Default for Wackypad {
+    fn default() -> Self { Wackypad::K5 }
+}
+
+impl Pad for Wackypad {
+    fn up(self) -> Self {
+        use Wackypad::*;
+        match self {
+                      K3 => K1,
+            K6 => K2, K7 => K3, K8 => K4,
+            KA => K6, KB => K7, KC => K8,
+                      KD => KB,
+            _ => self,
+        }
+    }
+
+    fn down(self) -> Self {
+        use Wackypad::*;
+        match self {
+                      K1 => K3,
+            K2 => K6, K3 => K7, K4 => K8,
+            K6 => KA, K7 => KB, K8 => KC,
+                      KB => KD,
+            _ => self,
+        }
+    }
+
+    fn left(self) -> Self {
+        use Wackypad::*;
+        match self {
+                      K3 => K2, K4 => K3,
+            K6 => K5, K7 => K6, K8 => K7, K9 => K8,
+                      KB => KA, KC => KB,
+            _ => self,
+        }
+    }
+
+    fn right(self) -> Self {
+        use Wackypad::*;
+        match self {
+                      K2 => K3, K3 => K4,
+            K5 => K6, K6 => K7, K7 => K8, K8 => K9,
+                      KA => KB, KB => KC,
+            _ => self,
+        }
+    }
+}
+
+fn solve<P: Pad>(input: &str) -> Vec<P> {
     let mut code = Vec::new();
-    let mut key = Keypad::K5;
+    let mut key = P::default();
 
     for line in input.lines() {
         for direction in line.chars() {
@@ -71,7 +139,8 @@ fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).unwrap();
 
-    println!("Part 1: {:?}", solve(&input));
+    println!("Part 1: {:?}", solve::<Keypad>(&input));
+    println!("Part 2: {:?}", solve::<Wackypad>(&input));
 }
 
 #[test]
@@ -79,3 +148,9 @@ fn part1() {
     use Keypad::*;
     assert_eq!(vec![K1, K9, K8, K5], solve("ULL\nRRDDD\nLURDL\nUUUUD"));
 }
+
+#[test]
+fn part2() {
+    use Wackypad::*;
+    assert_eq!(vec![K5, KD, KB, K3], solve("ULL\nRRDDD\nLURDL\nUUUUD"));
+}