diff options
-rw-r--r-- | AoC2022/10/solver.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/AoC2022/10/solver.rs b/AoC2022/10/solver.rs new file mode 100644 index 0000000..0b2ca75 --- /dev/null +++ b/AoC2022/10/solver.rs @@ -0,0 +1,55 @@ +use std::fs; +use std::io::{self, BufRead}; + +fn probe(cycle: &i32, register: &i32) -> Option<i32> { + let probes = vec![20, 60, 100, 140, 180, 220]; + + if probes.contains(cycle) { + Some(cycle * register) + } else { + None + } +} + +fn draw_pixel(cycle: &i32, register: &i32) { + let pixel = (cycle - 1) % 40; + if pixel == 0 { + println!(""); + } + let mark = if (register - 1) <= pixel && pixel <= (register + 1) { + "#" + } else { + "." + }; + print!("{}", mark); +} +fn main() { + let file = fs::File::open("input").unwrap(); + let lines = io::BufReader::new(file).lines(); + let mut register = 1; + let mut cycle = 1; + let mut measures = Vec::<i32>::new(); + + for line in lines { + draw_pixel(&cycle, ®ister); + cycle += 1; + let step1 = probe(&cycle, ®ister); + let inst = line.expect("valid instruction"); + let inst = inst.split(" ").collect::<Vec<&str>>(); + let step2 = if inst[0] == "addx" { + draw_pixel(&cycle, ®ister); + cycle += 1; + register += inst[1].parse::<i32>().unwrap(); + probe(&cycle, ®ister) + } else { + None + }; + + match [step1, step2] { + [Some(x), _] => measures.push(x), + [None, Some(y)] => measures.push(y), + _ => (), + } + } + println!("\n{:?} {}", measures, measures.iter().sum::<i32>()); +} |