From a87505aa6dc865661e19c61c6fc8aa11bd02be04 Mon Sep 17 00:00:00 2001 From: JonOfUs Date: Sat, 10 Dec 2022 16:06:36 +0100 Subject: [PATCH] Day 10 --- res/10/input.txt | 143 +++++++++++++++++++++++++++++++++++++++++++++++ src/days/d10.rs | 79 ++++++++++++++++++++++++++ src/main.rs | 4 +- 3 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 res/10/input.txt create mode 100644 src/days/d10.rs diff --git a/res/10/input.txt b/res/10/input.txt new file mode 100644 index 0000000..1f07fb5 --- /dev/null +++ b/res/10/input.txt @@ -0,0 +1,143 @@ +addx 2 +addx 3 +noop +noop +addx 1 +addx 5 +addx -1 +addx 5 +addx 1 +noop +noop +addx 4 +noop +noop +addx 5 +addx -5 +addx 6 +addx 3 +addx 1 +addx 5 +addx 1 +noop +addx -38 +addx 41 +addx -22 +addx -14 +addx 7 +noop +noop +addx 3 +addx -2 +addx 2 +noop +addx 17 +addx -12 +addx 5 +addx 2 +addx -16 +addx 17 +addx 2 +addx 5 +addx 2 +addx -30 +noop +addx -6 +addx 1 +noop +addx 5 +noop +noop +noop +addx 5 +addx -12 +addx 17 +noop +noop +noop +noop +addx 5 +addx 10 +addx -9 +addx 2 +addx 5 +addx 2 +addx -5 +addx 6 +addx 4 +noop +noop +addx -37 +noop +noop +addx 17 +addx -12 +addx 30 +addx -23 +addx 2 +noop +addx 3 +addx -17 +addx 22 +noop +noop +noop +addx 5 +noop +addx -10 +addx 11 +addx 4 +noop +addx 5 +addx -2 +noop +addx -6 +addx -29 +addx 37 +addx -30 +addx 27 +addx -2 +addx -22 +noop +addx 3 +addx 2 +noop +addx 7 +addx -2 +addx 2 +addx 5 +addx -5 +addx 6 +addx 2 +addx 2 +addx 5 +addx -25 +noop +addx -10 +noop +addx 1 +noop +addx 2 +noop +noop +noop +noop +addx 7 +addx 1 +addx 4 +addx 1 +noop +addx 2 +noop +addx 3 +addx 5 +addx -1 +noop +addx 3 +addx 5 +addx 2 +addx 1 +noop +noop +noop +noop diff --git a/src/days/d10.rs b/src/days/d10.rs new file mode 100644 index 0000000..3db3ae6 --- /dev/null +++ b/src/days/d10.rs @@ -0,0 +1,79 @@ +use std::fs; + +pub fn solve() { + let path = "res/10/input.txt"; + + let contents = fs::read_to_string(path).expect("I/O error, wrong path?"); + + let lines: Vec<&str> = contents + .lines() + .filter(|line| *line != "") + .collect(); + + let mut reg_x = 1; + let mut cycle = 0; + let mut signal_strengths = Vec::::new(); + + let mut crt = Vec::::new(); + + // parse commands line by line + lines.iter().for_each(|line| { + let parts: Vec<&str> = line.split_whitespace().collect(); + match parts[0] { + "noop" => { + // increase cycle 1 time + cycle += 1; + if rel_cycle(cycle) { + signal_strengths.push(cycle*reg_x); + } + crt.push(sprite_draw(cycle, reg_x)); + }, + "addx" => { + // increase cycle 2 times + (0..2).for_each(|_| { + cycle += 1; + if rel_cycle(cycle) { + signal_strengths.push(cycle*reg_x); + } + crt.push(sprite_draw(cycle, reg_x)); + }); + // change regX according to parameter + reg_x += parts[1].parse::().unwrap(); + }, + _ => {} + } + }); + + let res: i32 = signal_strengths.iter().sum(); + + println!("Result 1: {}", res); + + println!("Result 2: "); + let mut print_buffer = "".to_string(); + // print the crt line by line + crt.iter().enumerate().for_each(|(i, p)| { + if *p { print_buffer += "#" } + else { print_buffer += "." } + + // print buffer if a line is completed + if (i+1) % 40 == 0 { + println!("{}", print_buffer); + print_buffer = "".to_string(); + } + }) + +} + +// returns whether the passed cycle is a relevant one +fn rel_cycle(cycle: i32) -> bool { + if (cycle-20) % 40 == 0 && cycle >= 20 { + true + } else { + false + } +} + +// returns whether the sprite is currently in a position to draw a pixel +fn sprite_draw(cycle: i32, reg_x: i32) -> bool { + return ((cycle-1)%40).abs_diff(reg_x) <= 1 +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a66d90c..ae934d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ pub mod days; fn main() { - days::d09::solve() + days::d10::solve() } fn _all_days() { @@ -23,4 +23,6 @@ fn _all_days() { days::d08::solve(); println!("\nDay 9"); days::d09::solve(); + println!("\nDay 10"); + days::d10::solve(); }