day10: Changes for part 2

Could be less repeating by using some object for the counting.
2022
MasterofJOKers 1 year ago
parent 4274b306c8
commit ac5b34606b

@ -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" => {
if (strength - 1) <= cycle && (strength + 1) >= cycle {
lines += "#";
} else {
lines += ".";
}
cycle += 1;
if (cycle % 40) == 20 {
strength_sum += strength * cycle;
if DEBUG {
println!("{} {} {}", cycle, strength, strength * cycle)
}
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) == 20 {
strength_sum += strength * cycle;
if DEBUG {
println!("{} {} {}", cycle, strength, strength * cycle)
}
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()?;
strength += num;
if (cycle % 40) == 20 {
strength_sum += strength * cycle;
if DEBUG {
println!("{} {} {}", cycle, strength, strength * cycle)
}
if (cycle % 40) == 0 {
lines += "\n";
cycle = 0;
}
strength += num;
},
_ => Err(format!("Unknown command: {}", cmd))?
};
}
println!("Signal strength is {}", strength_sum);
println!("{lines}");
Ok(())
}

Loading…
Cancel
Save