From dcbed83ea465ab2de3e701bf1d19cd5c3ca5a5e2 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Sat, 10 Dec 2022 15:17:35 +0100 Subject: [PATCH] Update solution for 2022 day 10 Move tested outputs into files Make some micro-adjustments to code --- 2022/day-10/outputs/part_two/puzzle.txt | 6 ++++++ 2022/day-10/outputs/part_two/test.txt | 6 ++++++ 2022/day-10/src/bin/part_one.rs | 6 +++--- 2022/day-10/src/bin/part_two.rs | 28 +++++++------------------ 4 files changed, 22 insertions(+), 24 deletions(-) create mode 100644 2022/day-10/outputs/part_two/puzzle.txt create mode 100644 2022/day-10/outputs/part_two/test.txt diff --git a/2022/day-10/outputs/part_two/puzzle.txt b/2022/day-10/outputs/part_two/puzzle.txt new file mode 100644 index 0000000..cb3bcbd --- /dev/null +++ b/2022/day-10/outputs/part_two/puzzle.txt @@ -0,0 +1,6 @@ +###..####.#..#.####..##..###..####.####. +#..#.#....#.#.....#.#..#.#..#.#....#.... +#..#.###..##.....#..#....#..#.###..###.. +###..#....#.#...#...#....###..#....#.... +#.#..#....#.#..#....#..#.#....#....#.... +#..#.#....#..#.####..##..#....####.#.... \ No newline at end of file diff --git a/2022/day-10/outputs/part_two/test.txt b/2022/day-10/outputs/part_two/test.txt new file mode 100644 index 0000000..291eedf --- /dev/null +++ b/2022/day-10/outputs/part_two/test.txt @@ -0,0 +1,6 @@ +##..##..##..##..##..##..##..##..##..##.. +###...###...###...###...###...###...###. +####....####....####....####....####.... +#####.....#####.....#####.....#####..... +######......######......######......#### +#######.......#######.......#######..... \ 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 index d9e9c1f..7992131 100644 --- a/2022/day-10/src/bin/part_one.rs +++ b/2022/day-10/src/bin/part_one.rs @@ -15,9 +15,9 @@ fn main() -> io::Result<()> { 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; + let mut cycles_total: i64 = 0; + let mut reg_x: i64 = 1; + let mut sig_strength_total: i64 = 0; for instruction in instructions { let (cycles, add_x) = match instruction { diff --git a/2022/day-10/src/bin/part_two.rs b/2022/day-10/src/bin/part_two.rs index 64e1536..ffbedfa 100644 --- a/2022/day-10/src/bin/part_two.rs +++ b/2022/day-10/src/bin/part_two.rs @@ -22,8 +22,8 @@ fn solve(path: &str) -> io::Result { 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; + let mut cycles_total: i64 = 0; + let mut reg_x: i64 = 1; for instruction in instructions { let (cycles, add_x) = match instruction { @@ -34,7 +34,7 @@ fn solve(path: &str) -> io::Result { for _ in 0..cycles { let cur_row_pos = cycles_total % DRAW_CYCLE; - if (reg_x - 1..=reg_x + 1).contains(&cur_row_pos) { + if ((reg_x - 1)..=(reg_x + 1)).contains(&cur_row_pos) { crt_row[cur_row_pos as usize] = PIXEL_LIT; } @@ -52,8 +52,8 @@ fn solve(path: &str) -> io::Result { let lines = crt .iter() .map(|elem| elem.iter().collect::()) - .collect::>() - .join("\n"); + .reduce(|acc, elem| acc + "\n" + &elem) + .unwrap(); Ok(lines) } @@ -64,28 +64,14 @@ mod test { #[test] fn sample() { - const OUTPUT: &str = "\ -##..##..##..##..##..##..##..##..##..##.. -###...###...###...###...###...###...###. -####....####....####....####....####.... -#####.....#####.....#####.....#####..... -######......######......######......#### -#######.......#######.......#######....."; - + const OUTPUT: &str = include_str!("../../outputs/part_two/test.txt"); let result = solve("inputs/test.txt").unwrap(); assert_eq!(OUTPUT, &result) } #[test] fn puzzle() { - const OUTPUT: &str = "\ -###..####.#..#.####..##..###..####.####. -#..#.#....#.#.....#.#..#.#..#.#....#.... -#..#.###..##.....#..#....#..#.###..###.. -###..#....#.#...#...#....###..#....#.... -#.#..#....#.#..#....#..#.#....#....#.... -#..#.#....#..#.####..##..#....####.#...."; - + const OUTPUT: &str = include_str!("../../outputs/part_two/puzzle.txt"); let result = solve("inputs/puzzle.txt").unwrap(); assert_eq!(OUTPUT, result); }