-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_16.ex
90 lines (79 loc) · 2.09 KB
/
day_16.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
defmodule AdventOfCode.Y2015.Day16 do
@moduledoc """
--- Day 16: Aunt Sue ---
Problem Link: https://adventofcode.com/2015/day/16
Difficulty: m
Tags: hard-description count
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2015, 16)
def run(input \\ input()) do
input = parse(input)
{runner(input, &guess_1/1), runner(input, &guess_2/1)}
end
def runner(parsed_input, guesstimator), do: guess(parsed_input, guesstimator)
def parse(data) do
data
|> Transformers.lines()
|> Map.new(fn fact ->
[aunt | possessions] = String.split(fact, ": ")
{get_sue_number(aunt), parse_possessions(possessions |> Enum.join(": "))}
end)
end
def parse_possessions(possessions) do
possessions
|> String.split(", ")
|> Map.new(fn possession ->
[item, quantity] = String.split(possession, ": ")
{item, String.to_integer(quantity)}
end)
end
defp get_sue_number(aunt) do
[_, number] = String.split(aunt, " ")
String.to_integer(number)
end
defp guess(aunts, predicate) do
aunts
|> Enum.map(fn {aunt, possessions} ->
points = predicate.(possessions)
{aunt, points}
end)
|> Enum.max_by(fn {_, points} -> points end)
|> elem(0)
end
@ticker_outputs %{
"children" => 3,
"cats" => 7,
"samoyeds" => 2,
"pomeranians" => 3,
"akitas" => 0,
"vizslas" => 0,
"goldfish" => 5,
"trees" => 3,
"cars" => 2,
"perfumes" => 1
}
defp guess_1(possessions) do
Enum.count(possessions, fn {item, count} -> @ticker_outputs[item] == count end)
end
@ticker_outputs %{
"children" => 3,
"cats" => {&Kernel.>/2, 7},
"samoyeds" => 2,
"pomeranians" => {&Kernel.</2, 3},
"akitas" => 0,
"vizslas" => 0,
"goldfish" => {&Kernel.</2, 5},
"trees" => {&Kernel.>/2, 3},
"cars" => 2,
"perfumes" => 1
}
defp guess_2(possessions) do
Enum.count(possessions, fn {item, count} ->
case @ticker_outputs[item] do
{fun, val} -> fun.(count, val)
val -> val == count
end
end)
end
end