You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.4 KiB
69 lines
1.4 KiB
use std::error;
|
|
use std::fs::File;
|
|
use std::io::BufRead;
|
|
use std::io::BufReader;
|
|
|
|
const DEBUG: bool = false;
|
|
|
|
fn main() -> Result<(), Box<dyn error::Error>> {
|
|
let f = File::open("input.txt")?;
|
|
let reader = BufReader::new(f);
|
|
|
|
let mut strength = 1;
|
|
let mut cycle = 0;
|
|
let mut lines = String::from("");
|
|
for line in reader.lines() {
|
|
let line = line?;
|
|
let mut split_iter = line.split_whitespace();
|
|
let cmd = split_iter.next().ok_or("asdf")?;
|
|
if DEBUG {
|
|
println!("{} {}", cycle, strength);
|
|
}
|
|
match cmd {
|
|
"noop" => {
|
|
if (strength - 1) <= cycle && (strength + 1) >= cycle {
|
|
lines += "#";
|
|
} else {
|
|
lines += ".";
|
|
}
|
|
cycle += 1;
|
|
if (cycle % 40) == 0 {
|
|
lines += "\n";
|
|
cycle = 0;
|
|
}
|
|
if DEBUG {
|
|
println!("{} {}", cycle, strength);
|
|
}
|
|
},
|
|
"addx" => {
|
|
if (strength - 1) <= cycle && (strength + 1) >= cycle {
|
|
lines += "#";
|
|
} else {
|
|
lines += ".";
|
|
}
|
|
cycle += 1;
|
|
if (cycle % 40) == 0 {
|
|
lines += "\n";
|
|
cycle = 0;
|
|
}
|
|
if (strength - 1) <= cycle && (strength + 1) >= cycle {
|
|
lines += "#";
|
|
} else {
|
|
lines += ".";
|
|
}
|
|
cycle += 1;
|
|
let num: i32 = split_iter.next().ok_or("bla")?.parse()?;
|
|
if (cycle % 40) == 0 {
|
|
lines += "\n";
|
|
cycle = 0;
|
|
}
|
|
strength += num;
|
|
},
|
|
_ => Err(format!("Unknown command: {}", cmd))?
|
|
};
|
|
}
|
|
|
|
println!("{lines}");
|
|
Ok(())
|
|
}
|