diff --git a/.aoc-cache/10.txt b/.aoc-cache/10.txt new file mode 100644 index 0000000..a554eb8 --- /dev/null +++ b/.aoc-cache/10.txt @@ -0,0 +1,138 @@ +addx 1 +addx 5 +addx -1 +addx 20 +addx -14 +addx -1 +addx 5 +addx 13 +addx -12 +addx 3 +addx 3 +addx 3 +addx 1 +addx 4 +noop +noop +addx 1 +noop +noop +addx 4 +noop +addx -35 +addx 11 +addx -1 +addx -7 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx 5 +noop +noop +addx -2 +addx 2 +noop +addx 3 +addx 2 +addx 7 +noop +noop +addx 3 +addx -2 +addx -36 +noop +addx 25 +addx -22 +addx 7 +noop +addx -2 +noop +noop +noop +addx 5 +addx 5 +addx 4 +noop +addx -2 +addx 5 +addx -4 +addx 5 +addx 4 +noop +addx -29 +addx 32 +addx -23 +addx -12 +noop +addx 7 +noop +addx -2 +addx 4 +addx 3 +addx 20 +addx 3 +addx -20 +addx 5 +addx 16 +addx -15 +addx 6 +noop +noop +noop +addx 5 +noop +addx 5 +noop +noop +noop +addx -37 +addx 2 +addx -2 +addx 7 +noop +addx -2 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx -6 +addx -15 +addx 24 +addx 2 +noop +addx 3 +addx -8 +addx 15 +addx -14 +addx 15 +addx -38 +noop +noop +addx 21 +addx -14 +addx 1 +addx 5 +noop +addx -2 +addx 7 +addx -1 +addx 5 +noop +addx 2 +addx 3 +addx 3 +addx -2 +addx 4 +addx 2 +addx -17 +addx 20 +noop +noop +noop +noop diff --git a/src/bin/day_10.rs b/src/bin/day_10.rs new file mode 100644 index 0000000..86811a6 --- /dev/null +++ b/src/bin/day_10.rs @@ -0,0 +1,102 @@ +use std::{str::FromStr, fs::File, io::Write}; + +use aoc_2022::prelude::*; + +enum Instruction { + Addx(isize), + Noop, +} + +impl FromStr for Instruction { + type Err = color_eyre::Report; + + fn from_str(s: &str) -> Result { + let tokens = s.split_ascii_whitespace().collect::>(); + match &tokens[..] { + ["noop"] => Ok(Self::Noop), + ["addx", v] => Ok(Self::Addx(v.parse()?)), + _ => bail!("invalid instruction"), + } + } +} + +type Input = Vec; + +fn parse(s: &str) -> Result { + let mut instructions = vec![]; + for line in s.lines() { + instructions.push(line.parse()?); + } + Ok(instructions) +} + +#[aoc(day = 10, parse = parse, test_cases = ["day_10.txt"])] +fn day_10(input: Input) -> Result<()> { + // Part 1 + let mut x = 1; + let mut cycle = 0; + + let mut total = 0; + + for instruction in &input { + cycle += 1; + if cycle % 40 == 20 { + total += dbg!(cycle * x); + } + match instruction { + Instruction::Noop => {}, + &Instruction::Addx(v) => { + cycle += 1; + if cycle % 40 == 20 { + total += dbg!(cycle * x); + } + x += v; + } + } + + if cycle >= 221 { + break; + } + } + println!("Part one: {total}"); + + // Part 2 + println!("Part two: "); + + let mut x: isize = 0; + let mut cycle = 0; + + let mut output = std::io::stdout(); + + for instruction in &input { + cycle += 1; + let h_pixel = (cycle - 1) % 40; + if h_pixel == x || h_pixel == x - 1 || h_pixel == x + 1 { + write!(output, "#")?; + } else { + write!(output, " ")?; + } + if h_pixel == 39 { + writeln!(output, )?; + } + + match instruction { + Instruction::Noop => {}, + &Instruction::Addx(v) => { + cycle += 1; + x += v; + let h_pixel = (cycle - 1) % 40; + if h_pixel == x || h_pixel == x - 1 || h_pixel == x + 1 { + write!(output, "#")?; + } else { + write!(output, " ")?; + } + if h_pixel == 39 { + writeln!(output, )?; + } + } + } + } + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index 567452e..84472aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,6 @@ impl std::io::Write for NullWriter { pub mod prelude { pub use super::{data, load_data}; - pub use color_eyre::{eyre::eyre, Result}; + pub use color_eyre::{eyre::{bail, eyre}, Result}; pub use aoc_proc::aoc; } diff --git a/test_cases/day_10.txt b/test_cases/day_10.txt new file mode 100644 index 0000000..94cd0a8 --- /dev/null +++ b/test_cases/day_10.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