diff --git a/2022/Cargo.lock b/2022/Cargo.lock index 8973e1c..4a47273 100644 --- a/2022/Cargo.lock +++ b/2022/Cargo.lock @@ -44,6 +44,10 @@ version = "0.1.0" name = "aoc-2022-09" version = "0.1.0" +[[package]] +name = "aoc-2022-10" +version = "0.1.0" + [[package]] name = "either" version = "1.8.0" diff --git a/2022/Cargo.toml b/2022/Cargo.toml index 25cf03c..0b4585e 100644 --- a/2022/Cargo.toml +++ b/2022/Cargo.toml @@ -9,6 +9,7 @@ members = [ "./day-07", "./day-08", "./day-09", + "./day-10", ] diff --git a/2022/README.md b/2022/README.md index 9d582a1..ee0155b 100644 --- a/2022/README.md +++ b/2022/README.md @@ -13,7 +13,7 @@ | 07 | ✓ | ✓ | [Rust] | | 08 | ✓ | ✓ | [Rust] | | 09 | ✓ | ✓ | [Rust] | -| 10 | | | | +| 10 | ✓ | ✓ | [Rust] | | 11 | | | | | 12 | | | | | 13 | | | | diff --git a/2022/day-10/Cargo.toml b/2022/day-10/Cargo.toml new file mode 100644 index 0000000..24fb968 --- /dev/null +++ b/2022/day-10/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "aoc-2022-10" +version = "0.1.0" +edition = "2021" + + +[lib] +test = false +doctest = false + + +[dependencies] diff --git a/2022/day-10/Justfile b/2022/day-10/Justfile new file mode 100644 index 0000000..7d08527 --- /dev/null +++ b/2022/day-10/Justfile @@ -0,0 +1,5 @@ +@part PART INPUT_FILE="inputs/puzzle.txt": + cargo --quiet run --bin part_{{PART}} -- {{INPUT_FILE}} + +clean: + cargo clean diff --git a/2022/day-10/inputs/puzzle.txt b/2022/day-10/inputs/puzzle.txt new file mode 100644 index 0000000..3b7250a --- /dev/null +++ b/2022/day-10/inputs/puzzle.txt @@ -0,0 +1,139 @@ +noop +noop +noop +addx 5 +noop +addx 1 +addx 2 +addx 5 +addx 2 +addx 1 +noop +addx 5 +noop +addx -1 +noop +addx 5 +noop +noop +addx 5 +addx 1 +noop +noop +addx 3 +addx 2 +noop +addx -38 +noop +addx 3 +addx 2 +addx -5 +addx 12 +addx 2 +addx 27 +addx -40 +addx 19 +addx 2 +addx 19 +addx -18 +addx 2 +addx 5 +addx 2 +addx -23 +addx 22 +addx 4 +addx -34 +addx -1 +addx 5 +noop +addx 2 +addx 1 +addx 20 +addx -17 +noop +addx 25 +addx -17 +addx -2 +noop +addx 3 +addx 19 +addx -12 +addx 3 +addx -2 +addx 3 +addx 1 +noop +addx 5 +noop +noop +addx -37 +addx 3 +addx 4 +noop +addx 24 +addx -6 +addx -15 +addx 2 +noop +addx 6 +addx -2 +addx 6 +addx -12 +addx -2 +addx 19 +noop +noop +noop +addx 3 +noop +addx 7 +addx -2 +addx -24 +addx -11 +addx 4 +addx 3 +addx -2 +noop +addx 7 +addx -2 +addx 2 +noop +addx 3 +addx 7 +noop +addx -2 +addx 5 +addx 2 +addx 5 +noop +noop +noop +addx 3 +addx -35 +addx 35 +addx -21 +addx -14 +noop +addx 5 +addx 2 +addx 33 +addx -7 +addx -23 +addx 5 +addx 2 +addx 1 +noop +noop +addx 5 +addx -1 +noop +addx 3 +addx -23 +addx 30 +addx 1 +noop +addx 4 +addx -17 +addx 11 +noop +noop \ No newline at end of file diff --git a/2022/day-10/inputs/test.txt b/2022/day-10/inputs/test.txt new file mode 100644 index 0000000..94cd0a8 --- /dev/null +++ b/2022/day-10/inputs/test.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop \ No newline at end of file diff --git a/2022/day-10/src/bin/part_one.rs b/2022/day-10/src/bin/part_one.rs new file mode 100644 index 0000000..d9e9c1f --- /dev/null +++ b/2022/day-10/src/bin/part_one.rs @@ -0,0 +1,58 @@ +use std::{env, io}; + +use aoc_2022_10::{parse_input, Instruction}; + +fn main() -> io::Result<()> { + let infile_path = env::args().nth(1).expect("input file"); + + let result = solve(&infile_path)?; + + println!("{result}"); + + Ok(()) +} + +fn solve(path: &str) -> io::Result { + let instructions = parse_input(path)?; + + let mut cycles_total = 0; + let mut reg_x = 1; + let mut sig_strength_total = 0; + + for instruction in instructions { + let (cycles, add_x) = match instruction { + Instruction::Noop => (1, 0), + Instruction::Addx(val) => (2, val), + }; + + for _ in 0..cycles { + cycles_total += 1; + + if cycles_total == 20 || (cycles_total - 20) % 40 == 0 { + let sig_strength = cycles_total * reg_x; + sig_strength_total += sig_strength; + } + } + + reg_x += add_x; + } + + Ok(sig_strength_total) +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn sample() { + let result = solve("inputs/test.txt").unwrap(); + assert_eq!(13140, result) + } + + #[test] + fn puzzle() { + let result = solve("inputs/puzzle.txt").unwrap(); + assert_eq!(13760, result); + } +} diff --git a/2022/day-10/src/bin/part_two.rs b/2022/day-10/src/bin/part_two.rs new file mode 100644 index 0000000..64e1536 --- /dev/null +++ b/2022/day-10/src/bin/part_two.rs @@ -0,0 +1,92 @@ +use std::{env, io}; + +use aoc_2022_10::{parse_input, Instruction}; + +fn main() -> io::Result<()> { + let infile_path = env::args().nth(1).expect("input file"); + + let result = solve(&infile_path)?; + + println!("{result}"); + + Ok(()) +} + +const PIXEL_LIT: char = '#'; +const PIXEL_UNLIT: char = '.'; +const DRAW_CYCLE: i64 = 40; + +fn solve(path: &str) -> io::Result { + let instructions = parse_input(path)?; + + let mut crt = Vec::<[char; 40]>::new(); + let mut crt_row = [PIXEL_UNLIT; 40]; + + let mut cycles_total = 0; + let mut reg_x = 1_i64; + + for instruction in instructions { + let (cycles, add_x) = match instruction { + Instruction::Noop => (1, 0), + Instruction::Addx(val) => (2, val), + }; + + for _ in 0..cycles { + let cur_row_pos = cycles_total % DRAW_CYCLE; + + if (reg_x - 1..=reg_x + 1).contains(&cur_row_pos) { + crt_row[cur_row_pos as usize] = PIXEL_LIT; + } + + cycles_total += 1; + + if cycles_total % DRAW_CYCLE == 0 { + crt.push(crt_row); + crt_row = [PIXEL_UNLIT; 40]; + } + } + + reg_x += add_x; + } + + let lines = crt + .iter() + .map(|elem| elem.iter().collect::()) + .collect::>() + .join("\n"); + + Ok(lines) +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn sample() { + const OUTPUT: &str = "\ +##..##..##..##..##..##..##..##..##..##.. +###...###...###...###...###...###...###. +####....####....####....####....####.... +#####.....#####.....#####.....#####..... +######......######......######......#### +#######.......#######.......#######....."; + + let result = solve("inputs/test.txt").unwrap(); + assert_eq!(OUTPUT, &result) + } + + #[test] + fn puzzle() { + const OUTPUT: &str = "\ +###..####.#..#.####..##..###..####.####. +#..#.#....#.#.....#.#..#.#..#.#....#.... +#..#.###..##.....#..#....#..#.###..###.. +###..#....#.#...#...#....###..#....#.... +#.#..#....#.#..#....#..#.#....#....#.... +#..#.#....#..#.####..##..#....####.#...."; + + let result = solve("inputs/puzzle.txt").unwrap(); + assert_eq!(OUTPUT, result); + } +} diff --git a/2022/day-10/src/lib.rs b/2022/day-10/src/lib.rs new file mode 100644 index 0000000..2099a7e --- /dev/null +++ b/2022/day-10/src/lib.rs @@ -0,0 +1,33 @@ +use std::{ + fs::File, + io::{self, BufRead, BufReader}, + path::Path, +}; + +pub enum Instruction { + Noop, + Addx(i64), +} + +pub fn parse_input

(path: P) -> io::Result> +where + P: AsRef, +{ + let file = File::open(path)?; + let mut instructions = vec![]; + + for line in BufReader::new(file).lines() { + let line = line?; + let parts = line.split_whitespace().collect::>(); + + let instr = match parts[..] { + ["addx", val] => Instruction::Addx(val.parse().unwrap()), + ["noop"] => Instruction::Noop, + [..] => panic!("Invalid instruction"), + }; + + instructions.push(instr); + } + + Ok(instructions) +} diff --git a/README.md b/README.md index 4055d86..bcbb952 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,4 @@ - [2019](2019/README.md) (0% completed) - [2020](2020/README.md) (20% completed) - [2021](2021/README.md) (68% completed) -- [2022](2022/README.md) (36% completed) +- [2022](2022/README.md) (40% completed)