days 7-10
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day10"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day10"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,83 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{self, BufRead, Write};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Operation {
|
||||
opcode: String,
|
||||
arg: Option<isize>,
|
||||
cycles: usize,
|
||||
}
|
||||
|
||||
const SIGNAL_TICK_COUNT: usize = 220;
|
||||
const DRAW_CRT: usize = 240;
|
||||
|
||||
fn main() {
|
||||
let lines = io::stdin().lock().lines().map(|l| l.unwrap());
|
||||
|
||||
let mut program: Vec<Operation> = vec![];
|
||||
let mut queue: VecDeque<&Operation> = VecDeque::new();
|
||||
|
||||
for line in lines {
|
||||
let cmd = line.split(' ').collect::<Vec<&str>>();
|
||||
let op = cmd.first().unwrap();
|
||||
let mut arg: Option<isize> = None;
|
||||
|
||||
if let Some(num) = cmd.get(1) {
|
||||
arg = Some(num.parse::<isize>().unwrap());
|
||||
}
|
||||
let cycles = match op {
|
||||
&"noop" => 1,
|
||||
&"addx" => 2,
|
||||
invalid => panic!("invalid opcode {}", invalid),
|
||||
};
|
||||
program.push(Operation {
|
||||
opcode: op.to_string(),
|
||||
arg,
|
||||
cycles,
|
||||
});
|
||||
}
|
||||
|
||||
// init counters, program and queue
|
||||
let mut x = 1;
|
||||
let mut current_op = program.first().unwrap();
|
||||
let mut cycles_remaining_for_current = current_op.cycles;
|
||||
let mut signal_sum = 0;
|
||||
let mut screen = [[false; 40]; 6];
|
||||
|
||||
for (screen_idx, i) in (1..DRAW_CRT + 1).enumerate() {
|
||||
if cycles_remaining_for_current == 0 {
|
||||
if current_op.opcode == "addx" {
|
||||
x += current_op.arg.unwrap();
|
||||
}
|
||||
current_op = queue.pop_front().unwrap();
|
||||
cycles_remaining_for_current = current_op.cycles;
|
||||
}
|
||||
let cmd = program.get(i % program.len()).unwrap();
|
||||
queue.push_back(cmd);
|
||||
cycles_remaining_for_current -= 1;
|
||||
match i {
|
||||
20 | 60 | 100 | 140 | 180 | 220 => signal_sum += x * i as isize,
|
||||
_ => (),
|
||||
}
|
||||
|
||||
let (scrx, scry): (usize, usize) = (screen_idx / 40, screen_idx % 40);
|
||||
screen[scrx][scry] = x == screen_idx as isize % 40
|
||||
|| x - 1 == screen_idx as isize % 40
|
||||
|| x + 1 == screen_idx as isize % 40;
|
||||
|
||||
if i == SIGNAL_TICK_COUNT {
|
||||
println!("{}", signal_sum);
|
||||
}
|
||||
}
|
||||
draw_screen(&screen);
|
||||
}
|
||||
|
||||
fn draw_screen(screen: &[[bool; 40]; 6]) {
|
||||
let mut lock = io::stdout().lock();
|
||||
for y in 0..6 {
|
||||
for x in 0..40 {
|
||||
write!(lock, "{}", if screen[y][x] { "#" } else { "." }).unwrap()
|
||||
}
|
||||
writeln!(lock).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user