From 135a5017f767e6be7163ad954fc3e8f0b0f1f547 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 10 Dec 2022 06:38:47 +0100 Subject: [PATCH] 2022 day10/rust: cleanup --- 2022/day10/rust/src/main.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/2022/day10/rust/src/main.rs b/2022/day10/rust/src/main.rs index 6c311bb..d15111a 100644 --- a/2022/day10/rust/src/main.rs +++ b/2022/day10/rust/src/main.rs @@ -2,11 +2,10 @@ use std::{ io::{stdin, Read}, + iter, str::FromStr, }; -use aoc::*; - enum Instruction { Noop, Addx(i32), @@ -17,10 +16,10 @@ impl FromStr for Instruction { fn from_str(s: &str) -> Result { let mut args = s.split_whitespace(); - match args.next().unwrap() { + match args.next().ok_or(())? { "noop" => Ok(Self::Noop), "addx" => { - let i = args.next().unwrap().parse().unwrap(); + let i = args.next().ok_or(())?.parse().map_err(|_| ())?; Ok(Self::Addx(i)) } _ => Err(()), @@ -43,27 +42,25 @@ fn main() { *acc += i; Some(*acc) }); - let values = [1, 1] - .into_iter() + let values = iter::once(1) .chain(values) .enumerate() - .map(|(i, x)| (i32::try_from(i).unwrap(), x)); + .map(|(i, x)| (i32::try_from(i).unwrap() + 1, x)); + let sum: i32 = values .clone() - .skip(20) + .skip(20 - 1) .step_by(40) .map(|(i, x)| i * x) .sum(); + println!("{:?}", sum); let crt: Vec<_> = values - .skip(1) - .map(|(i, x)| ((i - 1) % 40 - x).abs() < 2) + .map(|(i, x)| ((i - 1) % 40).abs_diff(x) < 2) .map(|x| if x { '#' } else { '.' }) .collect(); - let (_, crt) = crt.split_last().unwrap(); - println!("{:?}", sum); - for line in crt.chunks(40) { + for line in crt.chunks_exact(40) { println!("{}", line.iter().collect::()); } }