From afa80765cfbc9fd83505a42ab40edb48b7cefc9b Mon Sep 17 00:00:00 2001 From: Bas Dado Date: Sun, 11 Dec 2022 16:17:45 +0100 Subject: [PATCH] [TASK] Solved Day 10 --- input/day10.txt | 139 ++++++++++++++++++++++++++++++++++++++ input/day10_example.txt | 146 ++++++++++++++++++++++++++++++++++++++++ src/day10.rs | 100 +++++++++++++++++++++++++++ src/main.rs | 3 + 4 files changed, 388 insertions(+) create mode 100644 input/day10.txt create mode 100644 input/day10_example.txt create mode 100644 src/day10.rs diff --git a/input/day10.txt b/input/day10.txt new file mode 100644 index 0000000..e013813 --- /dev/null +++ b/input/day10.txt @@ -0,0 +1,139 @@ +addx 1 +noop +noop +noop +addx 5 +addx 5 +noop +noop +addx 9 +addx -5 +addx 1 +addx 4 +noop +noop +noop +addx 6 +addx -1 +noop +addx 5 +addx -2 +addx 7 +noop +addx 3 +addx -2 +addx -38 +noop +noop +addx 32 +addx -22 +noop +addx 2 +addx 3 +noop +addx 2 +addx -2 +addx 7 +addx -2 +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx -5 +addx 10 +noop +addx 3 +noop +addx -38 +addx 1 +addx 27 +noop +addx -20 +noop +addx 2 +addx 27 +noop +addx -22 +noop +noop +noop +noop +addx 3 +addx 5 +addx 2 +addx -11 +addx 16 +addx -2 +addx -17 +addx 24 +noop +noop +addx 1 +addx -38 +addx 15 +addx 10 +addx -15 +noop +addx 2 +addx 26 +noop +addx -21 +addx 19 +addx -33 +addx 19 +noop +addx -6 +addx 9 +addx 3 +addx 4 +addx -21 +addx 4 +addx 20 +noop +addx 3 +addx -38 +addx 28 +addx -21 +addx 9 +addx -8 +addx 2 +addx 5 +addx 2 +addx -9 +addx 14 +addx -2 +addx -5 +addx 12 +addx 3 +addx -2 +addx 2 +addx 7 +noop +noop +addx -27 +addx 28 +addx -36 +noop +addx 1 +addx 5 +addx -1 +noop +addx 6 +addx -1 +addx 5 +addx 5 +noop +noop +addx -2 +addx 20 +addx -10 +addx -3 +addx 1 +addx 3 +addx 2 +addx 4 +addx 3 +noop +addx -30 +noop \ No newline at end of file diff --git a/input/day10_example.txt b/input/day10_example.txt new file mode 100644 index 0000000..94cd0a8 --- /dev/null +++ b/input/day10_example.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop \ No newline at end of file diff --git a/src/day10.rs b/src/day10.rs new file mode 100644 index 0000000..8e3de99 --- /dev/null +++ b/src/day10.rs @@ -0,0 +1,100 @@ + +use crate::day_solver::DaySolver; + +use super::util; + +pub struct Day10 { + instructions: Vec +} + +impl Day10 { + + pub fn create() -> Self { + // let lines = util::read_file("input/day10_example.txt"); + let lines = util::read_file("input/day10.txt"); + + // Put the input into the day struct + return Day10 { + instructions: lines.iter() + .map(|s| { + if s == "noop" { + Instruction::Noop + } else if s.starts_with("addx") { + Instruction::Addx(s.split_whitespace().last().unwrap().parse::().unwrap()) + } else { + panic!("Unknown instruction {}", s) + } + }) + .collect(), + } + } +} + +impl DaySolver for Day10 { + + + fn solve_part1(&mut self) -> String { + + let mut x = 1; + let mut cycle = 1; + let mut interesting_cycle_sum = 0; + for instr in &self.instructions { + + for _ in 0..instr.cycles() { + if (cycle + 20) % 40 == 0 { + // println!("{} * {} = {}", x, cycle, x * cycle); + interesting_cycle_sum += x * cycle; + } + cycle += 1; + } + + if let Instruction::Addx(addx) = instr { + x += addx; + } + } + return interesting_cycle_sum.to_string(); + } + + fn solve_part2(&mut self) -> String { + + let mut x: i32 = 1; + let mut cycle = 1; + let mut res: String = String::from("\n"); + for instr in &self.instructions { + + for _ in 0..instr.cycles() { + if x.abs_diff((cycle - 1) % 40) <= 1 { + res += "#"; + } else { + res += "." + } + if cycle % 40 == 0 { + res += "\n"; + } + cycle += 1; + } + + if let Instruction::Addx(addx) = instr { + x += addx; + } + } + return res; + + } +} + +#[derive(Debug, Clone)] +enum Instruction { + Noop, + Addx(i32) +} + +impl Instruction { + + fn cycles(&self) -> u8 { + match self { + Self::Addx(_) => 2, + _ => 1 + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d479e9d..5eee336 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use crate::day6::Day6; use crate::day7::Day7; use crate::day8::Day8; use crate::day9::Day9; +use crate::day10::Day10; use crate::day_solver::DaySolver; mod util; @@ -21,6 +22,7 @@ mod day6; mod day7; mod day8; mod day9; +mod day10; const MAX_DAY: u8 = 9; const DEFAULT_BENCHMARK_AMOUNT: u32 = 100; @@ -95,6 +97,7 @@ fn build_day_solver(day: u8) -> Option> { 7 => Some(Box::new(Day7::create())), 8 => Some(Box::new(Day8::create())), 9 => Some(Box::new(Day9::create())), + 10 => Some(Box::new(Day10::create())), _ => None } }