-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc202208.ex
152 lines (123 loc) · 3.97 KB
/
aoc202208.ex
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
defmodule AOC2022.Day08 do
@moduledoc """
Advent of Code 2022, day 8: Treetop Tree House.
"""
require AOC
@directions [:up, :down, :left, :right]
@doc """
Parse input.
"""
def parse(puzzle_input) do
for {line, row} <- puzzle_input |> String.split("\n", trim: true) |> Enum.with_index(),
{height, col} <- line |> String.split("", trim: true) |> Enum.with_index(),
into: %{},
do: {{row, col}, height |> String.to_integer()}
end
@doc """
Solve part 1.
"""
def part1(heights) do
shape = {max_row(heights), max_col(heights)}
heights
|> Map.keys()
|> Enum.map(&visible?(&1, heights, shape))
|> Enum.count(& &1)
end
@doc """
Solve part 2.
"""
def part2(heights) do
shape = {max_row(heights), max_col(heights)}
heights
|> Map.keys()
|> Enum.map(&scenic_score(&1, heights, shape))
|> Enum.max()
end
@doc """
Check if a tree is visible from any direction.
## Example:
429
523
189
iex> heights = parse("429\\n523\\n189")
iex> visible?({1, 1}, heights, {2, 2})
false
iex> visible?({0, 2}, heights, {2, 2})
true
"""
def visible?(tree, heights, shape),
do: @directions |> Enum.any?(&is_seen?({tree, heights[tree]}, heights, shape, &1))
@doc """
Calculate the scenic score of one tree.
## Example:
iex> heights = parse("30373\\n25512\\n65332\\n33549\\n35390")
iex> scenic_score({1, 2}, heights, {4, 4})
4
iex> scenic_score({3, 2}, heights, {4, 4})
8
"""
def scenic_score(tree, heights, shape) do
@directions
|> Enum.map(&sees_trees({tree, heights[tree]}, heights, shape, &1))
|> Enum.product()
end
@doc """
Check if one tree is seen in the given direction.
## Example:
469
563
189
iex> heights = parse("469\\n563\\n189")
iex> is_seen?({{1, 1}, 6}, heights, {2, 2}, :right)
true
iex> is_seen?({{1, 1}, 6}, heights, {2, 2}, :down)
false
"""
def is_seen?({{row, col}, height}, heights, {rows, _}, :up),
do: (row + 1)..rows//1 |> Enum.all?(&(heights[{&1, col}] < height))
def is_seen?({{row, col}, height}, heights, _, :down),
do: 0..(row - 1)//1 |> Enum.all?(&(heights[{&1, col}] < height))
def is_seen?({{row, col}, height}, heights, {_, cols}, :left),
do: (col + 1)..cols//1 |> Enum.all?(&(heights[{row, &1}] < height))
def is_seen?({{row, col}, height}, heights, _, :right),
do: 0..(col - 1)//1 |> Enum.all?(&(heights[{row, &1}] < height))
@doc """
Count how many trees is seen from one tree in the given direction.
## Example:
469
563
189
iex> heights = parse("469\\n563\\n189")
iex> sees_trees({{0, 2}, 9}, heights, {3, 3}, :down)
2
iex> sees_trees({{2, 1}, 8}, heights, {3, 3}, :up)
2
iex> sees_trees({{0, 0}, 4}, heights, {3, 3}, :left)
0
"""
def sees_trees({{row, col}, height}, heights, _, :up),
do:
(row - 1)..0//-1
|> Enum.find_index(&(heights[{&1, col}] >= height))
|> then(&if is_nil(&1), do: row, else: &1 + 1)
def sees_trees({{row, col}, height}, heights, {rows, _}, :down),
do:
(row + 1)..rows//1
|> Enum.find_index(&(heights[{&1, col}] >= height))
|> then(&if is_nil(&1), do: rows - row, else: &1 + 1)
def sees_trees({{row, col}, height}, heights, _, :left),
do:
(col - 1)..0//-1
|> Enum.find_index(&(heights[{row, &1}] >= height))
|> then(&if is_nil(&1), do: col, else: &1 + 1)
def sees_trees({{row, col}, height}, heights, {_, cols}, :right),
do:
(col + 1)..cols//1
|> Enum.find_index(&(heights[{row, &1}] >= height))
|> then(&if is_nil(&1), do: cols - col, else: &1 + 1)
defp max_row(heights), do: heights |> Map.keys() |> Enum.map(&elem(&1, 0)) |> Enum.max()
defp max_col(heights), do: heights |> Map.keys() |> Enum.map(&elem(&1, 1)) |> Enum.max()
def main(args) do
Enum.map(args, fn path -> AOC.solve(path, &parse/1, &part1/1, &part2/1) end)
end
end