-
Notifications
You must be signed in to change notification settings - Fork 0
/
quest08.rs
55 lines (44 loc) · 1.18 KB
/
quest08.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
use crate::util::math::*;
use crate::util::parse::*;
pub fn part1(notes: &str) -> u64 {
let blocks: u64 = notes.unsigned();
let height = blocks.sqrt() + 1;
let width = 2 * height - 1;
let missing = height * height - blocks;
width * missing
}
pub fn part2(notes: &str) -> u64 {
let supply = 20240000;
let priests: u64 = notes.unsigned();
let mut width = 1;
let mut layer = 1;
let mut blocks = 1;
while blocks < supply {
width += 2;
layer = (layer * priests) % 1111;
blocks += width * layer;
}
width * (blocks - supply)
}
pub fn part3(notes: &str) -> u64 {
let supply = 202400000;
let priests: u64 = notes.unsigned();
let mut width = 1;
let mut layer = 1;
let mut blocks = 1;
let mut height = 1;
let mut layers = Vec::new();
while blocks < supply {
layers.push(layer);
width += 2;
layer = (layer * priests) % 10 + 10;
blocks += width * layer;
height += layer;
}
blocks += (priests * width * height) % 10;
for layer in layers {
blocks -= 2 * ((priests * width * height) % 10);
height -= layer;
}
blocks - supply
}