diff options
author | Federico Igne <git@federicoigne.com> | 2021-12-06 14:33:31 +0000 |
---|---|---|
committer | Federico Igne <git@federicoigne.com> | 2021-12-06 14:33:31 +0000 |
commit | c8734354d4d7cc4fb822f426ef9f32a787420bb4 (patch) | |
tree | e8580f77e47936ff4ac606e99e988206edb8f18b /day6 | |
parent | f9213a03a8d5efd5023c3234549d84d2805289dd (diff) | |
download | aoc-c8734354d4d7cc4fb822f426ef9f32a787420bb4.tar.gz aoc-c8734354d4d7cc4fb822f426ef9f32a787420bb4.zip |
Day 6
Diffstat (limited to 'day6')
-rw-r--r-- | day6/Cargo.toml | 9 | ||||
-rw-r--r-- | day6/resources/input.txt | 1 | ||||
-rw-r--r-- | day6/src/main.rs | 48 |
3 files changed, 58 insertions, 0 deletions
diff --git a/day6/Cargo.toml b/day6/Cargo.toml new file mode 100644 index 0000000..422ffef --- /dev/null +++ b/day6/Cargo.toml | |||
@@ -0,0 +1,9 @@ | |||
1 | [package] | ||
2 | name = "day6" | ||
3 | version = "0.1.0" | ||
4 | authors = ["Federico Igne <git@federicoigne.com>"] | ||
5 | edition = "2018" | ||
6 | |||
7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
8 | |||
9 | [dependencies] | ||
diff --git a/day6/resources/input.txt b/day6/resources/input.txt new file mode 100644 index 0000000..c35cc57 --- /dev/null +++ b/day6/resources/input.txt | |||
@@ -0,0 +1 @@ | |||
5,1,1,5,4,2,1,2,1,2,2,1,1,1,4,2,2,4,1,1,1,1,1,4,1,1,1,1,1,5,3,1,4,1,1,1,1,1,4,1,5,1,1,1,4,1,2,2,3,1,5,1,1,5,1,1,5,4,1,1,1,4,3,1,1,1,3,1,5,5,1,1,1,1,5,3,2,1,2,3,1,5,1,1,4,1,1,2,1,5,1,1,1,1,5,4,5,1,3,1,3,3,5,5,1,3,1,5,3,1,1,4,2,3,3,1,2,4,1,1,1,1,1,1,1,2,1,1,4,1,3,2,5,2,1,1,1,4,2,1,1,1,4,2,4,1,1,1,1,4,1,3,5,5,1,2,1,3,1,1,4,1,1,1,1,2,1,1,4,2,3,1,1,1,1,1,1,1,4,5,1,1,3,1,1,2,1,1,1,5,1,1,1,1,1,3,2,1,2,4,5,1,5,4,1,1,3,1,1,5,5,1,3,1,1,1,1,4,4,2,1,2,1,1,5,1,1,4,5,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,4,2,1,1,1,2,5,1,4,1,1,1,4,1,1,5,4,4,3,1,1,4,5,1,1,3,5,3,1,2,5,3,4,1,3,5,4,1,3,1,5,1,4,1,1,4,2,1,1,1,3,2,1,1,4 \ No newline at end of file | |||
diff --git a/day6/src/main.rs b/day6/src/main.rs new file mode 100644 index 0000000..e3ef79c --- /dev/null +++ b/day6/src/main.rs | |||
@@ -0,0 +1,48 @@ | |||
1 | use std::fs; | ||
2 | use std::path::Path; | ||
3 | |||
4 | /* AOC21 Day 6: https://adventofcode.com/2021/day/6 */ | ||
5 | fn main() { | ||
6 | let input = Path::new("resources").join("input.txt"); | ||
7 | let content = fs::read_to_string(input).expect("Unable to read input file"); | ||
8 | println!("Ex1: The number of lanternfishes is {}", evolution(parse_input(&content), 80)); | ||
9 | println!("Ex2: The number of lanternfishes is {}", evolution(parse_input(&content), 256)); | ||
10 | } | ||
11 | |||
12 | fn parse_input(s: &str) -> Vec<u64> { | ||
13 | let mut lfs = vec![0;9]; | ||
14 | s.split(",").for_each(|n| lfs[n.parse::<usize>().expect("Malformed input")] += 1); | ||
15 | lfs | ||
16 | } | ||
17 | |||
18 | fn evolution(mut lfs: Vec<u64>, days: usize) -> u64 { | ||
19 | (1..days).for_each(|i| lfs[(i+7)%9] += lfs[i%9]); | ||
20 | lfs.iter().sum() | ||
21 | } | ||
22 | |||
23 | #[cfg(test)] | ||
24 | mod tests { | ||
25 | use super::*; | ||
26 | |||
27 | const LANTERNFISHES: &str = "3,4,3,1,2"; | ||
28 | |||
29 | #[test] | ||
30 | fn input_parsing() { | ||
31 | assert_eq!(vec![0,1,1,2,1,0,0,0,0], parse_input("3,4,3,1,2")) | ||
32 | } | ||
33 | |||
34 | #[test] | ||
35 | fn evolution_18days() { | ||
36 | assert_eq!(26, evolution(parse_input(LANTERNFISHES), 18)) | ||
37 | } | ||
38 | |||
39 | #[test] | ||
40 | fn evolution_80days() { | ||
41 | assert_eq!(5934, evolution(parse_input(LANTERNFISHES), 80)) | ||
42 | } | ||
43 | |||
44 | #[test] | ||
45 | fn evolution_256days() { | ||
46 | assert_eq!(26984457539, evolution(parse_input(LANTERNFISHES), 256)) | ||
47 | } | ||
48 | } | ||