From 3fdd2db85ff1b86d2318bf5f1668bb7afc032813 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Sat, 10 Dec 2022 20:43:34 +0100 Subject: [AoC2022] Rust Day 10 --- AoC2022/10/solver.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 AoC2022/10/solver.rs (limited to 'AoC2022/10') 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 { + 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::::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::>(); + let step2 = if inst[0] == "addx" { + draw_pixel(&cycle, ®ister); + cycle += 1; + register += inst[1].parse::().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::()); +} -- cgit v1.2.3