1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
use {
anyhow::{
anyhow,
Context,
Result,
},
std::{
env::args,
fs::File,
io::{
BufRead,
BufReader,
},
path::Path,
},
};
#[derive(Clone,Copy,Debug)]
struct LanternFish {
days: u8,
}
impl LanternFish {
pub fn new(days: u8) -> Self {
Self {
days,
}
}
fn step(&mut self) -> bool {
if self.days == 0 {
self.days = 6;
true
} else {
self.days -= 1;
false
}
}
}
impl Default for LanternFish {
fn default() -> Self {
Self::new(8)
}
}
struct Population {
count: Vec<usize>,
}
impl Population {
fn new<'a, I: IntoIterator<Item = &'a LanternFish>>(input: I) -> Self {
let mut count = vec![0; 9];
for fish in input {
count[fish.days as usize] += 1;
}
Self {
count,
}
}
fn step(&mut self) {
let iter = &mut self.count.iter_mut();
let spawn = iter.next().unwrap();
let mut new_count: Vec<_> = iter.map(|v| *v).collect();
new_count.push(0);
new_count[6] += *spawn;
new_count[8] = *spawn;
self.count = new_count;
}
fn sum(&self) -> usize {
self.count.iter().sum()
}
}
fn read_input<T: AsRef<Path>>(filename: T) -> Result<Vec<LanternFish>> {
let reader = BufReader::new(File::open(filename)?);
let line = reader.lines().next().ok_or(anyhow!("Could not read input"))??;
line.split(',').map(|v| {
let days = v.parse().map_err(|err| anyhow!("{}", err))?;
Ok(LanternFish::new(days))
}).collect()
}
fn part1<'a, I: IntoIterator<Item = &'a LanternFish>>(input: I) -> Result<usize> {
let mut population: Vec<_> = input.into_iter().copied().collect();
for _ in 0..80 {
let mut children = vec![];
for fish in &mut population {
if fish.step() {
children.push(LanternFish::default())
}
}
population.append(&mut children);
}
Ok(population.len())
}
fn part2<'a, I: IntoIterator<Item = &'a LanternFish>>(input: I) -> Result<usize> {
let mut population = Population::new(input);
for _ in 0..256 {
population.step();
}
Ok(population.sum())
}
fn main() -> Result<()> {
let ( do_part_1, do_part_2 ) = aoc::do_parts();
let filename = args().nth(1).ok_or(anyhow!("Missing input filename"))?;
let input = read_input(filename).context("Could not read input")?;
if do_part_1 {
let solution = part1(&input).context("No solution for part 1")?;
println!("Part1, solution found to be: {}", solution);
}
if do_part_2 {
let solution = part2(&input).context("No solution for part 2")?;
println!("Part2, solution found to be: {}", solution);
}
Ok(())
}
|