-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_04.ex
150 lines (131 loc) · 3.85 KB
/
day_04.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
defmodule AdventOfCode.Y2018.Day04 do
@moduledoc """
--- Day 4: Repose Record ---
Problem Link: https://adventofcode.com/2018/day/4
Difficulty: s
Tags: date-time sequence sort map
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2018, 4)
def run(input \\ input()) do
input = parse(input)
{run_1(input), run_2(input)}
end
def run_1(input) do
store = sleep_time(input)
most_sleeper = Enum.max_by(store, & &1[:logs][:duration])[:id]
most_minute =
store
|> Enum.group_by(& &1[:id])
|> Map.get(most_sleeper)
|> hd()
|> get_in([:logs, :minutes])
|> get_mode()
|> elem(0)
most_minute * most_sleeper
end
def run_2(input) do
input
|> sleep_time()
|> Enum.map(fn %{id: id, logs: %{minutes: m}} -> %{id: id, minutes: get_mode(m)} end)
|> Enum.max_by(&elem(&1[:minutes], 1))
|> then(fn %{id: id, minutes: {minute, _}} -> id * minute end)
end
def parse(data) do
data
|> Transformers.lines()
|> Enum.map(&parse_line/1)
|> Enum.sort(fn a, b -> NaiveDateTime.diff(a[:timestamp], b[:timestamp]) < 0 end)
|> normalize()
end
@regex ~r"""
^\[(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})\s
(?<hour>\d{2}):(?<minute>\d{2})\]
\s(?<log>.+)$
"""x
defp parse_line(line) do
@regex
|> Regex.named_captures(line)
|> sanitize()
end
defp sanitize(%{
"year" => year,
"day" => day,
"month" => month,
"hour" => hour,
"minute" => minute,
"log" => log
}) do
%{
timestamp: to_datetime(year, month, day, hour, minute),
day: String.to_integer(day),
month: String.to_integer(month),
hour: String.to_integer(hour),
minute: String.to_integer(minute),
log: parse_log(log)
}
end
defp to_datetime(year, month, day, hour, minute) do
{:ok, result} = NaiveDateTime.from_iso8601("#{year}-#{month}-#{day} #{hour}:#{minute}:00Z")
result
end
defp xid(log), do: String.split(log, " ") |> hd()
defp parse_log("wakes up"), do: {:wakes}
defp parse_log("falls asleep"), do: {:sleeps}
defp parse_log("Guard #" <> rst), do: {:begins, xid(rst)}
defp normalize(dataset) do
dataset
|> Enum.reduce(%{id: nil, data: []}, fn log, %{id: id, data: data} = acc ->
case log do
%{log: {:begins, current_id}} -> %{acc | id: current_id, data: [log | data]}
%{log: {action}} -> %{acc | data: [%{log | log: {action, id}} | data]}
end
end)
|> Map.get(:data)
|> Enum.reverse()
|> Enum.map(fn %{log: {action, id}} = log ->
log
|> Map.merge(%{action: action, id: id})
|> Map.delete(:log)
end)
end
def duration(start, stop) do
stop
|> NaiveDateTime.add(-60, :second)
|> NaiveDateTime.diff(start)
end
def minutes(t1, t2), do: Enum.to_list(t1.minute..(t2.minute - 1))
defp sleep_time(data) do
data
|> Enum.reject(fn %{action: action} -> action == :begins end)
|> Enum.map(&Map.take(&1, [:id, :timestamp]))
|> Enum.chunk_every(2)
|> Enum.map(fn [sleeps, wakes] ->
%{
id: String.to_integer(sleeps[:id]),
duration: duration(sleeps[:timestamp], wakes[:timestamp]),
minutes: minutes(sleeps[:timestamp], wakes[:timestamp])
}
end)
|> Enum.group_by(& &1[:id])
|> Enum.map(fn {id, logs} ->
logs =
logs
|> Enum.reduce(
%{duration: 0, minutes: []},
fn log, %{duration: duration, minutes: minutes} = acc ->
%{acc | duration: duration + log[:duration], minutes: minutes ++ log[:minutes]}
end
)
%{id: id, logs: logs}
end)
end
defp get_mode(lst) do
mode_line =
lst
|> Enum.sort()
|> Enum.chunk_by(& &1)
|> Enum.max_by(&length(&1))
{hd(mode_line), length(mode_line)}
end
end