diff --git a/2022/input/10/input b/2022/input/10/input new file mode 100644 index 0000000..734f57f --- /dev/null +++ b/2022/input/10/input @@ -0,0 +1,137 @@ +noop +noop +addx 5 +addx 3 +addx -2 +noop +addx 5 +addx 4 +noop +addx 3 +noop +addx 2 +addx -17 +addx 18 +addx 3 +addx 1 +noop +addx 5 +noop +addx 1 +addx 2 +addx 5 +addx -40 +noop +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 3 +addx -2 +addx 2 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx -24 +addx 31 +addx 2 +addx -33 +addx -6 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 3 +noop +addx 2 +addx -1 +addx 6 +noop +noop +addx 1 +addx 4 +noop +noop +addx -15 +addx 20 +noop +addx -23 +addx 27 +noop +addx -35 +addx 1 +noop +noop +addx 5 +addx 11 +addx -10 +addx 4 +addx 1 +noop +addx 2 +addx 2 +noop +addx 3 +noop +addx 3 +addx 2 +noop +addx 3 +addx 2 +addx 11 +addx -4 +addx 2 +addx -38 +addx -1 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx -7 +addx 8 +addx 2 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx -25 +addx 26 +addx 2 +addx 8 +addx -1 +addx 2 +addx -2 +addx -37 +addx 5 +addx 3 +addx -1 +addx 5 +noop +addx 22 +addx -21 +addx 2 +addx 5 +addx 2 +addx 13 +addx -12 +addx 4 +noop +noop +addx 5 +addx 1 +noop +noop +addx 2 +noop +addx 3 +noop +noop diff --git a/2022/input/10/test-1 b/2022/input/10/test-1 new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/2022/input/10/test-1 @@ -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 diff --git a/2022/src/bin/day10.rs b/2022/src/bin/day10.rs new file mode 100644 index 0000000..2e99db6 --- /dev/null +++ b/2022/src/bin/day10.rs @@ -0,0 +1,137 @@ +#![feature(test)] +use anyhow::Result; +use aoc::Solver; + +// -- Runners -- +fn main() -> Result<()> { + Day::solve() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_test1() -> Result<()> { + Day::test(aoc::Part::ONE, "test-1", 13140) + } + #[test] + fn part1_solution() -> Result<()> { + Day::test(aoc::Part::ONE, "input", 13220) + } + #[test] + fn part2_test1() -> Result<()> { + Day::test(aoc::Part::TWO, "test-1", -1) + } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::ONE, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(aoc::Part::TWO, b) + } +} + +enum Instruction { + NoOp, + AddX(isize), +} + +struct CPU { + x: isize +} + +impl CPU { + fn new() -> Self { + Self { x: 1 } + } + // Executes the given instruction + // Returns the number of cycles that it took to execute the instruction + // Also returns the value of x during the execution (SO NOT THE FINAL VALUE) + fn execute(&mut self, instruction: &Instruction) -> (isize, isize) { + match instruction { + Instruction::NoOp => (1, self.x), + Instruction::AddX(value) => { + let state = (2, self.x); + self.x += value; + return state; + } + } + } +} + +fn parse(input: &str) -> Vec { + input.lines() + .map(|line| line.split(" ").collect::>()) + .map(|line| match line.as_slice() { + ["noop"] => Instruction::NoOp, + ["addx", value] => Instruction::AddX(value.parse().unwrap()), + _ => panic!("Unknown instruction") + }) + .collect::>() +} + +// -- Solution -- +pub struct Day; +impl aoc::Solver for Day { + type Output = isize; + fn day() -> u8 { + 10 + } + + fn part1(input: &str) -> Self::Output { + let instructions = parse(input); + let mut cpu = CPU::new(); + + let mut cycle = 1; + let mut sum = 0; + for instruction in instructions { + let (cycle_count, x) = cpu.execute(&instruction); + + for c in 0..cycle_count { + let cc = cycle + c; + if (cc+ 20) % 40 == 0 { + sum += x * cc; + } + } + + cycle += cycle_count; + } + + sum + } + + fn part2(input: &str) -> Self::Output { + let instructions = parse(input); + let mut cpu = CPU::new(); + + let mut cycle = 1; + for instruction in instructions { + let (cycle_count, x) = cpu.execute(&instruction); + + for c in 0..cycle_count { + let ccm = (cycle + c - 1) % 40; + let mut sign = ' '; + if ccm-1 == x || ccm == x || ccm+1 == x { + sign = '#'; + } + print!("{}", sign); + + if ccm == 39 { + println!(""); + } + } + + cycle += cycle_count; + } + + // @TODO Figure out how we return this + 0 + } +}