From d42491f71a64c10cd50d9a98d543b74adabf23ab Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Sat, 10 Dec 2022 07:37:03 +0000 Subject: [PATCH] improved command parsing --- src/day10/mod.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/day10/mod.rs b/src/day10/mod.rs index 57746b8..763924d 100644 --- a/src/day10/mod.rs +++ b/src/day10/mod.rs @@ -7,11 +7,22 @@ enum Command { } impl Command { fn from_str(s: &str) -> Self { - if s == "noop" { - Self::Noop - } else { - let sp = s.split_once(' ').unwrap(); - Self::AddX(sp.1.parse().unwrap()) + match s { + "noop" => Command::Noop, + s => { + let (c, i) = s.split_once(' ').unwrap(); + match (c, i.parse::()) { + ("addx", Ok(i)) => Self::AddX(i), + _ => unreachable!(), + } + } + } + } + + fn num_cycles(&self) -> usize { + match self { + Self::Noop => 1, + Self::AddX(_) => 2, } } } @@ -32,9 +43,11 @@ pub fn run() { let mut pixels = [false; LINE_WIDTH]; let mut rows = vec![]; 'outer: loop { + // Part 1 if cycle == 20 || (cycle > 20 && (cycle - 20) % LINE_WIDTH == 0) { sig_str.push(cycle as i32 * v); } + if is_busy == 0 { match lines.next() { Some(s) => { @@ -43,17 +56,16 @@ pub fn run() { Command::Noop => {} Command::AddX(x) => { v_new += x; - is_busy = 1; } - } + }; + is_busy = cmd.num_cycles(); } _ => { break 'outer; } }; - } else { - is_busy -= 1; } + is_busy -= 1; let pixel = (cycle - 1) % LINE_WIDTH; let is_lit = pixel.abs_diff(v as usize) <= SPRITE_WIDTH_HALF;