diff options
Diffstat (limited to '2022')
-rw-r--r-- | 2022/Cargo.toml | 2 | ||||
-rw-r--r-- | 2022/day5/Cargo.toml | 8 | ||||
-rw-r--r-- | 2022/day5/example.txt | 9 | ||||
-rw-r--r-- | 2022/day5/input.txt | 514 | ||||
-rw-r--r-- | 2022/day5/src/lib.rs | 163 |
5 files changed, 695 insertions, 1 deletions
diff --git a/2022/Cargo.toml b/2022/Cargo.toml index afacff3..20df693 100644 --- a/2022/Cargo.toml +++ b/2022/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["day1", "day2", "day3", "day4"] +members = ["day1", "day2", "day3", "day4", "day5"] diff --git a/2022/day5/Cargo.toml b/2022/day5/Cargo.toml new file mode 100644 index 0000000..f9b62bf --- /dev/null +++ b/2022/day5/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day5" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2022/day5/example.txt b/2022/day5/example.txt new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/2022/day5/example.txt @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 diff --git a/2022/day5/input.txt b/2022/day5/input.txt new file mode 100644 index 0000000..8d7a981 --- /dev/null +++ b/2022/day5/input.txt @@ -0,0 +1,514 @@ + [G] [D] [Q] +[P] [T] [L] [M] [Z] +[Z] [Z] [C] [Z] [G] [W] +[M] [B] [F] [P] [C] [H] [N] +[T] [S] [R] [H] [W] [R] [L] [W] +[R] [T] [Q] [Z] [R] [S] [Z] [F] [P] +[C] [N] [H] [R] [N] [H] [D] [J] [Q] +[N] [D] [M] [G] [Z] [F] [W] [S] [S] + 1 2 3 4 5 6 7 8 9 + +move 7 from 6 to 8 +move 5 from 2 to 6 +move 2 from 4 to 1 +move 1 from 4 to 5 +move 5 from 7 to 6 +move 7 from 6 to 3 +move 5 from 9 to 2 +move 6 from 2 to 3 +move 2 from 7 to 9 +move 20 from 3 to 1 +move 11 from 1 to 6 +move 1 from 9 to 8 +move 3 from 8 to 2 +move 8 from 1 to 5 +move 10 from 8 to 4 +move 7 from 6 to 4 +move 1 from 8 to 3 +move 8 from 1 to 7 +move 16 from 4 to 8 +move 1 from 9 to 8 +move 1 from 5 to 2 +move 4 from 7 to 4 +move 5 from 6 to 7 +move 1 from 6 to 1 +move 8 from 7 to 4 +move 1 from 6 to 9 +move 12 from 4 to 5 +move 3 from 2 to 5 +move 1 from 6 to 2 +move 1 from 3 to 7 +move 1 from 3 to 2 +move 1 from 9 to 3 +move 1 from 7 to 8 +move 1 from 7 to 5 +move 1 from 3 to 2 +move 4 from 5 to 7 +move 5 from 5 to 7 +move 1 from 4 to 3 +move 1 from 3 to 9 +move 3 from 1 to 8 +move 1 from 9 to 1 +move 2 from 2 to 1 +move 2 from 2 to 7 +move 8 from 8 to 1 +move 3 from 5 to 2 +move 8 from 7 to 5 +move 7 from 1 to 3 +move 3 from 1 to 7 +move 1 from 1 to 5 +move 1 from 3 to 7 +move 7 from 5 to 8 +move 2 from 2 to 8 +move 1 from 3 to 2 +move 1 from 2 to 4 +move 1 from 4 to 8 +move 13 from 8 to 1 +move 13 from 5 to 9 +move 2 from 5 to 2 +move 7 from 9 to 3 +move 12 from 8 to 3 +move 4 from 9 to 3 +move 1 from 3 to 4 +move 2 from 2 to 3 +move 1 from 1 to 6 +move 1 from 2 to 3 +move 1 from 5 to 9 +move 7 from 7 to 4 +move 10 from 1 to 8 +move 1 from 1 to 4 +move 1 from 9 to 5 +move 2 from 5 to 1 +move 1 from 6 to 5 +move 3 from 8 to 9 +move 5 from 4 to 3 +move 4 from 4 to 1 +move 7 from 1 to 6 +move 2 from 5 to 7 +move 35 from 3 to 4 +move 4 from 9 to 1 +move 19 from 4 to 8 +move 1 from 7 to 6 +move 1 from 9 to 2 +move 10 from 4 to 5 +move 2 from 4 to 7 +move 3 from 4 to 3 +move 1 from 2 to 8 +move 1 from 1 to 9 +move 3 from 3 to 6 +move 4 from 8 to 6 +move 4 from 5 to 2 +move 2 from 8 to 3 +move 3 from 5 to 9 +move 12 from 6 to 1 +move 8 from 8 to 6 +move 2 from 9 to 1 +move 1 from 4 to 1 +move 1 from 3 to 8 +move 3 from 7 to 8 +move 2 from 9 to 7 +move 1 from 6 to 7 +move 10 from 6 to 8 +move 4 from 2 to 5 +move 1 from 3 to 7 +move 7 from 5 to 7 +move 13 from 8 to 1 +move 29 from 1 to 4 +move 8 from 7 to 8 +move 1 from 1 to 3 +move 3 from 7 to 6 +move 1 from 1 to 9 +move 15 from 4 to 1 +move 1 from 3 to 6 +move 10 from 1 to 6 +move 10 from 6 to 7 +move 1 from 4 to 9 +move 1 from 9 to 1 +move 1 from 9 to 7 +move 6 from 7 to 8 +move 1 from 1 to 6 +move 5 from 6 to 5 +move 21 from 8 to 9 +move 5 from 1 to 9 +move 2 from 9 to 5 +move 3 from 5 to 6 +move 3 from 7 to 9 +move 4 from 4 to 6 +move 6 from 8 to 7 +move 6 from 6 to 3 +move 2 from 7 to 9 +move 1 from 7 to 2 +move 6 from 3 to 2 +move 1 from 6 to 4 +move 4 from 5 to 9 +move 1 from 4 to 5 +move 9 from 4 to 6 +move 7 from 6 to 4 +move 10 from 9 to 2 +move 5 from 7 to 5 +move 10 from 2 to 7 +move 2 from 5 to 4 +move 2 from 5 to 9 +move 4 from 9 to 4 +move 1 from 8 to 6 +move 7 from 7 to 2 +move 1 from 5 to 4 +move 2 from 7 to 1 +move 1 from 5 to 7 +move 3 from 6 to 2 +move 4 from 4 to 5 +move 1 from 2 to 7 +move 10 from 4 to 7 +move 3 from 7 to 3 +move 17 from 9 to 4 +move 1 from 1 to 4 +move 1 from 1 to 5 +move 5 from 2 to 7 +move 1 from 9 to 2 +move 5 from 4 to 8 +move 2 from 9 to 7 +move 4 from 8 to 1 +move 3 from 4 to 8 +move 1 from 2 to 5 +move 1 from 9 to 2 +move 6 from 4 to 8 +move 3 from 7 to 5 +move 1 from 4 to 9 +move 1 from 9 to 1 +move 3 from 1 to 9 +move 4 from 8 to 5 +move 2 from 9 to 8 +move 4 from 2 to 5 +move 8 from 7 to 2 +move 5 from 8 to 5 +move 2 from 7 to 8 +move 1 from 3 to 5 +move 1 from 1 to 2 +move 1 from 1 to 6 +move 2 from 3 to 6 +move 5 from 2 to 8 +move 4 from 7 to 1 +move 7 from 8 to 5 +move 1 from 1 to 5 +move 3 from 8 to 3 +move 1 from 9 to 3 +move 7 from 2 to 3 +move 2 from 2 to 8 +move 2 from 4 to 8 +move 1 from 8 to 5 +move 1 from 1 to 4 +move 2 from 4 to 7 +move 2 from 7 to 1 +move 3 from 2 to 3 +move 3 from 5 to 2 +move 1 from 8 to 3 +move 3 from 3 to 2 +move 5 from 2 to 1 +move 17 from 5 to 8 +move 9 from 8 to 1 +move 11 from 3 to 5 +move 8 from 8 to 5 +move 2 from 8 to 5 +move 16 from 1 to 4 +move 13 from 4 to 7 +move 6 from 5 to 2 +move 2 from 4 to 8 +move 5 from 7 to 9 +move 2 from 1 to 2 +move 7 from 7 to 1 +move 1 from 1 to 4 +move 1 from 9 to 8 +move 7 from 2 to 8 +move 1 from 4 to 7 +move 2 from 9 to 4 +move 1 from 4 to 1 +move 1 from 3 to 5 +move 2 from 9 to 8 +move 11 from 8 to 7 +move 2 from 6 to 5 +move 1 from 6 to 9 +move 1 from 1 to 9 +move 1 from 9 to 1 +move 4 from 1 to 4 +move 2 from 1 to 8 +move 1 from 1 to 2 +move 1 from 9 to 5 +move 2 from 4 to 3 +move 2 from 2 to 7 +move 2 from 3 to 9 +move 1 from 9 to 1 +move 1 from 9 to 1 +move 5 from 5 to 1 +move 19 from 5 to 6 +move 5 from 1 to 4 +move 1 from 2 to 9 +move 1 from 1 to 3 +move 7 from 5 to 8 +move 1 from 3 to 6 +move 8 from 7 to 3 +move 7 from 4 to 8 +move 3 from 8 to 5 +move 1 from 4 to 1 +move 1 from 9 to 4 +move 1 from 4 to 9 +move 1 from 5 to 2 +move 2 from 5 to 6 +move 2 from 8 to 2 +move 7 from 8 to 1 +move 1 from 1 to 7 +move 3 from 6 to 9 +move 2 from 3 to 2 +move 1 from 2 to 1 +move 1 from 8 to 7 +move 2 from 9 to 6 +move 2 from 9 to 5 +move 1 from 5 to 6 +move 1 from 2 to 8 +move 2 from 1 to 7 +move 1 from 4 to 3 +move 3 from 2 to 5 +move 7 from 1 to 3 +move 10 from 3 to 4 +move 3 from 5 to 4 +move 1 from 3 to 8 +move 3 from 3 to 2 +move 1 from 8 to 1 +move 1 from 1 to 3 +move 3 from 8 to 3 +move 5 from 4 to 6 +move 1 from 2 to 3 +move 4 from 6 to 4 +move 1 from 5 to 7 +move 4 from 3 to 4 +move 1 from 2 to 8 +move 12 from 7 to 6 +move 1 from 8 to 2 +move 2 from 2 to 7 +move 1 from 8 to 4 +move 23 from 6 to 3 +move 14 from 3 to 6 +move 15 from 4 to 6 +move 1 from 8 to 6 +move 10 from 3 to 7 +move 2 from 4 to 2 +move 11 from 7 to 8 +move 2 from 2 to 6 +move 44 from 6 to 9 +move 21 from 9 to 3 +move 12 from 3 to 6 +move 1 from 7 to 4 +move 1 from 4 to 7 +move 9 from 3 to 2 +move 2 from 8 to 6 +move 3 from 2 to 4 +move 17 from 9 to 1 +move 3 from 4 to 6 +move 2 from 2 to 9 +move 4 from 9 to 2 +move 10 from 6 to 9 +move 1 from 7 to 6 +move 4 from 9 to 5 +move 4 from 2 to 4 +move 14 from 1 to 5 +move 4 from 4 to 3 +move 3 from 2 to 9 +move 9 from 9 to 7 +move 1 from 2 to 5 +move 9 from 8 to 5 +move 8 from 7 to 2 +move 4 from 3 to 8 +move 5 from 6 to 2 +move 3 from 1 to 6 +move 1 from 7 to 1 +move 4 from 2 to 4 +move 3 from 6 to 4 +move 3 from 8 to 3 +move 13 from 5 to 2 +move 2 from 3 to 5 +move 12 from 5 to 9 +move 1 from 3 to 5 +move 1 from 5 to 9 +move 1 from 8 to 3 +move 4 from 9 to 5 +move 6 from 4 to 5 +move 12 from 9 to 7 +move 1 from 9 to 3 +move 1 from 3 to 2 +move 12 from 5 to 6 +move 12 from 7 to 2 +move 1 from 3 to 7 +move 1 from 4 to 8 +move 33 from 2 to 8 +move 1 from 7 to 5 +move 1 from 1 to 2 +move 4 from 5 to 4 +move 3 from 2 to 5 +move 34 from 8 to 6 +move 1 from 4 to 3 +move 1 from 5 to 7 +move 1 from 7 to 5 +move 3 from 4 to 9 +move 2 from 9 to 7 +move 1 from 9 to 4 +move 1 from 3 to 7 +move 1 from 5 to 8 +move 1 from 5 to 1 +move 1 from 5 to 7 +move 1 from 4 to 8 +move 1 from 1 to 4 +move 1 from 4 to 2 +move 3 from 7 to 5 +move 2 from 8 to 5 +move 1 from 2 to 8 +move 4 from 6 to 2 +move 1 from 8 to 6 +move 1 from 7 to 9 +move 29 from 6 to 7 +move 4 from 2 to 3 +move 2 from 5 to 8 +move 1 from 9 to 5 +move 2 from 8 to 1 +move 23 from 7 to 5 +move 2 from 6 to 1 +move 23 from 5 to 6 +move 1 from 3 to 6 +move 4 from 5 to 9 +move 2 from 1 to 3 +move 5 from 3 to 8 +move 2 from 6 to 5 +move 2 from 1 to 4 +move 1 from 9 to 8 +move 1 from 9 to 1 +move 1 from 4 to 6 +move 2 from 5 to 6 +move 6 from 7 to 8 +move 2 from 9 to 2 +move 18 from 6 to 5 +move 21 from 6 to 4 +move 1 from 1 to 6 +move 2 from 6 to 7 +move 2 from 7 to 9 +move 2 from 2 to 8 +move 7 from 4 to 3 +move 12 from 5 to 3 +move 1 from 9 to 5 +move 1 from 9 to 4 +move 6 from 5 to 2 +move 17 from 3 to 4 +move 3 from 4 to 3 +move 1 from 2 to 4 +move 5 from 2 to 8 +move 1 from 5 to 8 +move 19 from 8 to 7 +move 1 from 3 to 6 +move 1 from 8 to 4 +move 1 from 6 to 1 +move 15 from 4 to 6 +move 1 from 1 to 4 +move 3 from 3 to 5 +move 4 from 6 to 7 +move 1 from 4 to 7 +move 10 from 6 to 7 +move 16 from 4 to 5 +move 24 from 7 to 2 +move 8 from 7 to 8 +move 1 from 4 to 2 +move 6 from 8 to 7 +move 1 from 8 to 7 +move 1 from 6 to 9 +move 14 from 5 to 4 +move 9 from 7 to 8 +move 4 from 5 to 1 +move 2 from 1 to 5 +move 3 from 8 to 6 +move 2 from 6 to 9 +move 2 from 2 to 8 +move 6 from 2 to 7 +move 3 from 4 to 6 +move 1 from 3 to 4 +move 3 from 5 to 7 +move 1 from 6 to 9 +move 5 from 7 to 2 +move 4 from 9 to 1 +move 1 from 7 to 9 +move 9 from 8 to 4 +move 5 from 1 to 2 +move 2 from 6 to 1 +move 6 from 4 to 7 +move 1 from 7 to 3 +move 1 from 3 to 9 +move 1 from 9 to 7 +move 1 from 6 to 7 +move 9 from 4 to 5 +move 7 from 7 to 9 +move 3 from 7 to 5 +move 1 from 9 to 2 +move 6 from 9 to 8 +move 4 from 4 to 5 +move 1 from 4 to 2 +move 1 from 4 to 2 +move 2 from 1 to 2 +move 1 from 9 to 8 +move 10 from 2 to 4 +move 8 from 2 to 7 +move 12 from 2 to 9 +move 6 from 7 to 4 +move 1 from 1 to 2 +move 8 from 9 to 8 +move 7 from 5 to 1 +move 9 from 4 to 3 +move 14 from 8 to 4 +move 1 from 8 to 4 +move 1 from 1 to 5 +move 1 from 5 to 2 +move 3 from 2 to 4 +move 1 from 7 to 1 +move 1 from 7 to 3 +move 2 from 1 to 7 +move 3 from 5 to 7 +move 2 from 7 to 6 +move 1 from 6 to 5 +move 3 from 7 to 1 +move 1 from 6 to 8 +move 1 from 8 to 7 +move 1 from 3 to 6 +move 1 from 7 to 1 +move 4 from 1 to 4 +move 6 from 3 to 2 +move 3 from 1 to 2 +move 3 from 3 to 6 +move 3 from 2 to 6 +move 6 from 6 to 5 +move 1 from 1 to 4 +move 1 from 9 to 6 +move 5 from 2 to 1 +move 3 from 1 to 2 +move 2 from 9 to 8 +move 3 from 1 to 5 +move 1 from 9 to 7 +move 25 from 4 to 1 +move 1 from 1 to 7 +move 2 from 8 to 3 +move 13 from 1 to 9 +move 2 from 3 to 5 +move 8 from 5 to 9 +move 4 from 2 to 1 +move 2 from 6 to 7 +move 10 from 5 to 9 +move 4 from 7 to 2 +move 2 from 2 to 3 +move 9 from 9 to 2 +move 4 from 4 to 5 +move 4 from 5 to 4 +move 5 from 1 to 4 +move 10 from 4 to 5 +move 22 from 9 to 1 +move 2 from 2 to 7 +move 3 from 2 to 1 +move 6 from 2 to 6 +move 1 from 7 to 1 +move 10 from 5 to 7 +move 15 from 1 to 4 +move 13 from 1 to 5 +move 3 from 6 to 8 +move 1 from 8 to 9 diff --git a/2022/day5/src/lib.rs b/2022/day5/src/lib.rs new file mode 100644 index 0000000..c5f7458 --- /dev/null +++ b/2022/day5/src/lib.rs @@ -0,0 +1,163 @@ +use std::collections::HashMap; +use std::collections::VecDeque; + +pub fn part1(input: &str) -> String { + let mut h: Vec<VecDeque<char>> = Vec::with_capacity(10); + h.resize(10, VecDeque::new()); + + let split: Vec<&str> = input.split("\n\n").collect(); + let mut m: HashMap<usize, usize> = HashMap::new(); + let mut i = 0; + let mut j = 1; + while i < 10 { + m.insert(j, i); + j += 4; + i += 1; + } + + assert!(split.len() == 2); + + for line in split[0].lines() { + if line.chars().nth(1).unwrap() == '1' { + continue; + } + + for (i, ch) in line.chars().enumerate() { + match ch { + ' ' | '[' | ']' => { + continue; + } + x => { + let idx = m[&i]; + h[idx].push_front(x); + } + }; + } + } + + for line in split[1].lines() { + let actions: Vec<usize> = line + .split(' ') + .enumerate() + .filter(|&(i, _)| i % 2 == 1) + .map(|(_, e)| e.parse::<usize>().unwrap()) + .collect(); + + for _ in 0..actions[0] { + let x = h[actions[1] - 1].pop_back().unwrap(); + h[actions[2] - 1].push_back(x); + } + } + + let mut s = String::new(); + + for mut e in h { + if e.len() == 0 { + continue; + } + + let x = e.pop_back().unwrap().to_string(); + s += &x; + } + + s +} + +pub fn part2(input: &str) -> String { + let mut h: Vec<VecDeque<char>> = Vec::with_capacity(10); + h.resize(10, VecDeque::new()); + + let split: Vec<&str> = input.split("\n\n").collect(); + let mut m: HashMap<usize, usize> = HashMap::new(); + let mut i = 0; + let mut j = 1; + while i < 10 { + m.insert(j, i); + j += 4; + i += 1; + } + + assert!(split.len() == 2); + + for line in split[0].lines() { + if line.chars().nth(1).unwrap() == '1' { + continue; + } + + for (i, ch) in line.chars().enumerate() { + match ch { + ' ' | '[' | ']' => { + continue; + } + x => { + let idx = m[&i]; + h[idx].push_front(x); + } + }; + } + } + + for line in split[1].lines() { + let actions: Vec<usize> = line + .split(' ') + .enumerate() + .filter(|&(i, _)| i % 2 == 1) + .map(|(_, e)| e.parse::<usize>().unwrap()) + .collect(); + + let mut xs = Vec::<char>::new(); + for _ in 0..actions[0] { + xs.push(h[actions[1] - 1].pop_back().unwrap()); + } + xs.reverse(); + for x in xs { + h[actions[2] - 1].push_back(x); + } + } + + let mut s = String::new(); + + for mut e in h { + if e.len() == 0 { + continue; + } + + let x = e.pop_back().unwrap().to_string(); + s += &x; + } + + s +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_example() { + let input = include_str!("../example.txt"); + + assert_eq!(part1(input), String::from("CMZ")); + } + + #[test] + fn test_input1() { + let input = include_str!("../input.txt"); + + assert_eq!(part1(input), String::from("RTGWZTHLD")); + } + + #[test] + fn test_example2() { + let input = include_str!("../example.txt"); + + assert_eq!(part2(input), String::from("MCD")); + } + + #[test] + fn test_input2() { + let input = include_str!("../input.txt"); + + assert_eq!(part2(input), String::from("STHGRZZFR")); + } +} |