advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 8a99d90efaca8d4199de4038e978da7454c8088a
parent 466984b5486557dee53929d890e7fdee0b088829
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Tue,  7 Dec 2021 06:58:12 -0600

Add 2021 day 06

Diffstat:
A2021/day06/input.txt | 1+
A2021/day06/main.rs | 40++++++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+), 0 deletions(-)
diff --git a/2021/day06/input.txt b/2021/day06/input.txt
@@ -0,0 +1 @@
+2,1,1,1,1,1,1,5,1,1,1,1,5,1,1,3,5,1,1,3,1,1,3,1,4,4,4,5,1,1,1,3,1,3,1,1,2,2,1,1,1,5,1,1,1,5,2,5,1,1,2,1,3,3,5,1,1,4,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,4,1,5,1,2,1,1,1,1,5,1,1,1,1,1,5,1,1,1,4,5,1,1,3,4,1,1,1,3,5,1,1,1,2,1,1,4,1,4,1,2,1,1,2,1,5,1,1,1,5,1,2,2,1,1,1,5,1,2,3,1,1,1,5,3,2,1,1,3,1,1,3,1,3,1,1,1,5,1,1,1,1,1,1,1,3,1,1,1,1,3,1,1,4,1,1,3,2,1,2,1,1,2,2,1,2,1,1,1,4,1,2,4,1,1,4,4,1,1,1,1,1,4,1,1,1,2,1,1,2,1,5,1,1,1,1,1,5,1,3,1,1,2,3,4,4,1,1,1,3,2,4,4,1,1,3,5,1,1,1,1,4,1,1,1,1,1,5,3,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,5,1,4,4,1,1,1,1,1,1,1,1,3,1,3,1,4,1,1,2,2,2,1,1,2,1,1
diff --git a/2021/day06/main.rs b/2021/day06/main.rs
@@ -0,0 +1,40 @@
+fn main() {
+    let input = std::fs::read_to_string("input.txt")
+        .unwrap()
+        .trim()
+        .split(',')
+        .map(|x| x.parse::<i32>().unwrap())
+        .collect::<Vec<i32>>();
+    // 388739
+    println!("Part 1: {}", part_1(&input, 80));
+    // 1741362314973
+    println!("Part 2: {}", part_1(&input, 256));
+}
+
+fn part_1(input: &Vec<i32>, days: usize) -> usize {
+    let mut population = {
+        let mut tmp = std::collections::HashMap::<i32, usize>::new();
+        for i in input {
+            tmp.insert(*i, **tmp.get(i).get_or_insert(&0) + 1);
+        }
+        tmp
+    };
+    for _ in 0..days {
+        let mut new_born = None;
+        population = population
+            .into_iter()
+            .map(|(k, v)| {
+                if k == 0 {
+                    new_born = Some(v);
+                    (8, v)
+                } else {
+                    (k - 1, v)
+                }
+            })
+            .collect();
+        if let Some(new_born) = new_born {
+            population.insert(6, *population.get(&6).get_or_insert(&0) + new_born);
+        }
+    }
+    population.values().sum()
+}