-
Notifications
You must be signed in to change notification settings - Fork 0
/
quest12.rs
52 lines (43 loc) · 1.09 KB
/
quest12.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
use crate::util::grid::*;
use crate::util::iter::*;
use crate::util::parse::*;
use crate::util::point::*;
pub fn part1(notes: &str) -> i32 {
targets(notes)
}
pub fn part2(notes: &str) -> i32 {
targets(notes)
}
pub fn part3(notes: &str) -> i32 {
notes.iter_signed::<i32>().chunk::<2>().map(|[x, y]| ranking(x / 2, y - x / 2 - x % 2)).sum()
}
pub fn targets(notes: &str) -> i32 {
let grid = Grid::parse(notes);
let mut total = 0;
for y in 0..grid.height {
for x in 0..grid.width {
total += match grid[Point::new(x, y)] {
b'T' => ranking(x - 1, grid.height - 2 - y),
b'H' => 2 * ranking(x - 1, grid.height - 2 - y),
_ => 0,
}
}
}
total
}
fn ranking(x: i32, y: i32) -> i32 {
for base in 0..3 {
let y = y - base;
let horizontal = x + y;
if x < y {
continue;
}
if x <= 2 * y {
return (base + 1) * y;
}
if horizontal % 3 == 0 {
return (base + 1) * (horizontal / 3);
}
}
unreachable!()
}