-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_05.ex
59 lines (44 loc) · 2.61 KB
/
day_05.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
defmodule Aoc.Y2021.Day05 do
@moduledoc """
Solved https://adventofcode.com/2021/day/5
"""
import Aoc.Helper.IO
import Aoc.Helper.Util
def solve_part1(data), do: data |> Enum.filter(fn [a, b, x, y] -> a == x or b == y end) |> count_overlaps()
def solve_part2(data), do: data |> count_overlaps()
defp count_overlaps(data),
do: data |> populate_grid(data |> get_max() |> gen_grid()) |> Map.values() |> Enum.count(&(&1 > 1))
defp populate_grid([], grid), do: grid
defp populate_grid([[x, x, y, y] | data], grid), do: populate_grid(data, update_grid(x, y, grid))
defp populate_grid([[x, y1, x, y2] | data], grid), do: populate_grid(data, update_grid({x, {y1, y2}}, grid))
defp populate_grid([[x1, y, x2, y] | data], grid), do: populate_grid(data, update_grid({{x1, x2}, y}, grid))
defp populate_grid([[x1, y1, x2, y2] | data], grid) when abs(x1 - x2) == abs(y1 - y2),
do: populate_grid(data, populate_diagonals(x1, y1, x2, y2, grid))
defp populate_diagonals(x, y, x, y, grid), do: bump_grid({x, y}, grid)
defp populate_diagonals(x1, y1, x2, y2, grid),
do: populate_diagonals(shift(x1, x2), shift(y1, y2), x2, y2, bump_grid({x1, y1}, grid))
defp shift(x1, x2) when x1 > x2, do: x1 - 1
defp shift(x1, _x2), do: x1 + 1
defp update_grid(x, y, grid) when x > y, do: update_grid(x, y + 1, bump_grid({y, y}, grid))
defp update_grid(x, y, grid) when x < y, do: update_grid(x + 1, y, bump_grid({x, x}, grid))
defp update_grid(x, x, grid), do: bump_grid({x, x}, grid)
defp update_grid({x, {y1, y2}}, grid) when y1 > y2, do: update_grid({x, {y1, y2 + 1}}, bump_grid({x, y2}, grid))
defp update_grid({x, {y1, y2}}, grid) when y1 < y2, do: update_grid({x, {y1 + 1, y2}}, bump_grid({x, y1}, grid))
defp update_grid({x, {y, y}}, grid), do: bump_grid({x, y}, grid)
defp update_grid({{x1, x2}, y}, grid) when x1 > x2, do: update_grid({{x1, x2 + 1}, y}, bump_grid({x2, y}, grid))
defp update_grid({{x1, x2}, y}, grid) when x1 < x2, do: update_grid({{x1 + 1, x2}, y}, bump_grid({x1, y}, grid))
defp update_grid({{x, x}, y}, grid), do: bump_grid({x, y}, grid)
defp bump_grid({x, y}, grid), do: Map.update(grid, {x, y}, 0, &(&1 + 1))
defp gen_grid({x, y}), do: Map.new(for i <- 0..x, j <- 0..y, do: {{i, j}, 0})
defp get_max(data),
do:
transpose(data)
|> then(fn [x1, y1, x2, y2] -> {Enum.max(List.flatten([x1, x2])), Enum.max(List.flatten([y1, y2]))} end)
def get_input() do
get_string_input("2021", "05")
|> String.split(["\n", " -> ", ",", " "], trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(4, 4, :discard)
end
def solved_status(), do: :solved
end