From 920945061f23863a2c5f6ed310374edd4fe9850d Mon Sep 17 00:00:00 2001 From: Andrew Coleman Date: Sat, 10 Dec 2022 02:32:57 -0500 Subject: [PATCH] day10 solution --- 2022/day10.txt | 143 ++++++++++++++++++++++++++++++++++++++++ 2022/src/days/day10.rs | 57 ++++++++++++++++ 2022/src/days/mod.rs | 1 + 2022/src/main.rs | 3 + 2022/tests/day10.txt | 146 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 350 insertions(+) create mode 100644 2022/day10.txt create mode 100644 2022/src/days/day10.rs create mode 100644 2022/tests/day10.txt diff --git a/2022/day10.txt b/2022/day10.txt new file mode 100644 index 0000000..1832939 --- /dev/null +++ b/2022/day10.txt @@ -0,0 +1,143 @@ +noop +noop +addx 5 +noop +noop +addx 6 +addx 4 +addx -4 +addx 4 +addx -6 +addx 11 +addx -1 +addx 2 +addx 4 +addx 3 +noop +addx 2 +addx -30 +addx 2 +addx 33 +noop +addx -37 +noop +noop +noop +addx 3 +addx 2 +addx 5 +addx 20 +addx 7 +addx -24 +addx 2 +noop +addx 7 +addx -2 +addx -6 +addx 13 +addx 3 +addx -2 +addx 2 +noop +addx -5 +addx 10 +addx 5 +addx -39 +addx 1 +addx 5 +noop +addx 3 +noop +addx -5 +addx 10 +addx -2 +addx 2 +noop +noop +addx 7 +noop +noop +noop +noop +addx 3 +noop +addx 3 +addx 2 +addx 8 +addx -1 +addx -20 +addx 21 +addx -38 +addx 5 +addx 2 +noop +noop +noop +addx 8 +noop +noop +addx -2 +addx 2 +addx -7 +addx 14 +addx 5 +noop +noop +noop +addx -16 +addx 17 +addx 2 +addx -12 +addx 19 +noop +noop +addx -37 +noop +noop +noop +addx 3 +addx 2 +addx 2 +addx 5 +addx 20 +addx -19 +addx 2 +noop +noop +noop +addx 5 +addx 19 +addx -12 +addx 3 +addx -2 +addx 2 +addx -18 +addx 25 +addx -14 +addx -22 +addx 1 +noop +noop +noop +addx 3 +addx 5 +addx -4 +addx 7 +addx 4 +noop +addx 1 +noop +noop +addx 2 +addx -6 +addx 15 +addx -1 +addx 4 +noop +noop +addx 1 +addx 4 +addx -33 +noop +addx 21 +noop diff --git a/2022/src/days/day10.rs b/2022/src/days/day10.rs new file mode 100644 index 0000000..41279f4 --- /dev/null +++ b/2022/src/days/day10.rs @@ -0,0 +1,57 @@ +use anyhow::{anyhow, Result}; +use std::fs; + +pub fn run() -> Result<()> { + #[cfg(not(feature = "test_input"))] + let file_contents = fs::read_to_string("day10.txt")?; + + #[cfg(feature = "test_input")] + let file_contents = fs::read_to_string("tests/day10.txt")?; + + let mut regx: Vec = Vec::with_capacity(250); + regx.push(1); + + for line in file_contents.lines() { + regx.push(0); + match &line[0..4] { + "noop" => {} + "addx" => { + let count: i32 = line[5..].parse().expect("invalid numeric value"); + regx.push(count); + } + _ => { + return Err(anyhow!("unknown op code")); + } + } + } + + let sum_20: i32 = regx.iter().take(20).sum(); + let sum_60: i32 = regx.iter().take(60).sum(); + let sum_100: i32 = regx.iter().take(100).sum(); + let sum_140: i32 = regx.iter().take(140).sum(); + let sum_180: i32 = regx.iter().take(180).sum(); + let sum_220: i32 = regx.iter().take(220).sum(); + let part1 = + sum_20 * 20 + sum_60 * 60 + sum_100 * 100 + sum_140 * 140 + sum_180 * 180 + sum_220 * 220; + + println!("part1 {}", part1); + + let mut cycle = 0; + let mut sprite = 1; + for reg_val in regx.iter().skip(1) { + if cycle > 0 && cycle % 40 == 0 { + println!(); + } + if sprite >= (cycle % 40) - 1 && sprite <= (cycle % 40) + 1 { + print!("#"); + } else { + print!("."); + } + sprite += reg_val; + + cycle += 1; + } + println!(); + + Ok(()) +} diff --git a/2022/src/days/mod.rs b/2022/src/days/mod.rs index b7940e5..bcca163 100644 --- a/2022/src/days/mod.rs +++ b/2022/src/days/mod.rs @@ -1,4 +1,5 @@ pub mod day1; +pub mod day10; pub mod day2; pub mod day3; pub mod day4; diff --git a/2022/src/main.rs b/2022/src/main.rs index 3d1cfb8..4a7efbb 100644 --- a/2022/src/main.rs +++ b/2022/src/main.rs @@ -39,6 +39,9 @@ fn run_day(number: i32) -> Result<()> { 9 => { days::day9::run()?; } + 10 => { + days::day10::run()?; + } _ => return Err(anyhow!("Invalid day provided")), } Ok(()) diff --git a/2022/tests/day10.txt b/2022/tests/day10.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/2022/tests/day10.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