day 10 done

This commit is contained in:
Dylan "smellyfis" Thies
2022-12-19 19:39:24 -05:00
parent e8a9a65d71
commit f67fa4a3d4
4 changed files with 199 additions and 0 deletions

46
day10/src/main.rs Normal file
View File

@@ -0,0 +1,46 @@
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::str;
fn main() -> std::io::Result<()> {
//Read in file
let file = File::open("input")?;
let reader = BufReader::new(file);
let mut part1: Vec<i32> = Vec::new();
let mut part2 = [[b'.'; 40]; 6];
let mut x_reg = 1;
let mut pc = 0;
reader.lines().for_each(|line| {
let line = line.unwrap();
let op = match line.split(' ').collect::<Vec<_>>()[..] {
["addx", x] => Some(x.parse::<i32>().unwrap()),
["noop"] => None,
_ => panic!("invalid command: {}", line),
};
let steps = if op.is_some() { 2 } else { 1 };
for i in 0..steps {
let row = pc / 40;
let col = pc % 40;
if col - 1 == x_reg || col == x_reg || col + 1 == x_reg {
part2[row as usize][col as usize] = b'#';
}
pc += 1;
if pc < 221 && (pc - 20) % 40 == 0 {
part1.push(pc * x_reg);
}
if i == steps - 1 {
if let Some(x) = op {
x_reg += x;
}
}
}
});
println!("Part 1: {}", part1.iter().sum::<i32>());
for row in part2 {
println!("Part 2: {}", str::from_utf8(&row).unwrap());
}
Ok(())
}