advent_of_code_2022/aoc_10b/src/main.rs

53 lines
1.4 KiB
Rust

use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn update_crt(cycle: i32, register: i32) {
let m = cycle % 40;
if m - register >= 0 && m - register <= 2 {
print!("#");
} else {
print!(".");
}
if m == 0 {
println!("");
}
}
fn main() {
let file_path = String::from("../aoc_10a/input");
let mut cycle: i32 = 0;
let mut register: i32 = 1;
println!("In file {}", file_path);
if let Ok(lines) = read_lines(file_path) {
// Consumes the iterator, returns an (Optional) String
for line in lines {
if let Ok(ip) = line {
let split: Vec<&str> = ip.split(" ").collect();
let v = split.get(1).unwrap_or(&"").parse::<i32>();
match v {
Ok(n) => {
cycle += 1;
update_crt(cycle, register);
cycle += 1;
update_crt(cycle, register);
register += n;
},
Err(_) => {
cycle += 1;
update_crt(cycle, register);
},
}
}
}
}
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}