Day 10 part 2

This commit is contained in:
Daniel Flanagan 2022-12-15 00:29:44 -06:00
parent e81333b41a
commit 786ed49808
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
2 changed files with 24 additions and 27 deletions

View file

@ -1,5 +1,5 @@
use std::env::var as envvar;
use std::fmt::Debug;
use std::fmt::Display;
use std::fs;
use std::path::Path;
@ -20,12 +20,12 @@ pub fn day_input(day: u8) -> String {
}
#[allow(dead_code)]
pub fn show_answers(answer1: &impl Debug, answer2: &impl Debug) {
println!("Part 1: {:?}", answer1);
println!("Part 2: {:?}", answer2);
pub fn show_answers(answer1: &impl Display, answer2: &impl Display) {
println!("Part 1: {}", answer1);
println!("Part 2: {}", answer2);
}
#[allow(dead_code)]
pub fn show_both_answers((a, b): &(impl Debug, impl Debug)) {
pub fn show_both_answers((a, b): &(impl Display, impl Display)) {
show_answers(a, b)
}

View file

@ -53,36 +53,32 @@ fn part2(input: &Input) -> Answer2 {
let w = 40;
let mut crt = String::new();
let mut x = 1;
let mut cycles = 0;
for i in input {
println!("{:?}", i);
let now_x = x.clone();
cycles += match i {
Instruction::Noop => 1,
Instruction::AddX(n) => {
x += n;
2
}
};
let mut pixel = '.';
let mut draw_pixel = |now_x| {
let mut pixel = ' ';
if ((crt.len() % w) as i32).abs_diff(now_x) <= 1 {
pixel = '#';
}
crt.push(pixel);
crt.push('#');
};
for i in input {
println!("{:?}", i);
let now_x = x.clone();
match i {
Instruction::Noop => draw_pixel(now_x),
Instruction::AddX(n) => {
draw_pixel(now_x);
draw_pixel(now_x);
x += n;
}
};
}
crt.chars()
.enumerate()
.flat_map(|(i, c)| {
if i > 0 && i % w == 0 {
Some('\n')
} else {
None
}
.into_iter()
.chain(std::iter::once(c))
if i % w == 0 { Some('\n') } else { None }
.into_iter()
.chain(std::iter::once(c))
})
.collect::<String>()
}
@ -256,12 +252,13 @@ noop";
assert_eq!(part1(&input), 13140);
assert_eq!(
part2(&input),
"##..##..##..##..##..##..##..##..##..##..
"\n##..##..##..##..##..##..##..##..##..##..
###...###...###...###...###...###...###.
####....####....####....####....####....
#####.....#####.....#####.....#####.....
######......######......######......####
#######.......#######.......#######....."
.replace('.', " ")
);
// assert_eq!(both_parts(&input), (0, 0));
}