day 10
This commit is contained in:
55
day10/src/main.rs
Normal file
55
day10/src/main.rs
Normal file
@ -0,0 +1,55 @@
|
||||
fn do_cycle(cycle: u64, x_reg: i64) {
|
||||
let col = (cycle - 1) % 40;
|
||||
|
||||
// println!("{} <= ")
|
||||
if x_reg - 1 <= col.try_into().unwrap() && x_reg + 1 >= col.try_into().unwrap() {
|
||||
print!("#");
|
||||
} else {
|
||||
print!(".");
|
||||
}
|
||||
|
||||
if col == 39 {
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
const INPUT: &str = include_str!("../input.txt");
|
||||
const ADD_CYCLES: u64 = 2;
|
||||
|
||||
let mut strengths: Vec<i64> = Vec::new();
|
||||
let mut cycles = 1u64;
|
||||
let mut x_reg = 1i64;
|
||||
|
||||
do_cycle(cycles, x_reg);
|
||||
|
||||
|
||||
for line in INPUT.lines() {
|
||||
|
||||
if line.starts_with("noop") {
|
||||
cycles += 1;
|
||||
} else {
|
||||
let (_, operand) = line.split_once(' ').unwrap();
|
||||
let operand = operand.parse::<i64>().unwrap();
|
||||
|
||||
for _ in 0..(ADD_CYCLES - 1) {
|
||||
cycles += 1;
|
||||
if (cycles + 20) % 40 == 0 {
|
||||
strengths.push((cycles as i64) * x_reg);
|
||||
}
|
||||
do_cycle(cycles, x_reg);
|
||||
}
|
||||
|
||||
cycles += 1;
|
||||
x_reg += operand;
|
||||
}
|
||||
|
||||
if (cycles + 20) % 40 == 0 {
|
||||
strengths.push((cycles as i64) * x_reg);
|
||||
}
|
||||
do_cycle(cycles, x_reg);
|
||||
}
|
||||
|
||||
let sum: i64 = strengths.iter().sum();
|
||||
println!("{}", sum);
|
||||
}
|
Reference in New Issue
Block a user