Minor refactor

This commit is contained in:
Max Nuding 2022-12-10 06:05:28 +00:00
parent a0b13e4f3e
commit 8ae3092389
2 changed files with 148 additions and 146 deletions

View File

@ -1,146 +1,145 @@
ddx 15 noop
addx -11 noop
addx 6
addx -3
addx 5 addx 5
addx -1 addx 21
addx -16
noop
addx 1
noop
noop
addx 4
addx 1
addx 4
addx 1
noop
addx 4
addx -9
noop
addx 19
addx -5
noop
noop
addx 5
addx 1
addx -38
addx 5
addx -2
addx 2
noop
noop
addx 7
addx 9
addx 20
addx -3
addx -18
addx 2
addx 5
noop
noop
addx -2
noop
noop
addx 7
addx 3
addx -2
addx 2
addx -28
addx -7
addx 5
noop
addx 2
addx 32
addx -27
noop
noop
noop
noop
noop
addx 7
noop
addx 22
addx -19
noop
addx 5
noop
addx -7
addx 17
addx -7
noop
addx -20
addx 27
noop
addx -16
addx -20
addx 1
noop
addx 3
addx 15
addx -8 addx -8
addx 13 addx -2
addx -6
addx 14
addx 4 addx 4
noop noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop noop
noop addx -17
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22 addx 22
addx -6 noop
addx 1 addx 5
noop
noop
noop noop
addx 2 addx 2
noop
addx 3
addx -32
addx -5
noop
addx 4
addx 3
addx -2
addx 34
addx -27
addx 5
addx 16
addx -18
addx 7
noop
addx -2
addx -1
addx 8
addx 14
addx -9
noop
addx -15
addx 16
addx 2
addx -35
noop
noop
noop
noop
addx 3
addx 4
noop
addx 1
addx 4
addx 1 addx 1
noop noop
addx -10 addx 4
noop
noop
addx 20
addx 1
addx 2 addx 2
addx 3
addx -5
addx 19
addx -9
addx 2
addx 4
noop
noop
noop
noop
addx 3
addx 2 addx 2
addx -6
addx -11
noop noop
noop noop
noop noop

View File

@ -1,7 +1,4 @@
use crate::read; use crate::read;
use itertools::Itertools;
use std::collections::HashSet;
use std::str::FromStr;
#[derive(Debug, PartialEq, Eq, Clone, Hash)] #[derive(Debug, PartialEq, Eq, Clone, Hash)]
enum Command { enum Command {
@ -20,6 +17,10 @@ impl Command {
} }
pub fn run() { pub fn run() {
const LINE_WIDTH: usize = 40;
const SPRITE_WIDTH: usize = 3;
const SPRITE_WIDTH_HALF: usize = SPRITE_WIDTH / 2;
let input = read("10"); let input = read("10");
let mut lines = input.lines(); let mut lines = input.lines();
@ -28,10 +29,11 @@ pub fn run() {
let mut v: i32 = 1; let mut v: i32 = 1;
let mut v_new: i32 = 1; let mut v_new: i32 = 1;
let mut sig_str = vec![]; let mut sig_str = vec![];
let mut pixels = [false; 40]; let mut pixels = [false; LINE_WIDTH];
let mut rows = vec![];
'outer: loop { 'outer: loop {
if cycle == 20 || (cycle - 20) % 40 == 0 { if cycle == 20 || (cycle > 20 && (cycle - 20) % LINE_WIDTH == 0) {
sig_str.push(cycle * v); sig_str.push(cycle as i32 * v);
} }
if is_busy == 0 { if is_busy == 0 {
match lines.next() { match lines.next() {
@ -52,16 +54,17 @@ pub fn run() {
} else { } else {
is_busy -= 1; is_busy -= 1;
} }
if is_busy == 0 {
v = v_new; let pixel = (cycle - 1) % LINE_WIDTH;
let is_lit = pixel.abs_diff(v as usize) <= SPRITE_WIDTH_HALF;
pixels[pixel] = is_lit;
if pixel == LINE_WIDTH - 1 {
let row: String = pixels.map(|l| if l { '#' } else { ' ' }).iter().collect();
rows.push(row);
} }
let pixel = cycle % 40; if is_busy == 0 {
let is_lit = pixel - 1 == v || pixel == v || pixel + 1 == v; v = v_new;
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; cycle += 1;
} }
@ -73,6 +76,6 @@ pub fn run() {
#[cfg(feature = "part2")] #[cfg(feature = "part2")]
{ {
println!("Day 10, Part 02: {}", "TODO"); println!("Day 10, Part 02: \n{}", rows.join("\n"));
} }
} }