1
0
Fork 0
advent-of-code/2022/src/days/day10.rs

58 lines
1.5 KiB
Rust

use anyhow::{anyhow, Result};
use std::fs;
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
let file_contents = fs::read_to_string("day10.txt")?;
#[cfg(feature = "test_input")]
let file_contents = fs::read_to_string("tests/day10.txt")?;
let mut regx: Vec<i32> = Vec::with_capacity(250);
regx.push(1);
for line in file_contents.lines() {
regx.push(0);
match &line[0..4] {
"noop" => {}
"addx" => {
let count: i32 = line[5..].parse().expect("invalid numeric value");
regx.push(count);
}
_ => {
return Err(anyhow!("unknown op code"));
}
}
}
let sum_20: i32 = regx.iter().take(20).sum();
let sum_60: i32 = regx.iter().take(60).sum();
let sum_100: i32 = regx.iter().take(100).sum();
let sum_140: i32 = regx.iter().take(140).sum();
let sum_180: i32 = regx.iter().take(180).sum();
let sum_220: i32 = regx.iter().take(220).sum();
let part1 =
sum_20 * 20 + sum_60 * 60 + sum_100 * 100 + sum_140 * 140 + sum_180 * 180 + sum_220 * 220;
println!("part1 {}", part1);
let mut cycle = 0;
let mut sprite = 1;
for reg_val in regx.iter().skip(1) {
if cycle > 0 && cycle % 40 == 0 {
println!();
}
if sprite >= (cycle % 40) - 1 && sprite <= (cycle % 40) + 1 {
print!("#");
} else {
print!(".");
}
sprite += reg_val;
cycle += 1;
}
println!();
Ok(())
}