summaryrefslogtreecommitdiff
path: root/d02
diff options
context:
space:
mode:
authorPrefetch2023-02-25 11:41:27 +0100
committerPrefetch2023-02-25 11:41:27 +0100
commit3b877bf4cc667eb8bcc787d145203600a4dba2d2 (patch)
treec1d247def29fcb58ae28e4ae4e4d73d1b4e1b27f /d02
Initial commit
Diffstat (limited to 'd02')
-rw-r--r--d02/Cargo.toml6
-rw-r--r--d02/input.txt5
-rw-r--r--d02/src/main.rs86
3 files changed, 97 insertions, 0 deletions
diff --git a/d02/Cargo.toml b/d02/Cargo.toml
new file mode 100644
index 0000000..47add02
--- /dev/null
+++ b/d02/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "d02"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/d02/input.txt b/d02/input.txt
new file mode 100644
index 0000000..ba5a73d
--- /dev/null
+++ b/d02/input.txt
@@ -0,0 +1,5 @@
+RUDULRLLUULRURDDRRUDURULLLDRLRLUDDLUDUDDUDRRDUDULDUUULLRULLRLDDLDLDDRLRRRRUDLLDDUULDRLLUDDRRUURLULRRRDLLURRUUDURUDDURLUDDDLUDDUUDUURUDLRDRDRLRDRLDRUDRUUDLRDDRRURDDLRDDRRURDUDDLULLUDRURURRRLRRUDUULULULRRLDLUDUURRLLRUDLLDRDDLRRRULRUDLULDDLLLULDLRUDLLLLRDDLRDRLDRLLRDRRDLRDULULRLLLDRUDRRRUULRUULDRURLUDRURRDLLDLRDLDDDDRRLUDLRRLUUUURDRDDLRRURURRDUULLRLURLURUDDDRDURDUUDRLRLRRLDDLDLDLDDDUDDULURLDDLLRLRRDULUDDLULRLUDDLDLRULUUUDRLDRUDURLUDDRLLRUULDLRRRRDLLLLURULLRDRRUDLUULRRDLLRLRLUDLDDULLDLLRDLDLL
+LLUUUUUUDUDRLRDRDLDURRRLLRRLRURLLUURRLLUDUDLULUURUUURDLUDLDDLULLRDLRUULDLRDUDURLLDDUDUDULLUDDUULLLUULRRRLULRURRDLRUDUDDURRRDRUURDURLLULLRULLDRUULLURLDRDUUDDDDDDRRLDRLRRRLULDDUURRLLLLDRURLURDRDRDURUDUURRDUDUDRLLUUDDRLUDDDRDLDLRLDRURRDLLRULDRLLURURRLUULLRLRRURDDRDRUUURUURUUUDLLRRLUDRLDLRLURLDLUDDUDDDLDUDRRLDLRURULRLLRDUULURRRULDLLLRLDDDUURRRRDULLRURRLULULDLRRUDUDDLRUURDLDUDDUDRRDLRRRDUDUUUDLLDDDDLURLURRRUUULLLULRRLLLLLLULDUUDLRUDRRDLRDUUDUDLLRLDLLRUURDUUURUUUDDLLUUDLULDURLULULUUUDRUDULLURRULRULLRDLDDU
+RLUUURULLDLRLDUDRDURRDUURLLUDDDUULRRRLRLURDDRUULUDULDUUDDDDUDDDDRUDDLDUUDRUDLRRRLLRDDLLLRLLRUULRUULDDRURRLURRLRLULDDRRRDDURDDRDRDULRUDRUUDULRLLULDLRLLDRULRDDRRDDUDLRLLUDRDRRRLUDULRDLRDDURRUUDDRRUDURRUUUDDRRDUDURLUUDUDUURDDDLURLULLUULULURUDUUDRUDULLUUULURDLDUULLDDLLDULRLRLRDUUURUUDLRLDURUDRLDULLUDLDLLRDUURRDUDURLUUUDLLRRULRLULRLDLLURDURRULRLLRRDUDLLRDRRRRDLUUDRUUUDDLRLUDDDDDDRURRRUUURRDLLRURLDDLLDLRRLLLDRRULRRUDLDRDDRRLULURLLUURURURRRRUUUUURUDURLRLLLULULDLLDLRDRRULUDUDRDRRDRDRRDUDLLLRUDRUDDDULRULRRRDRLRUUUURUDURDUUULLULRUDDULDUUDLDURRD
+ULRULDDLDLULLLRRRLRUDDDDDLLDDUDLRRDULUUDRDLRRURDRRLUULRURUDRRULDLLLUDRUUDULULUDDRUDDDRDURRRDRDUUURLRDULUDRDRLDRUDDLLLDRRULUDLUDLDLLRRUDUULULDLDLLUURDLDDLLUUDURLURLLLDRDLDRRLRULUURRDRULRUUURULRRUDDDDLLDLDDLLRRLRRRRDUUDUDLDRDRRURDLRURULDLRDLLLLRUDRLLRDLRLRDURDRUDURRRLRDRDLLRLUDDDDRLRLLDUURRURLUURUULUDLUURDRRUDDLUDUDDDURRDRUDRLRULDULUUUUUUDDUDRUDUUURUDRRDLUDLUUDUULUDURDLDDDLLURRURUUDUDDRRDRLLULULDRLRURRDDDRDUUURDDDRULUDRDDLDURRLDDDLRRRLDDRDURULDLUDLLLURLURRLRRULDLLDDUDRRULDRRRRLURRUULRRRUDLURDLLDLLDULUUDRRLDLLLDRLRUDLUULDLDRUDUDURDRUDRDDDLRLULLUR
+LRLUUURRLRRRRRUURRLLULRLULLDLUDLUDRDDRLDLRLULLURDURLURDLLRLDUUDDURRRRLDLLRULLRLDLLUUDRLDDLLDRULDRLLRURDLRURRUDLULLRURDLURRURUDULLDRLLUUULUDRURRUUDUDULUUULRLDDULDRDLUDDUDDDLRURULLDLLLRLLUURDLRUDLLLLDLLRLRUUUDDRUUUUDLDLRDDURLDURUULLLUUDLLLLDULRRRLLDLDRRDRLUDRUDURLLUDLRLLUDUDRDDDRDLRDLRULUULDRLUDLRLDUURLRRLUDDDUUDDDUDRLDLDUDLURUULLDDDURUUULRLUDLDURUUDRDRURUDDUURDUUUDLLDLDLDURUURLLLLRURUURURULRULLRUDLRRUUUUUDRRLLRDDUURDRDRDDDUDRLURDRRRUDLLLDURDLUUDLLUDDULUUDLDUUULLDRDLRURUURRDURRDLURRRRLLUUULRDULDDLDUURRDLDLLULRRLLUDLDUDLUUL
diff --git a/d02/src/main.rs b/d02/src/main.rs
new file mode 100644
index 0000000..33c7ec0
--- /dev/null
+++ b/d02/src/main.rs
@@ -0,0 +1,86 @@
+use std::fs;
+
+fn solve_part1(lines: &Vec<&str>) -> String {
+ let mut result = String::new();
+
+ let mut num = 5;
+ for line in lines {
+ for c in line.chars() {
+ num = match c {
+ 'U' => if num - 3 > 0 { num - 3 } else { num },
+ 'D' => if num + 3 < 10 { num + 3 } else { num },
+ 'L' => if num % 3 != 1 { num - 1 } else { num },
+ 'R' => if num % 3 != 0 { num + 1 } else { num },
+ _ => num, // shouldn't happen
+ }
+ }
+ result.push_str(&num.to_string());
+ }
+
+ result
+}
+
+fn solve_part2(lines: &Vec<&str>) -> String {
+ let mut result = String::new();
+
+ // Part 2's keypad is a bit more exotic
+ let keypad = [
+ [0, 0, 1, 0, 0],
+ [0, 2, 3, 4, 0],
+ [5, 6, 7, 8, 9],
+ [0, 10, 11, 12, 0],
+ [0, 0, 13, 0, 0],
+ ];
+ let mut pos = (2, 0); // (row, col) index
+
+ for line in lines {
+ for c in line.chars() {
+ let mut new = pos;
+ match c {
+ 'U' => new.0 = if pos.0 > 0 { pos.0 - 1 } else { pos.0 },
+ 'D' => new.0 = if pos.0 < 4 { pos.0 + 1 } else { pos.0 },
+ 'L' => new.1 = if pos.1 > 0 { pos.1 - 1 } else { pos.1 },
+ 'R' => new.1 = if pos.1 < 4 { pos.1 + 1 } else { pos.1 },
+ _ => {}, // shouldn't happen
+ };
+ // Ignore this `c' if it would take us off the diamond's edge
+ if keypad[new.0][new.1] != 0 {
+ pos = new;
+ }
+ }
+
+ // Print as hexadecimal to follow puzzle description
+ result.push_str(&format!("{:X}", keypad[pos.0][pos.1]));
+ }
+
+ result
+}
+
+fn main() {
+ // Read directions from input text file
+ let input = fs::read_to_string("input.txt").unwrap();
+ let lines = input.lines().collect();
+
+ // Part 1 gives 74921 for me
+ println!("Part 1 solution: {}", solve_part1(&lines));
+
+ // Part 2 gives A6B35 for me
+ println!("Part 2 solution: {}", solve_part2(&lines));
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn part1_example1() {
+ let lines = vec!["ULL", "RRDDD", "LURDL", "UUUUD"];
+ assert_eq!(solve_part1(&lines), "1985");
+ }
+
+ #[test]
+ fn part2_example1() {
+ let lines = vec!["ULL", "RRDDD", "LURDL", "UUUUD"];
+ assert_eq!(solve_part2(&lines), "5DB3");
+ }
+}