-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_22.ex
143 lines (122 loc) · 3.8 KB
/
day_22.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
defmodule AdventOfCode.Y2023.Day22 do
@moduledoc """
--- Day 22: Sand Slabs ---
Problem Link: https://adventofcode.com/2023/day/22
Difficulty: xl
Tags: stack range not-fast-enough
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2023, 22)
def run(input \\ input()) do
input
|> parse()
|> supports()
|> then(&{length(free(&1)), disintegrate(&1)})
end
def parse(data \\ input()) do
for {line, i} <- Enum.with_index(Transformers.lines(data)) do
line
|> String.split([",", "~"], trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(3)
|> Enum.zip_with(fn [a, b] -> Range.new(min(a, b), max(a, b)) end)
|> List.to_tuple()
|> then(fn {x, y, z} -> {to_alpha(i), x, y, z} end)
end
end
defp supports(data), do: data |> drop() |> support()
defp to_alpha(i), do: to_alpha(i, [])
defp to_alpha(i, acc) when i < 26, do: to_string([rem(i, 26) + 65 | acc])
defp to_alpha(i, acc), do: to_alpha(div(i, 26) - 1, [rem(i, 26) + 65 | acc])
defp drop(bricks),
do:
bricks
|> Enum.sort_by(fn {_, _, _, z1.._//_} -> z1 end)
|> drop([])
defp drop([], settled), do: settled
defp drop([{_, _, _, 1.._//_} = brick | bricks], settled),
do: drop(bricks, [brick | settled])
defp drop([{id, ax, ay, az} | bricks], settled) do
{_, _, _, _..bz2//_} =
Enum.filter(settled, fn {_, bx, by, _} ->
not (Range.disjoint?(ax, bx) or
Range.disjoint?(ay, by))
end)
|> Enum.max_by(
fn {_, _, _, _..bz2//_} -> bz2 end,
fn -> {nil, nil, nil, 0..0} end
)
az1 = bz2 + 1
az2 = bz2 + Range.size(az)
brick = {id, ax, ay, az1..az2}
drop(bricks, [brick | settled])
end
defp support(bricks), do: support(bricks, bricks, %{})
defp support([], _, supports), do: supports
defp support([{_, ax, ay, az1.._//_} = current | rest], bricks, supports) do
others =
Enum.filter(bricks, fn {_, bx, by, _..bz2//_} ->
bz2 == az1 - 1 and
not (Range.disjoint?(ax, bx) or
Range.disjoint?(ay, by))
end)
supports =
Map.update(
supports,
current,
%{is_above: others, is_below: []},
fn %{is_above: above} = existing ->
%{existing | is_above: others ++ above}
end
)
supports =
Enum.reduce(others, supports, fn other, supports ->
Map.update(
supports,
other,
%{is_above: [], is_below: [current]},
fn %{is_below: below} = existing ->
%{existing | is_below: [current | below]}
end
)
end)
support(rest, bricks, supports)
end
defp free(supports) do
supports
|> Enum.filter(fn {_, %{is_above: is_above}} -> length(is_above) == 1 end)
|> Enum.flat_map(fn {_, %{is_above: is_above}} -> is_above end)
|> MapSet.new()
|> then(fn required ->
supports
|> Map.keys()
|> MapSet.new()
|> MapSet.difference(required)
|> Enum.to_list()
end)
end
defp chain(brick, supports), do: chain([brick], supports, MapSet.new())
defp chain([], _, acc), do: acc
defp chain([brick | rest], supports, acc) do
destroyed =
supports[brick].is_below
|> Enum.filter(fn above_brick ->
below_bricks = supports[above_brick].is_above
length(below_bricks) == 1 or
Enum.all?(below_bricks, fn below -> MapSet.member?(acc, below) end)
end)
destroyed
|> MapSet.new()
|> MapSet.union(acc)
|> then(fn acc -> chain(rest ++ destroyed, supports, acc) end)
end
defp disintegrate(supports) do
for brick <- Map.keys(supports), reduce: 0 do
acc ->
brick
|> chain(supports)
|> MapSet.size()
|> Kernel.+(acc)
end
end
end