-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_13.ex
59 lines (50 loc) · 1.66 KB
/
day_13.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 AdventOfCode.Y2022.Day13 do
@moduledoc """
--- Day 13: Distress Signal ---
Problem Link: https://adventofcode.com/2022/day/13
Difficulty: s
Tags: json sequence
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2022, 13)
def run(input \\ input()), do: {run_1(input), run_2(input)}
def run_1(input) do
input
|> String.split(~r{(\r\n\r\n|\r\r|\n\n)})
|> Enum.map(fn pair ->
[a, b] = Transformers.lines(pair)
{JSON.decode!(a), JSON.decode!(b)}
end)
|> Enum.with_index(1)
|> Enum.filter(fn {{x, y}, _} -> order(x, y) end)
|> Enum.sum_by(&elem(&1, 1))
end
def run_2(input) do
input
|> String.split(~r{(\r\n\r\n|\r\r|\n\n)})
|> Enum.flat_map(&Transformers.lines(&1))
|> Enum.map(&JSON.decode!/1)
|> Kernel.++([[[2]], [[6]]])
|> Enum.sort_by(&Function.identity/1, &order/2)
|> Enum.with_index(1)
|> Enum.filter(fn {elem, _} -> elem in [[[2]], [[6]]] end)
|> Enum.product_by(&elem(&1, 1))
end
def order([], []), do: nil
def order([_ | _], []), do: false
def order([], [_ | _]), do: true
def order([x | x_rest], [y | y_rest]) when is_integer(x) and is_integer(y),
do: (x == y && order(x_rest, y_rest)) || x < y
def order([x | x_rest], [y | y_rest]) when is_list(x) and is_list(y) do
case order(x, y) do
nil -> order(x_rest, y_rest)
val -> val
end
end
def order([x | x_rest], [y | y_rest]) when is_list(x) and is_integer(y) do
order([x | x_rest], [[y] | y_rest])
end
def order([x | x_rest], [y | y_rest]) when is_integer(x) and is_list(y) do
order([[x] | x_rest], [y | y_rest])
end
end