day10: Changes for part 2

Could be less repeating by using some object for the counting.
This commit is contained in:
MasterofJOKers 2023-01-03 18:32:18 +01:00
parent 4274b306c8
commit ac5b34606b
1 changed files with 34 additions and 19 deletions

View File

@ -9,45 +9,60 @@ 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;
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" => {
cycle += 1;
if (cycle % 40) == 20 {
strength_sum += strength * cycle;
if DEBUG {
println!("{} {} {}", cycle, strength, strength * cycle)
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" => {
cycle += 1;
if (cycle % 40) == 20 {
strength_sum += strength * cycle;
if DEBUG {
println!("{} {} {}", cycle, strength, strength * cycle)
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;
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);
println!("{lines}");
Ok(())
}