From b3476976da69c6397a7391fd29aaaab21caa90f8 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 11 Dec 2022 10:37:37 -0800 Subject: [PATCH] fix display bug in part2 --- 2022-aoc/src/d10.rs | 165 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 160 insertions(+), 5 deletions(-) diff --git a/2022-aoc/src/d10.rs b/2022-aoc/src/d10.rs index c9c2b8d..c27ee00 100644 --- a/2022-aoc/src/d10.rs +++ b/2022-aoc/src/d10.rs @@ -110,13 +110,13 @@ fn part1(input: &Vm) -> i32 { #[aoc_run(day10, part2)] fn part2(input: &Vm) -> String { let mut vm = input.clone(); - let mut crt = vec![vec![' '; 40]; 6]; + let mut crt = vec![vec!['.'; 40]; 6]; for _ in 0..240 { let (cycle, x) = vm.run(); + let x = (x - 1)..=(x + 1); let (row, col) = cycle2beam(cycle); - let diff = (x.abs() - col as i32).abs(); - if diff < 2 { + if x.contains(&(col as i32)) { crt[row][col] = '#'; } } @@ -299,9 +299,164 @@ noop"; ####....####....####....####....####.... #####.....#####.....#####.....#####..... ######......######......######......#### -#######.......#######.......#######....."; +#######.......#######.......#######..... +"; let out = part2(&vm); println!("{}", &out); - assert_eq!(&out.trim(), &expected.trim()); + assert_eq!(&out, &expected); + } + + #[test] + fn p2_alt_test() { + let input = "noop +addx 10 +addx -4 +addx -1 +noop +noop +addx 5 +addx -12 +addx 17 +noop +addx 1 +addx 2 +noop +addx 3 +addx 2 +noop +noop +addx 7 +addx 3 +noop +addx 2 +noop +noop +addx 1 +addx -38 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx -4 +addx 26 +addx -19 +addx 2 +addx 5 +addx -2 +addx 7 +addx -2 +addx 5 +addx 2 +addx 4 +addx -17 +addx -23 +addx 1 +addx 5 +addx 3 +noop +addx 2 +addx 24 +addx 4 +addx -23 +noop +addx 5 +addx -1 +addx 6 +noop +addx -2 +noop +noop +noop +addx 7 +addx 1 +addx 4 +noop +noop +noop +noop +addx -37 +addx 5 +addx 2 +addx 1 +noop +addx 4 +addx -2 +addx -4 +addx 9 +addx 7 +noop +noop +addx 2 +addx 3 +addx -2 +noop +addx -12 +addx 17 +noop +addx 3 +addx 2 +addx -3 +addx -30 +addx 3 +noop +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx 11 +addx -6 +noop +addx 2 +addx -19 +addx 20 +addx -7 +addx 14 +addx 8 +addx -7 +addx 2 +addx -26 +addx -7 +noop +noop +addx 5 +addx -2 +addx 5 +addx 15 +addx -13 +addx 5 +noop +noop +addx 1 +addx 4 +addx 3 +addx -2 +addx 4 +addx 1 +noop +addx 2 +noop +addx 3 +addx 2 +noop +noop +noop +noop +noop"; + let mut vm = parse_input(input); + let out = part2(&vm); + let expected = " +###...##...##..####.#..#.#....#..#.####. +#..#.#..#.#..#.#....#.#..#....#..#.#.... +###..#..#.#....###..##...#....####.###.. +#..#.####.#....#....#.#..#....#..#.#.... +#..#.#..#.#..#.#....#.#..#....#..#.#.... +###..#..#..##..####.#..#.####.#..#.#.... +"; + assert_eq!(out, expected); } }