Skip to content

Commit afc31fa

Browse files
committed
day14
1 parent a94f3d6 commit afc31fa

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

14/sample_input

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
O....#....
2+
O.OO#....#
3+
.....##...
4+
OO.#O....O
5+
.O.....O#.
6+
O.#..O.#.#
7+
..O..#O..O
8+
.......O..
9+
#....###..
10+
#OO..#....

14/sol.exs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
defmodule Day14 do
2+
def read_input(file_path) do
3+
case File.read(file_path) do
4+
{:ok, content} ->
5+
content
6+
|> String.split("\n", trim: true)
7+
|> Enum.map(&String.graphemes/1)
8+
9+
{:error, reason} ->
10+
raise "Oh no! #{reason}"
11+
end
12+
end
13+
14+
def partA(file_path) do
15+
file_path
16+
|> read_input()
17+
|> tilt_and_rotate()
18+
|> calc_all_loads()
19+
end
20+
21+
def partB(file_path) do
22+
file_path
23+
|> read_input()
24+
|> tilt_n_cycle(1_000_000_000)
25+
|> rotate()
26+
|> calc_all_loads()
27+
end
28+
29+
def tilt_insert(x, []), do: [x]
30+
def tilt_insert("O", ["." | t]), do: ["." | tilt_insert("O", t)]
31+
def tilt_insert("O", ["#" | t]), do: ["O" | ["#" | t]]
32+
def tilt_insert(x, t), do: [x | t]
33+
34+
def tilt(l) do
35+
Enum.reduce(l, [], fn x, acc -> tilt_insert(x, acc) end)
36+
end
37+
38+
def tilt_and_rotate(ll), do: tilt_and_rotate(ll, [])
39+
def tilt_and_rotate([[] | _], acc), do: acc
40+
41+
def tilt_and_rotate(l, acc) do
42+
heads = l |> Enum.map(&hd/1)
43+
tails = l |> Enum.map(&tl/1)
44+
tilt_and_rotate(tails, acc ++ [tilt(heads)])
45+
end
46+
47+
def rotate(l), do: rotate(l, [])
48+
49+
def rotate([[] | _], acc), do: acc
50+
51+
def rotate(l, acc) do
52+
heads = l |> Enum.map(&hd/1)
53+
tails = l |> Enum.map(&tl/1)
54+
rotate(tails, acc ++ [Enum.reverse(heads)])
55+
end
56+
57+
def calc_load([], _, acc), do: acc
58+
def calc_load(["O" | t], i, acc), do: calc_load(t, i + 1, acc + i)
59+
def calc_load([_ | t], i, acc), do: calc_load(t, i + 1, acc)
60+
def calc_load(l), do: calc_load(l, 1, 0)
61+
def calc_all_loads(ll), do: Enum.map(ll, &calc_load/1) |> Enum.sum()
62+
63+
def tilt_1_cycle(ll) do
64+
ll |> tilt_and_rotate() |> tilt_and_rotate() |> tilt_and_rotate() |> tilt_and_rotate()
65+
end
66+
67+
def tilt_n_cycle(ll, n), do: tilt_n_cycle(ll, n, %{n: n})
68+
def tilt_n_cycle(ll, 0, _), do: ll
69+
70+
def tilt_n_cycle(ll, m, memo) do
71+
new_m =
72+
case is_map_key(memo, ll) do
73+
true -> Integer.mod(m, memo[ll] - m) - 1
74+
false -> m - 1
75+
end
76+
77+
tilt_n_cycle(ll |> tilt_1_cycle(), new_m, Map.put(memo, ll, m))
78+
end
79+
end
80+
81+
IO.puts(Day14.partA("./input"))
82+
IO.puts(Day14.partB("./input"))

0 commit comments

Comments
 (0)