1
0
Fork 0

day(10): refactor a bit more

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-12-10 14:08:28 +01:00
parent 6afe8c41cc
commit 1b3394fd94
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -1,9 +1,10 @@
use std::{fmt::Display, str::FromStr}; use std::{collections::HashMap, fmt::Display, str::FromStr};
use aoc_2022::*; use aoc_2022::*;
use color_eyre::{eyre::eyre, Report}; use color_eyre::{eyre::eyre, Report};
use itertools::Itertools; use itertools::Itertools;
use lazy_static::lazy_static;
type Input = Vec<Instruction>; type Input = Vec<Instruction>;
type Output = Out; type Output = Out;
@ -65,6 +66,10 @@ struct State {
register: i32, register: i32,
} }
lazy_static! {
static ref MAPPING: HashMap<bool, char> = HashMap::from([(false, ' '), (true, '█')]);
}
impl State { impl State {
fn new() -> Self { fn new() -> Self {
Self { Self {
@ -74,9 +79,7 @@ impl State {
} }
fn check_signal_strength(&self, next_cycle: i32, total: &mut i32) { fn check_signal_strength(&self, next_cycle: i32, total: &mut i32) {
*total += if let Some(cycle) = if let Some(cycle) = (self.cycle..next_cycle).find(|&c| c >= 20 && (c + 20) % 40 == 0) {
(self.cycle..next_cycle).find(|&c| c >= 20 && (c + 20) % 40 == 0)
{
// debug!( // debug!(
// "Adding {} x {} = {} to sum", // "Adding {} x {} = {} to sum",
// cycle, // cycle,
@ -84,27 +87,21 @@ impl State {
// cycle * self.register // cycle * self.register
// ); // );
cycle * self.register *total += cycle * self.register;
} else { }
0
};
} }
fn draw_crt(&self, next_cycle: i32, screen: &mut Vec<Vec<char>>) { fn draw_crt(&self, next_cycle: i32, screen: &mut Vec<Vec<char>>) {
// debug!("Checking: {:?} {:?}", i, (self.cycle..next.cycle)); // debug!("Checking: {:?} {:?}", i, (self.cycle..next.cycle));
(self.cycle..next_cycle).for_each(|c| { (self.cycle..next_cycle)
if screen.is_empty() || screen.last().unwrap().len() == 40 { .map(|c| (c - 1) % 40)
.for_each(|c| {
if screen.last().unwrap().len() == 40 {
screen.push(vec![]); screen.push(vec![]);
} }
let idx = screen.len() - 1; let idx = screen.len() - 1;
screen[idx].push( screen[idx].push(MAPPING[&(self.register - 1..=self.register + 1).contains(&c)]);
if self.register - 1 <= (c - 1) % 40 && (c - 1) % 40 <= self.register + 1 {
'█'
} else {
' '
},
);
}); });
} }