advent-of-code

Perserverance, or the lack thereof

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

main.rs (1121B)

    1 fn main() {
    2     let input = std::fs::read_to_string("input.txt")
    3         .unwrap()
    4         .trim()
    5         .split(',')
    6         .map(|x| x.parse::<i32>().unwrap())
    7         .collect::<Vec<i32>>();
    8     // 388739
    9     println!("Part 1: {}", part_1(&input, 80));
   10     // 1741362314973
   11     println!("Part 2: {}", part_1(&input, 256));
   12 }
   13 
   14 fn part_1(input: &Vec<i32>, days: usize) -> usize {
   15     let mut population = {
   16         let mut tmp = std::collections::HashMap::<i32, usize>::new();
   17         for i in input {
   18             tmp.insert(*i, **tmp.get(i).get_or_insert(&0) + 1);
   19         }
   20         tmp
   21     };
   22     for _ in 0..days {
   23         let mut new_born = None;
   24         population = population
   25             .into_iter()
   26             .map(|(k, v)| {
   27                 if k == 0 {
   28                     new_born = Some(v);
   29                     (8, v)
   30                 } else {
   31                     (k - 1, v)
   32                 }
   33             })
   34             .collect();
   35         if let Some(new_born) = new_born {
   36             population.insert(6, *population.get(&6).get_or_insert(&0) + new_born);
   37         }
   38     }
   39     population.values().sum()
   40 }