-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.rs
176 lines (141 loc) · 3.77 KB
/
day18.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! [Day 18: Settlers of The North Pole](https://adventofcode.com/2018/day/18)
use std::collections::HashMap;
// use std::collections::{HashMap,HashSet};
use aoc::grid;
use aoc::grid::Grid;
const OPEN_ACRE: u8 = 0;
const TREE: u8 = 1;
const LUMBERYARD: u8 = 2;
type Area = Grid<u8>;
fn collect(area: &Area) -> Area {
let mut new_area = Grid::<u8>::with_size(area.size().0, area.size().1);
for (x, y, acre) in area.iter() {
let (trees, lumberyards) = {
let mut trees = 0;
let mut lumberyards = 0;
for c in area.iter_neighbors((x, y)) {
match area[c] {
TREE => trees += 1,
LUMBERYARD => lumberyards += 1,
_ => (),
}
}
(trees, lumberyards)
};
let mut acre = *acre;
match acre {
OPEN_ACRE => {
if trees >= 3 {
acre = TREE;
}
}
TREE => {
if lumberyards >= 3 {
acre = LUMBERYARD;
}
}
LUMBERYARD => {
if trees == 0 || lumberyards == 0 {
acre = OPEN_ACRE;
}
}
_ => (),
}
new_area[(x, y)] = acre;
}
new_area
}
fn value(area: &Area) -> u32 {
let mut trees = 0;
let mut lumberyards = 0;
for (_, _, c) in area.iter() {
match *c {
TREE => trees += 1,
LUMBERYARD => lumberyards += 1,
_ => (),
}
}
trees * lumberyards
}
fn hashable(area: &Area) -> Vec<u8> {
let mut s = Vec::new();
for (_, _, &c) in area.iter() {
s.push(c);
}
s
}
struct Puzzle {
area: Area,
n: usize,
}
impl Puzzle {
fn new() -> Puzzle {
Puzzle {
area: grid![],
n: 0,
}
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
self.n = 0;
for (y, line) in data.lines().enumerate() {
if self.n == 0 {
self.n = line.len();
self.area.resize(self.n, self.n);
}
for (x, acre) in line.chars().enumerate() {
self.area[(x, y)] = match acre {
'|' => TREE,
'#' => LUMBERYARD,
_ => OPEN_ACRE,
};
}
}
}
/// Solve part one.
fn part1(&self) -> u32 {
let mut area = self.area.clone();
for _ in 0..10 {
area = collect(&area);
}
value(&area)
}
/// Solve part two.
fn part2(&self) -> u32 {
let mut area = self.area.clone();
let mut values = vec![];
let mut seen = HashMap::new();
for i in 0.. {
values.push(value(&area));
if seen.contains_key(&hashable(&area)) {
let cycle_start = seen.get(&hashable(&area)).unwrap();
let cycle_end = i;
let n = 1_000_000_000;
let cycle = cycle_end - cycle_start;
return values[cycle_start + (n - cycle_end) % cycle as usize];
}
seen.insert(hashable(&area), i);
area = collect(&area);
}
0
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(args.path.as_str());
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part1(), 1147);
}
}