done with part2

This commit is contained in:
Joe Ardent 2022-12-10 16:30:42 -08:00
parent af99955fc2
commit 100be8c55f

View file

@ -71,6 +71,12 @@ impl Vm {
} }
} }
fn cycle2beam(cycle: usize) -> (usize, usize) {
let col = (cycle - 1) % 40;
let row = (cycle - 1) / 40;
(row, col)
}
#[aoc_generator(day10)] #[aoc_generator(day10)]
fn parse_input(input: &str) -> Vm { fn parse_input(input: &str) -> Vm {
let mut data = Vec::new(); let mut data = Vec::new();
@ -102,8 +108,28 @@ fn part1(input: &Vm) -> i32 {
} }
#[aoc_run(day10, part2)] #[aoc_run(day10, part2)]
fn part2(input: &Vm) -> u32 { fn part2(input: &Vm) -> String {
0 let mut vm = input.clone();
let mut crt = vec![vec![' '; 40]; 6];
for _ in 0..240 {
let (cycle, x) = vm.run();
let (row, col) = cycle2beam(cycle);
let diff = (x.abs() - col as i32).abs();
if diff < 2 {
crt[row][col] = '#';
}
}
let mut out = String::with_capacity(248);
out.push('\n');
for row in crt {
for char in row.iter() {
out.push(*char);
}
out.push('\n');
}
out
} }
#[cfg(test)] #[cfg(test)]
@ -267,6 +293,15 @@ noop";
#[test] #[test]
fn part2_test() { fn part2_test() {
let vm = parse_input(INPUT); let vm = parse_input(INPUT);
assert_eq!(part2(&vm), 1); let expected = "
##..##..##..##..##..##..##..##..##..##..
###...###...###...###...###...###...###.
####....####....####....####....####....
#####.....#####.....#####.....#####.....
######......######......######......####
#######.......#######.......#######.....";
let out = part2(&vm);
println!("{}", &out);
assert_eq!(&out.trim(), &expected.trim());
} }
} }