From 714405d4660216562302713afb1987aa00f36b3e Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 10 Dec 2022 15:07:20 -0800 Subject: [PATCH] aoc day10 part1 done --- 2022-aoc/src/d10.rs | 272 ++++++++++++++++++++++++++++++++++++++++++++ 2022-aoc/src/lib.rs | 1 + 2 files changed, 273 insertions(+) create mode 100644 2022-aoc/src/d10.rs diff --git a/2022-aoc/src/d10.rs b/2022-aoc/src/d10.rs new file mode 100644 index 0000000..3d6d932 --- /dev/null +++ b/2022-aoc/src/d10.rs @@ -0,0 +1,272 @@ +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; + +#[derive(Debug, Clone, PartialEq, Eq)] +enum Instr { + Noop, + AddX(i32, u8), +} + +impl Instr { + pub fn execute(&mut self) -> Option { + match self { + Instr::AddX(val, done) => { + if *done == 0 { + Some(*val) + } else { + *done -= 1; + None + } + } + Instr::Noop => None, + } + } + + pub fn addx(val: i32) -> Self { + Instr::AddX(val, 1) + } + + pub fn is_noop(&self) -> bool { + self == &Instr::Noop + } +} + +#[derive(Debug, Clone)] +struct Vm { + cycle: usize, + x: i32, + ip: usize, + instrs: Vec, +} + +impl Vm { + pub fn new(instrs: Vec) -> Self { + Vm { + cycle: 1, + x: 1, + ip: 0, + instrs, + } + } + + pub fn run(&mut self) -> (usize, i32) { + let instr = self.instrs.get_mut(self.ip).unwrap(); + let out = (self.cycle, self.x); + let mut incr = false; + if let Some(v) = instr.execute() { + self.x += v; + incr = true; + } + if instr.is_noop() { + incr = true; + } + if incr { + self.ninst(); + } + self.cycle += 1; + out + } + + fn ninst(&mut self) { + self.ip = (self.ip + 1).min(self.instrs.len() - 1); + } +} + +#[aoc_generator(day10)] +fn parse_input(input: &str) -> Vm { + let mut data = Vec::new(); + for line in input.lines() { + match line { + "noop" => data.push(Instr::Noop), + _ => { + let (_, v) = line.split_once(' ').unwrap(); + data.push(Instr::addx(v.parse().unwrap())); + } + } + } + Vm::new(data) +} + +#[aoc_run(day10, part1)] +fn part1(input: &Vm) -> i32 { + let mut out = 0; + let mut vm = input.clone(); + let mut samp = 20; + for _ in 0..221 { + let (cycle, x) = vm.run(); + if cycle % samp == 0 { + samp += 40; + out += cycle as i32 * x; + } + } + out +} + +#[aoc_run(day10, part2)] +fn part2(input: &Vm) -> u32 { + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + const INPUT: &str = "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"; + + #[test] + fn part1_test() { + let vm = parse_input(INPUT); + + assert_eq!(part1(&vm), 13140); + } + + #[test] + fn part2_test() { + let vm = parse_input(INPUT); + assert_eq!(part2(&vm), 1); + } +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs index 92358ca..d33246d 100644 --- a/2022-aoc/src/lib.rs +++ b/2022-aoc/src/lib.rs @@ -9,5 +9,6 @@ mod d6; mod d7; mod d8; mod d9; +mod d10; aoc_lib! { year = 2022 }