Solution for 2022/day10-part1

This commit is contained in:
Tobias Marschner 2024-02-21 22:15:17 +01:00
parent 9fc55e32f6
commit 182fd18fc8
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,8 @@
[package]
name = "day10-part1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,56 @@
fn main() {
// Use command line arguments to specify the input filename.
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
panic!("Usage: ./main <input-file>\nNo input file provided. Exiting.");
}
// Next, read the contents of the input file into a string for easier processing.
let input = std::fs::read_to_string(&args[1]).expect("Error opening file");
// Line-by-line processing is easiest.
let input = input.lines();
// --- TASK BEGIN ---
// Keep track of the current value of X for all the instructions.
// We'll use one massive vector for that purpose.
let mut x_over_time: Vec<i32> = Vec::with_capacity(512);
// Keep track of the actual x as well.
let mut x = 1;
// Then, process line-by-line.
for line in input {
// Split by space.
let line: Vec<_> = line.split(' ').collect();
// Differentiate by instruction
match line[0] {
"noop" => {
// Nothing changes.
x_over_time.push(x);
}
"addx" => {
// Addition is complete *after* two cycles.
// So during those two cycles x has the old value still.
x_over_time.push(x);
x_over_time.push(x);
// Of course, afterwards the value of x is updated.
x += line[1].parse::<i32>().unwrap();
}
_ => ()
}
}
// dbg!(&x_over_time);
// Afterwards, compute our signal strength result.
let mut signal_strength = 0;
for (i,x) in x_over_time.iter().skip(19).step_by(40).enumerate() {
let cycle: i32 = 20 + (i as i32) * 40;
signal_strength += cycle * x;
}
println!("Signal strength: {}", signal_strength);
}