Day 10 initial

This commit is contained in:
2022-12-10 05:36:25 +00:00
parent 8787215fdf
commit a0b13e4f3e
2 changed files with 209 additions and 4 deletions

View File

@ -1,15 +1,74 @@
use crate::read;
use std::collections::HashSet;
use itertools::Itertools;
use std::collections::HashSet;
use std::str::FromStr;
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
enum Command {
AddX(i32),
Noop,
}
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())
}
}
}
//#[derive(Debug, PartialEq, Eq, Default, Clone, Hash)]
pub fn run() {
let input = read("10");
let lines = input.lines();
let mut lines = input.lines();
let mut cycle = 1;
let mut is_busy = 0;
let mut v: i32 = 1;
let mut v_new: i32 = 1;
let mut sig_str = vec![];
let mut pixels = [false; 40];
'outer: loop {
if cycle == 20 || (cycle - 20) % 40 == 0 {
sig_str.push(cycle * v);
}
if is_busy == 0 {
match lines.next() {
Some(s) => {
let cmd = Command::from_str(s);
match cmd {
Command::Noop => {}
Command::AddX(x) => {
v_new += x;
is_busy = 1;
}
}
}
_ => {
break 'outer;
}
};
} else {
is_busy -= 1;
}
if is_busy == 0 {
v = v_new;
}
let pixel = cycle % 40;
let is_lit = pixel - 1 == v || pixel == v || pixel + 1 == v;
pixels[pixel as usize] = is_lit;
if pixel == 0 && cycle != 0 {
let row: String = pixels.map(|l| if l { '#' } else { '.' }).iter().collect();
println!("{}", row);
}
cycle += 1;
}
#[cfg(feature = "part1")]
{
println!("Day 10, Part 01: {}", "TODO");
println!("Day 10, Part 01: {}", sig_str.iter().sum::<i32>());
}
#[cfg(feature = "part2")]