day10: Add part 1 solution
This commit is contained in:
parent
ba121691ba
commit
4274b306c8
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day10"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "day10"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,53 @@
|
||||||
|
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_sum = 0;
|
||||||
|
let mut strength = 1;
|
||||||
|
let mut cycle = 1;
|
||||||
|
for line in reader.lines() {
|
||||||
|
let line = line?;
|
||||||
|
let mut split_iter = line.split_whitespace();
|
||||||
|
let cmd = split_iter.next().ok_or("asdf")?;
|
||||||
|
match cmd {
|
||||||
|
"noop" => {
|
||||||
|
cycle += 1;
|
||||||
|
if (cycle % 40) == 20 {
|
||||||
|
strength_sum += strength * cycle;
|
||||||
|
if DEBUG {
|
||||||
|
println!("{} {} {}", cycle, strength, strength * cycle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"addx" => {
|
||||||
|
cycle += 1;
|
||||||
|
if (cycle % 40) == 20 {
|
||||||
|
strength_sum += strength * cycle;
|
||||||
|
if DEBUG {
|
||||||
|
println!("{} {} {}", cycle, strength, strength * cycle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cycle += 1;
|
||||||
|
let num: i32 = split_iter.next().ok_or("bla")?.parse()?;
|
||||||
|
strength += num;
|
||||||
|
if (cycle % 40) == 20 {
|
||||||
|
strength_sum += strength * cycle;
|
||||||
|
if DEBUG {
|
||||||
|
println!("{} {} {}", cycle, strength, strength * cycle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => Err(format!("Unknown command: {}", cmd))?
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Signal strength is {}", strength_sum);
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue