diff --git a/day10/src/main.rs b/day10/src/main.rs index 5841013..b27b258 100644 --- a/day10/src/main.rs +++ b/day10/src/main.rs @@ -1,5 +1,80 @@ +use std::collections::HashMap; + +#[derive(Debug)] +enum Instruction { + Addx(isize), + Noop, +} +use Instruction::*; + +impl Instruction { + pub fn from_line(line: &str) -> Self { + let parts: Vec<&str> = line.split_ascii_whitespace().collect(); + + if parts.len() == 2 && parts[0] == "addx" { + let value = parts[1].parse::().unwrap(); + + return Addx(value); + } + + Noop + } +} + +// ----------------------------------------------------------------------------- + +#[derive(Debug)] +struct CPU { + x: isize, +} + +impl CPU { + pub fn new() -> Self { + CPU { x: 1 } + } + + fn noop(&self) -> (usize, Option) { + (1, None) + } + + fn addx(&mut self, i: isize) -> (usize, Option) { + self.x += i; + + (2, Some(self.x)) + } + + pub fn run(&mut self, command: Instruction) -> (usize, Option) { + match command { + Addx(i) => self.addx(i), + Noop => self.noop(), + } + } +} + +// ----------------------------------------------------------------------------- + +#[derive(Debug)] +struct CycleCounter { + cpu: CPU, + log: HashMap, + cycle: usize, +} + +impl CycleCounter { + pub fn new() -> Self { + Self { + cpu: CPU::new(), + log: HashMap::new(), + cycle: 1, + } + } +} + +// ----------------------------------------------------------------------------- + fn main() { let file_str = include_str!("input.txt"); + let instructions: Vec = file_str.lines().map(Instruction::from_line).collect(); } #[cfg(test)]