-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_06.rs
61 lines (48 loc) · 1.37 KB
/
day_06.rs
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
use common::{solution, Answer};
use std::hash::Hash;
use hashbrown::HashMap;
solution!("Lanternfish", 6);
fn part_a(input: &str) -> Answer {
let data = Fish::parse_inp(input);
Fish::sim(data, 80).into()
}
fn part_b(input: &str) -> Answer {
let data = Fish::parse_inp(input);
Fish::sim(data, 256).into()
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
struct Fish {
timer: u32,
}
impl Fish {
fn new(timer: u32) -> Fish {
Fish { timer }
}
fn parse_inp(inp: &str) -> Vec<Fish> {
inp.lines()
.next()
.unwrap()
.split(',')
.map(|x| Fish::new(x.parse().unwrap()))
.collect()
}
fn sim(fish_vec: Vec<Fish>, days: u32) -> usize {
let mut fish = HashMap::new();
for i in fish_vec {
*fish.entry(i).or_insert(0) += 1;
}
for _ in 0..days {
let mut new_fish: HashMap<Fish, usize> = HashMap::new();
for i in &fish {
if i.0.timer == 0 {
*new_fish.entry(Fish::new(6)).or_insert(0) += *i.1;
*new_fish.entry(Fish::new(8)).or_insert(0) += *i.1;
continue;
}
*new_fish.entry(Fish::new(i.0.timer - 1)).or_insert(0) += *i.1;
}
fish = new_fish;
}
fish.values().sum()
}
}