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

View File

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