advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.rs (2483B)

    1 #[derive(Debug, Clone)]
    2 struct Board {
    3     board: std::collections::HashMap<(usize, usize), u32>,
    4 }
    5 
    6 impl Board {
    7     fn new(input: &Vec<Vec<u32>>) -> Self {
    8         let mut board = std::collections::HashMap::<(usize, usize), u32>::new();
    9         for x in 0..input.len() {
   10             for y in 0..input[0].len() {
   11                 board.insert((x, y), input[x][y]);
   12             }
   13         }
   14         Board { board }
   15     }
   16 
   17     fn step_forward(&mut self) -> i32 {
   18         for v in self.board.values_mut() {
   19             *v += 1;
   20         }
   21         let mut tot_flash_count = 0;
   22         loop {
   23             let mut flash_count = 0;
   24             let mut flashed = Vec::<(usize, usize)>::new();
   25             for (k, v) in self.board.iter_mut() {
   26                 if *v > 9 {
   27                     flash_count += 1;
   28                     *v = 0;
   29                     flashed.push(*k);
   30                 }
   31             }
   32             for (x, y) in flashed {
   33                 for dx in -1..=1 {
   34                     for dy in -1..=1 {
   35                         if let Some(v) = self
   36                             .board
   37                             .get_mut(&((x as i32 + dx) as usize, (y as i32 + dy) as usize))
   38                         {
   39                             if *v != 0 {
   40                                 *v += 1;
   41                             }
   42                         }
   43                     }
   44                 }
   45             }
   46             tot_flash_count += flash_count;
   47             if flash_count == 0 {
   48                 break;
   49             }
   50         }
   51         tot_flash_count
   52     }
   53 }
   54 
   55 fn main() {
   56     let input = std::fs::read_to_string("input.txt")
   57         .unwrap()
   58         .trim()
   59         .split('\n')
   60         .map(|s| {
   61             s.chars()
   62                 .map(|c| c.to_digit(10).unwrap())
   63                 .collect::<Vec<u32>>()
   64         })
   65         .collect::<Vec<_>>();
   66     let board = Board::new(&input);
   67     // 1613
   68     println!("Part 1: {}", part_1(&board));
   69     // 510
   70     println!("Part 2: {}", part_2(&board));
   71 }
   72 
   73 fn part_1(board: &Board) -> i32 {
   74     let mut board = board.clone();
   75     let mut total_flashed = 0;
   76     for _ in 0..100 {
   77         let flashed = board.step_forward();
   78         total_flashed += flashed;
   79     }
   80     total_flashed
   81 }
   82 
   83 fn part_2(board: &Board) -> i32 {
   84     let mut board = board.clone();
   85     let mut step = 0;
   86     loop {
   87         let flashed = board.step_forward();
   88         step += 1;
   89         if flashed as usize == board.board.len() {
   90             return step;
   91         }
   92     }
   93 }