From 4ad9f14894f7a79d68fd757ced982dffc6523a31 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 13 Dec 2022 18:32:44 -0500 Subject: [PATCH] Progress on day 10 --- day10/src/main.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) 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)]