From f1481a8b61d1ee22399d55f6be7e74fc634cc019 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 17 Dec 2018 13:56:26 +0100 Subject: Optimise day 17 a bit --- 2018/src/day17.rs | 25 +++++++++++++++---------- 1 file 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(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); -- cgit v1.2.3-70-g09d2