-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_07.ex
54 lines (48 loc) · 1.27 KB
/
day_07.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
defmodule AdventOfCode.Y2017.Day07 do
@moduledoc """
--- Day 7: Recursive Circus ---
Problem Link: https://adventofcode.com/2017/day/7
Difficulty: m
Tags: tree
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2017, 7)
def run(input \\ input()) do
input = parse(input)
{run_1(input), run_2(input)}
end
defp run_1(input) do
input
|> Enum.map(fn %{name: name} -> name end)
|> MapSet.new()
|> MapSet.difference(get_branches(input))
|> MapSet.to_list()
|> hd()
end
defp run_2(_input) do
{:todo, 2}
end
@regex ~r/(?<name>[a-z]+) \((?<weight>\d+)\)( -> (?<branches>(\s*[a-z]+\,?)+))?/
def parse(data \\ input()) do
data
|> Transformers.lines()
|> Enum.map(fn line ->
Regex.named_captures(@regex, line)
end)
|> Enum.map(fn %{"branches" => branches, "weight" => weight, "name" => name} ->
%{
branches: (String.trim(branches) != "" && String.split(branches, ", ")) || nil,
name: name,
weight: String.to_integer(weight)
}
end)
end
defp get_branches(parsed_data) do
parsed_data
|> Enum.flat_map(fn
%{branches: nil} -> []
%{branches: branches} -> branches
end)
|> MapSet.new()
end
end