adventofcode

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

commit
e895158fae6d2249ae7af1d54998a82e8c682126
parent
0919a026c650a162267b5dd40b6306cd0714ecdf
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2021-12-20 08:33
optimize

Diffstat

M 2021/20/part1.rs 34 ++++++++++++++--------------------

1 files changed, 14 insertions, 20 deletions


diff --git a/2021/20/part1.rs b/2021/20/part1.rs

@@ -34,38 +34,32 @@ fn enhance(img: &Vec<Vec<bool>>, outside: bool, cypher: &Vec<bool>) -> (Vec<Vec<
   34    34     let rows = img.len();
   35    35     let cols = img[0].len();
   36    36 
   37    -1     let mut result = vec![];
   38    -1 
   39    -1     for y in 0..rows+2 {
   40    -1         let mut row = vec![];
   41    -1 
   42    -1         for x in 0..cols+2 {
   43    -1             let mut i = 0;
   44    -1 
   45    -1             for dy in [0, 1, 2].iter() {
   46    -1                 if y + dy < 2 || y + dy >= rows + 2 {
   47    -1                     i <<= 3;
   48    -1                     if outside {
   49    -1                         i |= 0b111;
   50    -1                     }
   51    -1                     continue;
   -1    37     let mut result = vec![vec![false; cols + 2]; rows + 2];
   -1    38     let mut i;
   -1    39 
   -1    40     for x in 0..cols+2 {
   -1    41         i = if outside {0b111111111} else {0};
   -1    42         for y in 0..rows+2 {
   -1    43             i &= 0b111111;
   -1    44             if y >= rows {
   -1    45                 i <<= 3;
   -1    46                 if outside {
   -1    47                     i |= 0b111;
   52    48                 }
   53    -1 
   -1    49             } else {
   54    50                 for dx in [0, 1, 2].iter() {
   55    51                     i <<= 1;
   56    52                     if x + dx < 2 || x + dx >= cols + 2 {
   57    53                         if outside {
   58    54                             i |= 1;
   59    55                         }
   60    -1                     } else if img[y + dy - 2][x + dx - 2] {
   -1    56                     } else if img[y][x + dx - 2] {
   61    57                         i |= 1;
   62    58                     }
   63    59                 }
   64    60             }
   65    -1 
   66    -1             row.push(cypher[i]);
   -1    61             result[y][x] = cypher[i];
   67    62         }
   68    -1         result.push(row);
   69    63     }
   70    64 
   71    65     let outside_i = if outside {0b111111111} else {0};