diff options
author | tomsmeding <tom.smeding@gmail.com> | 2018-12-17 13:56:26 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2018-12-17 13:57:26 +0100 |
commit | f1481a8b61d1ee22399d55f6be7e74fc634cc019 (patch) | |
tree | 0e898a0159c2ddc71dd7371d18a8cefb6f226c59 | |
parent | 0d3f096f6e9c01203031f5a2a0ce066bdb61b3be (diff) |
Optimise day 17 a bit
-rw-r--r-- | 2018/src/day17.rs | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/2018/src/day17.rs b/2018/src/day17.rs index 3730e60..f920421 100644 --- a/2018/src/day17.rs +++ b/2018/src/day17.rs @@ -174,8 +174,9 @@ fn apply_patterns(grid: &mut Grid, (x, y): (i32, i32)) -> Vec<(i32, i32)> { } for dx in &[-1, 1] { - // WP···PW -> W~···~W - // W···W W···W + // if dx = 1: + // WP?···?W -> W~···~W where ? in {P, .} + // W····W W···W if grid.get(x, y).produces() && grid.get(x, y+1).is_wall() && grid.get(x-dx, y).is_wall() { let mut x2 = x + dx; let make_still; @@ -186,20 +187,20 @@ fn apply_patterns(grid: &mut Grid, (x, y): (i32, i32)) -> Vec<(i32, i32)> { break; } - if !(grid.get(x2, y).produces() && grid.get(x2, y+1).is_wall()) { + if (grid.get(x2, y).produces() || grid.get(x2, y) == Cell::Sand) && grid.get(x2, y+1).is_wall() { + x2 += dx; + } else { make_still = false; break; } - - x2 += dx; } if make_still { - x2 = x; - while !grid.get(x2, y).is_wall() { - grid.set(x2, y, Cell::Still); - res.push((x2, y-1)); - x2 += dx; + let mut x3 = x; + while x3 != x2 { + grid.set(x3, y, Cell::Still); + res.push((x3, y-1)); + x3 += dx; } } } @@ -253,6 +254,10 @@ pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> { // inqueue.remove(&(x, y)); // let pos = (x, y); + if grid.get(pos.0, pos.1) == Cell::Still { + continue; + } + for &(x, y) in apply_patterns(&mut grid, pos).iter().filter(|&&(x, y)| grid.inrange(x, y)) { let p = (-y, x); // let p = (x, y); |