-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2022_day08.santa
74 lines (63 loc) · 1.53 KB
/
aoc2022_day08.santa
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
input: read("aoc://2022/8")
let parse_grid = |input| {
zip(0.., lines(input))
|> flat_map(|[y, row]| {
zip(0.., row)
|> map(|[x, tree_height]| [[y, x], int(tree_height)]);
})
|> dict;
}
let grid_directions = |grid| {
let [ys, xs] = zip(..keys(grid));
let [ly, hy] = [min(ys), max(ys)];
let [lx, hx] = [min(xs), max(xs)];
|[y, x]| {
let not_self = _ != [y, x];
// up, down, left, right
[
max(y-1, ly)..=lx |> map(|dy| [dy, x]) |> filter(not_self),
min(y+1, hy)..=hy |> map(|dy| [dy, x]) |> filter(not_self),
max(x-1, lx)..=lx |> map(|dx| [y, dx]) |> filter(not_self),
min(x+1, hx)..=hx |> map(|dx| [y, dx]) |> filter(not_self)
];
};
}
part_one: {
let grid = parse_grid(input);
let directions = grid_directions(grid);
grid
|> count(|tree_height, position| {
directions(position)
|> any?(all?(|viewpoint| grid[viewpoint] < tree_height))
});
}
part_two: {
let grid = parse_grid(input);
let directions = grid_directions(grid);
let scenic_score = |tree_height, direction| {
direction |> fold(0) |score, viewpoint| {
if grid[viewpoint] < tree_height { score + 1 } else { break(score + 1) }
}
};
grid
|> map(|tree_height, position| {
directions(position) |> fold(1) |score, direction| {
score * scenic_score(tree_height, direction)
}
})
|> max;
}
test: {
input: "30373
25512
65332
33549
35390"
part_one: 21
part_two: 8
}
test: {
input: read("aoc://2022/8")
part_one: 1719
part_two: 590824
}