solved day 6 part 2

This commit is contained in:
timeshifter
2026-06-07 20:59:18 +02:00
parent f3cc4c7246
commit 738b558c54
+47 -2
View File
@@ -16,7 +16,9 @@ enum Switch {
} }
#[derive(Debug)] #[derive(Debug)]
enum ParseLineError {} enum ParseLineError {
WrongKeyword,
}
impl std::fmt::Display for ParseLineError { impl std::fmt::Display for ParseLineError {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -60,7 +62,7 @@ impl FromStr for Switch {
"turn on" => Switch::On(rect), "turn on" => Switch::On(rect),
"turn off" => Switch::Off(rect), "turn off" => Switch::Off(rect),
"toggle" => Switch::Toggle(rect), "toggle" => Switch::Toggle(rect),
_ => unreachable!(), _ => return Err(ParseLineError::WrongKeyword),
}; };
Ok(instance) Ok(instance)
@@ -75,6 +77,8 @@ fn main() {
.collect(); .collect();
part_1(&data); part_1(&data);
part_2(&data);
} }
#[allow(clippy::needless_range_loop)] #[allow(clippy::needless_range_loop)]
@@ -119,3 +123,44 @@ fn part_1(data: &[Switch]) {
println!("{sum}"); println!("{sum}");
} }
#[allow(clippy::needless_range_loop)]
fn part_2(data: &[Switch]) {
let mut grid = vec![vec![0_usize; 1000]; 1000];
for task in data {
match task {
Switch::On(rect) => {
for x in rect.0.0..=rect.1.0 {
for y in rect.0.1..=rect.1.1 {
grid[x][y] += 1;
}
}
}
Switch::Off(rect) => {
for x in rect.0.0..=rect.1.0 {
for y in rect.0.1..=rect.1.1 {
grid[x][y] = grid[x][y].saturating_sub(1);
}
}
}
Switch::Toggle(rect) => {
for x in rect.0.0..=rect.1.0 {
for y in rect.0.1..=rect.1.1 {
grid[x][y] += 2;
}
}
}
}
}
let mut sum = 0;
for x in 0..1000 {
for y in 0..1000 {
sum += grid[x][y];
}
}
println!("{sum}");
}