-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_13.ex
64 lines (54 loc) · 1.5 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
60
61
62
63
64
defmodule AdventOfCode.Y2017.Day13 do
@moduledoc """
--- Day 13: Packet Scanners ---
Problem Link: https://adventofcode.com/2017/day/13
Difficulty: s
Tags: sequence slow
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2017, 13)
def run(input \\ input()) do
input = parse(input)
limit = Enum.max(Map.keys(input))
task_1 = Task.async(fn -> run_1(input, limit) end)
task_2 = Task.async(fn -> run_2(input, limit) end)
{Task.await(task_1, :infinity), Task.await(task_2, :infinity)}
end
def parse(data) do
data
|> Transformers.lines()
|> Map.new(fn line ->
line
|> String.split(": ")
|> Enum.map(&String.to_integer/1)
|> List.to_tuple()
end)
end
defp run_1(input, limit, delay \\ 0) do
input
|> sneak(limit, delay)
|> severity(input)
end
defp run_2(input, limit, delay \\ 0) do
case sneak(input, limit, delay) do
[] -> delay
_ -> run_2(input, limit, delay + 1)
end
end
defp sneak(input, limit, delay) do
Enum.reduce(0..limit, [], fn pos, detections ->
case Map.get(input, pos) do
nil -> detections
range -> (detect?(pos, range, delay) && [pos | detections]) || detections
end
end)
end
defp detect?(pico_second, range, delay) do
rem(pico_second + delay, 2 * (range - 1)) == 0
end
defp severity(detections, input) do
input
|> Map.take(detections)
|> Enum.sum_by(fn {k, v} -> k * v end)
end
end