adventofcode

git clone https://git.ce9e.org/adventofcode.git

commit
6af372228cc5f33a27d4867774f288dcde5b873f
parent
4145d262ff46a69a594b7caaf557188c8ae3879e
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-12-14 07:23
perf: reuse previous path

Diffstat

M 2022/14/solution.rs 18 ++++++++++--------

1 files changed, 10 insertions, 8 deletions


diff --git a/2022/14/solution.rs b/2022/14/solution.rs

@@ -47,11 +47,12 @@ fn main() {
   47    47 
   48    48     render(&blocked);
   49    49 
   -1    50     let mut path = vec![(500, 0)];
   50    51     let mut count = 0;
   51    52     let mut part1 = true;
   52    -1     loop {
   53    -1         let mut x = 500;
   54    -1         let mut y = 0;
   -1    53 
   -1    54     while path.len() > 0 {
   -1    55         let (mut x, mut y) = path.pop().unwrap();
   55    56         loop {
   56    57             if y == max_y + 1 {
   57    58                 if part1 {
@@ -63,11 +64,14 @@ fn main() {
   63    64                 count += 1;
   64    65                 break;
   65    66             } else if !blocked.contains(&(x, y + 1)) {
   -1    67                 path.push((x, y));
   66    68                 y += 1;
   67    69             } else if !blocked.contains(&(x - 1, y + 1)) {
   -1    70                 path.push((x, y));
   68    71                 x -= 1;
   69    72                 y += 1;
   70    73             } else if !blocked.contains(&(x + 1, y + 1)) {
   -1    74                 path.push((x, y));
   71    75                 x += 1;
   72    76                 y += 1;
   73    77             } else {
@@ -76,10 +80,8 @@ fn main() {
   76    80                 break;
   77    81             }
   78    82         }
   79    -1         if (x, y) == (500, 0) {
   80    -1             render(&blocked);
   81    -1             println!("part2: {}", count);
   82    -1             break;
   83    -1         }
   84    83     }
   -1    84 
   -1    85     render(&blocked);
   -1    86     println!("part2: {}", count);
   85    87 }