adventofcode

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

commit
1cf4e90b72b8adc3b410adf8aa4f0efd4b6a5f85
parent
f02078d42e1a016bb5519920522f02b9db0cb4d9
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-12-07 12:28
fixup 2024-12-04

Diffstat

M 2024/04/solution.rs 89 ++++++++++++++++++++++++++++++++++---------------------------

1 files changed, 49 insertions, 40 deletions


diff --git a/2024/04/solution.rs b/2024/04/solution.rs

@@ -1,7 +1,34 @@
    1    -1 #[path = "../lib.rs"] mod lib;
   -1     1 #[path = "../lib.rs"]
   -1     2 mod lib;
    2     3 
    3    -1 fn parse_input() -> Vec<Vec<char>> {
    4    -1     return lib::iter_input().map(|line| line.chars().collect()).collect();
   -1     4 fn parse_input() -> Vec<Vec<u8>> {
   -1     5     return lib::iter_input()
   -1     6         .map(|line| line.bytes().collect())
   -1     7         .collect();
   -1     8 }
   -1     9 
   -1    10 fn add(x: usize, dx: isize, max: usize) -> Option<usize> {
   -1    11     if dx > 0 {
   -1    12         let y = x + (dx as usize);
   -1    13         if y < max {
   -1    14             return Some(y);
   -1    15         } else {
   -1    16             return None;
   -1    17         }
   -1    18     } else {
   -1    19         let dy = -dx as usize;
   -1    20         if x >= dy {
   -1    21             return Some(x - dy);
   -1    22         } else {
   -1    23             return None;
   -1    24         }
   -1    25     }
   -1    26 }
   -1    27 
   -1    28 fn get(map: &Vec<Vec<u8>>, x: usize, y: usize, dx: isize, dy: isize) -> Option<u8> {
   -1    29     let xx = add(x, dx, map[0].len())?;
   -1    30     let yy = add(y, dy, map.len())?;
   -1    31     return Some(map[yy][xx]);
    5    32 }
    6    33 
    7    34 fn main() {
@@ -15,44 +42,26 @@ fn main() {
   15    42 
   16    43     for y in 0..h {
   17    44         for x in 0..w {
   18    -1             if map[y][x] == 'X' {
   19    -1                 if x + 3 < w && map[y][x + 1] == 'M' && map[y][x + 2] == 'A' && map[y][x + 3] == 'S' {
   20    -1                     count1 += 1;
   21    -1                 }
   22    -1                 if x >= 3 && map[y][x - 1] == 'M' && map[y][x - 2] == 'A' && map[y][x - 3] == 'S' {
   23    -1                     count1 += 1;
   24    -1                 }
   25    -1 
   26    -1                 if y + 3 < h && map[y + 1][x] == 'M' && map[y + 2][x] == 'A' && map[y + 3][x] == 'S' {
   27    -1                     count1 += 1;
   28    -1                 }
   29    -1                 if y >= 3 && map[y - 1][x] == 'M' && map[y - 2][x] == 'A' && map[y - 3][x] == 'S' {
   30    -1                     count1 += 1;
   31    -1                 }
   32    -1 
   33    -1                 if x + 3 < w && y + 3 < h && map[y + 1][x + 1] == 'M' && map[y + 2][x + 2] == 'A' && map[y + 3][x + 3] == 'S' {
   34    -1                     count1 += 1;
   35    -1                 }
   36    -1                 if x + 3 < w && y >= 3 && map[y - 1][x + 1] == 'M' && map[y - 2][x + 2] == 'A' && map[y - 3][x + 3] == 'S' {
   37    -1                     count1 += 1;
   38    -1                 }
   39    -1 
   40    -1                 if x >= 3 && y + 3 < h && map[y + 1][x - 1] == 'M' && map[y + 2][x - 2] == 'A' && map[y + 3][x - 3] == 'S' {
   41    -1                     count1 += 1;
   42    -1                 }
   43    -1                 if x >= 3 && y >= 3 && map[y - 1][x - 1] == 'M' && map[y - 2][x - 2] == 'A' && map[y - 3][x - 3] == 'S' {
   44    -1                     count1 += 1;
   45    -1                 }
   46    -1             } else if map[y][x] == 'A' && x > 0 && x + 1 < w && y > 0 && y + 1 < h {
   47    -1                 if map[y - 1][x - 1] == 'M' && map[y - 1][x + 1] == 'S' && map[y + 1][x - 1] == 'M' && map[y + 1][x + 1] == 'S' {
   48    -1                     count2 += 1;
   49    -1                 } else if map[y - 1][x - 1] == 'M' && map[y - 1][x + 1] == 'M' && map[y + 1][x - 1] == 'S' && map[y + 1][x + 1] == 'S' {
   50    -1                     count2 += 1;
   51    -1                 } else if map[y - 1][x - 1] == 'S' && map[y - 1][x + 1] == 'M' && map[y + 1][x - 1] == 'S' && map[y + 1][x + 1] == 'M' {
   52    -1                     count2 += 1;
   53    -1                 } else if map[y - 1][x - 1] == 'S' && map[y - 1][x + 1] == 'S' && map[y + 1][x - 1] == 'M' && map[y + 1][x + 1] == 'M' {
   54    -1                     count2 += 1;
   -1    45             if map[y][x] == b'X' {
   -1    46                 for dx in -1..=1 {
   -1    47                     for dy in -1..=1 {
   -1    48                         if get(&map, x, y, dx, dy) == Some(b'M')
   -1    49                             && get(&map, x, y, 2 * dx, 2 * dy) == Some(b'A')
   -1    50                             && get(&map, x, y, 3 * dx, 3 * dy) == Some(b'S')
   -1    51                         {
   -1    52                             count1 += 1;
   -1    53                         }
   -1    54                     }
   55    55                 }
   -1    56             } else if map[y][x] == b'A'
   -1    57                 && ((get(&map, x, y, -1, -1) == Some(b'M') && get(&map, x, y, 1, 1) == Some(b'S'))
   -1    58                     || (get(&map, x, y, -1, -1) == Some(b'S')
   -1    59                         && get(&map, x, y, 1, 1) == Some(b'M')))
   -1    60                 && ((get(&map, x, y, 1, -1) == Some(b'M') && get(&map, x, y, -1, 1) == Some(b'S'))
   -1    61                     || (get(&map, x, y, 1, -1) == Some(b'S')
   -1    62                         && get(&map, x, y, -1, 1) == Some(b'M')))
   -1    63             {
   -1    64                 count2 += 1;
   56    65             }
   57    66         }
   58    67     }