2015: day06: use Iterator on cells
This commit is contained in:
parent
c99426fd39
commit
127cf3f7c4
|
@ -28,25 +28,19 @@ fn part1(input: &str) -> Result<usize> {
|
||||||
for inst in instructions {
|
for inst in instructions {
|
||||||
match inst.action {
|
match inst.action {
|
||||||
Action::TurnOn => {
|
Action::TurnOn => {
|
||||||
((inst.horizontal.0)..=(inst.horizontal.1))
|
for (x, y) in inst {
|
||||||
.flat_map(|i| ((inst.vertical.0)..=(inst.vertical.1)).map(move |j| (i, j)))
|
|
||||||
.for_each(|(x, y)| {
|
|
||||||
grid[x][y] = true;
|
grid[x][y] = true;
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
Action::TurnOff => {
|
Action::TurnOff => {
|
||||||
((inst.horizontal.0)..=(inst.horizontal.1))
|
for (x, y) in inst {
|
||||||
.flat_map(|i| ((inst.vertical.0)..=(inst.vertical.1)).map(move |j| (i, j)))
|
|
||||||
.for_each(|(x, y)| {
|
|
||||||
grid[x][y] = false;
|
grid[x][y] = false;
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
Action::Toggle => {
|
Action::Toggle => {
|
||||||
((inst.horizontal.0)..=(inst.horizontal.1))
|
for (x, y) in inst {
|
||||||
.flat_map(|i| ((inst.vertical.0)..=(inst.vertical.1)).map(move |j| (i, j)))
|
|
||||||
.for_each(|(x, y)| {
|
|
||||||
grid[x][y] = !grid[x][y];
|
grid[x][y] = !grid[x][y];
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,25 +67,19 @@ fn part2(input: &str) -> Result<u64> {
|
||||||
for inst in instructions {
|
for inst in instructions {
|
||||||
match inst.action {
|
match inst.action {
|
||||||
Action::TurnOn => {
|
Action::TurnOn => {
|
||||||
((inst.horizontal.0)..=(inst.horizontal.1))
|
for (x, y) in inst {
|
||||||
.flat_map(|i| ((inst.vertical.0)..=(inst.vertical.1)).map(move |j| (i, j)))
|
|
||||||
.for_each(|(x, y)| {
|
|
||||||
grid[x][y] = grid[x][y].saturating_add(1);
|
grid[x][y] = grid[x][y].saturating_add(1);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
Action::TurnOff => {
|
Action::TurnOff => {
|
||||||
((inst.horizontal.0)..=(inst.horizontal.1))
|
for (x, y) in inst {
|
||||||
.flat_map(|i| ((inst.vertical.0)..=(inst.vertical.1)).map(move |j| (i, j)))
|
|
||||||
.for_each(|(x, y)| {
|
|
||||||
grid[x][y] = grid[x][y].saturating_sub(1);
|
grid[x][y] = grid[x][y].saturating_sub(1);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
Action::Toggle => {
|
Action::Toggle => {
|
||||||
((inst.horizontal.0)..=(inst.horizontal.1))
|
for (x, y) in inst {
|
||||||
.flat_map(|i| ((inst.vertical.0)..=(inst.vertical.1)).map(move |j| (i, j)))
|
|
||||||
.for_each(|(x, y)| {
|
|
||||||
grid[x][y] = grid[x][y].saturating_add(2);
|
grid[x][y] = grid[x][y].saturating_add(2);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,6 +99,22 @@ struct Instruction {
|
||||||
vertical: (usize, usize),
|
vertical: (usize, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IntoIterator for Instruction {
|
||||||
|
type Item = (usize, usize);
|
||||||
|
type IntoIter = Box<dyn Iterator<Item = Self::Item>>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
let begin_x = self.horizontal.0;
|
||||||
|
let end_x = self.horizontal.1;
|
||||||
|
let begin_y = self.vertical.0;
|
||||||
|
let end_y = self.vertical.1;
|
||||||
|
|
||||||
|
let iter = (begin_x..=end_x).flat_map(move |i| (begin_y..=end_y).map(move |j| (i, j)));
|
||||||
|
|
||||||
|
Box::new(iter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for Instruction {
|
impl FromStr for Instruction {
|
||||||
type Err = Box<dyn Error>;
|
type Err = Box<dyn Error>;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue