feat: day 10 (38:26.49)

This commit is contained in:
Ashhhleyyy 2022-12-10 20:41:46 +00:00
parent 8eb1167e26
commit f487f8ad0e
Signed by: ash
GPG key ID: 83B789081A0878FB
4 changed files with 387 additions and 1 deletions

138
.aoc-cache/10.txt Normal file
View file

@ -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

102
src/bin/day_10.rs Normal file
View file

@ -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<Self, Self::Err> {
let tokens = s.split_ascii_whitespace().collect::<Vec<_>>();
match &tokens[..] {
["noop"] => Ok(Self::Noop),
["addx", v] => Ok(Self::Addx(v.parse()?)),
_ => bail!("invalid instruction"),
}
}
}
type Input = Vec<Instruction>;
fn parse(s: &str) -> Result<Input> {
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(())
}

View file

@ -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;
}

146
test_cases/day_10.txt Normal file
View file

@ -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