fix display bug in part2

This commit is contained in:
Joe Ardent 2022-12-11 10:37:37 -08:00
parent 100be8c55f
commit b3476976da

View file

@ -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);
}
}