From 4b4d4bdadda5dfd5528d02bc8666738ff633291f Mon Sep 17 00:00:00 2001 From: Lars Martens Date: Mon, 6 Dec 2021 12:38:41 +0100 Subject: [PATCH] Day 6 --- input/06-test.txt | 1 + input/06.txt | 1 + src/bin/06.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 input/06-test.txt create mode 100644 input/06.txt create mode 100644 src/bin/06.rs diff --git a/input/06-test.txt b/input/06-test.txt new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/input/06-test.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file diff --git a/input/06.txt b/input/06.txt new file mode 100644 index 0000000..877bf65 --- /dev/null +++ b/input/06.txt @@ -0,0 +1 @@ +1,1,3,5,1,1,1,4,1,5,1,1,1,1,1,1,1,3,1,1,1,1,2,5,1,1,1,1,1,2,1,4,1,4,1,1,1,1,1,3,1,1,5,1,1,1,4,1,1,1,4,1,1,3,5,1,1,1,1,4,1,5,4,1,1,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,5,1,1,1,3,4,1,1,1,1,3,1,1,1,1,1,4,1,1,3,1,1,3,1,1,1,1,1,3,1,5,2,3,1,2,3,1,1,2,1,2,4,5,1,5,1,4,1,1,1,1,2,1,5,1,1,1,1,1,5,1,1,3,1,1,1,1,1,1,4,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,2,1,1,1,1,2,2,1,2,1,1,1,5,5,1,1,1,1,1,1,1,1,1,1,1,1,2,2,1,1,4,2,1,4,1,1,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,1,5,1,1,1,1,1,1,1,1,3,1,1,3,3,1,1,1,3,5,1,1,4,1,1,1,1,1,4,1,1,3,1,1,1,1,1,1,1,1,2,1,5,1,1,1,1,1,1,1,1,1,1,4,1,1,1,1 \ No newline at end of file diff --git a/src/bin/06.rs b/src/bin/06.rs new file mode 100644 index 0000000..2c11bb6 --- /dev/null +++ b/src/bin/06.rs @@ -0,0 +1,38 @@ +use std::collections::VecDeque; + +type School = VecDeque; + +fn main() { + let mut fish = input(); + for _ in 0..80 { + step(&mut fish); + } + println!("First solution: {}", sum(&fish)); + + let mut fish = input(); + for _ in 0..256 { + step(&mut fish); + } + println!("Second solution: {}", sum(&fish)); +} + +fn step(fish: &mut School) { + let front = fish.pop_front().unwrap(); + fish.push_back(front); + fish[6] += front; +} + +fn sum(fish: &School) -> u64 { + fish.iter().sum() +} + +fn input() -> School { + let init: School = vec![0; 9].into(); + include_str!("../../input/06.txt") + .split(',') + .map(|n| n.parse().unwrap()) + .fold(init, |mut acc, n| { + acc[n] += 1; + acc + }) +}