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 f = File::open("input.txt")?;
let reader = BufReader::new(f); let reader = BufReader::new(f);
let mut strength_sum = 0;
let mut strength = 1; let mut strength = 1;
let mut cycle = 1; let mut cycle = 0;
let mut lines = String::from("");
for line in reader.lines() { for line in reader.lines() {
let line = line?; let line = line?;
let mut split_iter = line.split_whitespace(); let mut split_iter = line.split_whitespace();
let cmd = split_iter.next().ok_or("asdf")?; let cmd = split_iter.next().ok_or("asdf")?;
if DEBUG {
println!("{} {}", cycle, strength);
}
match cmd { match cmd {
"noop" => { "noop" => {
if (strength - 1) <= cycle && (strength + 1) >= cycle {
lines += "#";
} else {
lines += ".";
}
cycle += 1; cycle += 1;
if (cycle % 40) == 20 { if (cycle % 40) == 0 {
strength_sum += strength * cycle; lines += "\n";
if DEBUG { cycle = 0;
println!("{} {} {}", cycle, strength, strength * cycle) }
} if DEBUG {
println!("{} {}", cycle, strength);
} }
}, },
"addx" => { "addx" => {
if (strength - 1) <= cycle && (strength + 1) >= cycle {
lines += "#";
} else {
lines += ".";
}
cycle += 1; cycle += 1;
if (cycle % 40) == 20 { if (cycle % 40) == 0 {
strength_sum += strength * cycle; lines += "\n";
if DEBUG { cycle = 0;
println!("{} {} {}", cycle, strength, strength * cycle) }
} if (strength - 1) <= cycle && (strength + 1) >= cycle {
lines += "#";
} else {
lines += ".";
} }
cycle += 1; cycle += 1;
let num: i32 = split_iter.next().ok_or("bla")?.parse()?; let num: i32 = split_iter.next().ok_or("bla")?.parse()?;
strength += num; if (cycle % 40) == 0 {
if (cycle % 40) == 20 { lines += "\n";
strength_sum += strength * cycle; cycle = 0;
if DEBUG {
println!("{} {} {}", cycle, strength, strength * cycle)
}
} }
strength += num;
}, },
_ => Err(format!("Unknown command: {}", cmd))? _ => Err(format!("Unknown command: {}", cmd))?
}; };
} }
println!("Signal strength is {}", strength_sum); println!("{lines}");
Ok(()) Ok(())
} }