From e04663040489c2286e054ac7b86aed63a468ea54 Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 10 Dec 2022 15:42:24 +0800 Subject: [PATCH] day 10 Simple challenge for today. Misread the instructions a few times but implementing it wasn't too bad. --- day_10/src/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/day_10/src/main.rs b/day_10/src/main.rs index e7a11a9..ed474d3 100644 --- a/day_10/src/main.rs +++ b/day_10/src/main.rs @@ -1,3 +1,74 @@ +use std::{collections::HashSet, fs::read_to_string}; + +const PATH: &str = "src/input"; + fn main() { - println!("Hello, world!"); + let binding = read_to_string(PATH).expect("Error reading file"); + assert!(binding.is_ascii()); + let data = binding.trim().split('\n').into_iter(); + + // SCREEN OUTPUT + let mut vga = HashSet::<(i32, i32)>::new(); + + // REGISTERS + let mut rx = 1; + let mut ra = 0; // SIGNAL STRENGTH SUM + let mut rclk = 0; + + macro_rules! update_clk { + () => { + // OUTPUT TO SCREEN + // CHECK ALL 3 POSITIONS OF SPRITE. + for dx in -1..=1 { + if rclk % 40 == rx + dx { + vga.insert((rx + dx, rclk / 40)); + } + } + + // INCREMENT CLOCK + rclk += 1; + + // UPDATE VOLTAGE COUNTER REGISTER + if (rclk - 20) % 40 == 0 { + ra += rclk * rx; + } + }; + } + + for instruction in data { + let mut tokens = instruction.split(' '); + // MATCH MNENOMIC AND UPDATE CLOCK + let mnenomic = tokens.next().unwrap(); + match mnenomic { + "addx" => { + let operand = tokens.next().unwrap().parse::().unwrap(); + + // TWO CYCLES + update_clk!(); + update_clk!(); + rx += operand; + } + "noop" | _ => { + // ONE CYCLE + update_clk!(); + } + } + } + + println!("PART 1 {}", ra); + println!("PART 2"); + print_screen(vga); +} + +fn print_screen(vga: HashSet<(i32, i32)>) { + for j in 0..6 { + for i in 0..40 { + if vga.contains(&(i, j)) { + print!("#"); + } else { + print!("."); + } + } + println!(); + } }