-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_11.ex
80 lines (68 loc) · 2.15 KB
/
day_11.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
defmodule AdventOfCode.Day11 do
def part1(args) do
args
|> parseInput()
|> then(&Enum.reduce(1..10000, &1, fn _, map -> playRound(map) end))
|> Map.values()
|> Enum.map(& &1.counter)
|> IO.inspect(charlists: :as_lists)
|> Enum.sort(:desc)
|> Enum.take(2)
|> Enum.product()
end
def part2(_args) do
end
def parseInput(args) do
re =
~r/Monkey (?<id>\d+):\n Starting items: (?<items>\d+(, \d+)*)\n Operation: new = old (?<op>. [\d\w]+)\n Test: divisible by (?<test>\d+)\n If true: throw to monkey (?<trMonk>\d+)\n If false: throw to monkey (?<faMonk>\d+)/
args
|> String.split("\n\n")
|> Enum.map(&Regex.named_captures(re, &1))
|> Enum.reduce(%{}, &reMapToMonkeyMap(&1, &2))
end
def reMapToMonkeyMap(reMap, targetMap) do
id = reMap["id"]
items = reMap["items"] |> String.split(", ") |> Enum.map(&String.to_integer/1)
faMonk = reMap["faMonk"]
trMonk = reMap["trMonk"]
test = String.to_integer(reMap["test"])
op =
reMap["op"]
|> String.split()
|> then(
&case &1 do
["*", "old"] -> fn x -> x * x end
["+", "old"] -> fn x -> x + x end
["*", num] -> fn x -> x * String.to_integer(num) end
["+", num] -> fn x -> x + String.to_integer(num) end
end
)
Map.put_new(targetMap, id, %{
items: items,
faMonk: faMonk,
trMonk: trMonk,
test: test,
op: op,
counter: 0
})
end
def processItem(map, item, id) do
worry = map[id].op.(item)
afterInspect = Integer.mod(worry, 9_699_690)
check = Integer.mod(afterInspect, map[id].test) == 0
target = if check, do: map[id].trMonk, else: map[id].faMonk
map
|> update_in([target, :items], fn xs -> xs ++ [afterInspect] end)
|> update_in([id, :items], fn xs -> tl(xs) end)
|> update_in([id, :counter], fn i -> i + 1 end)
end
def playRound(monkeyMap) do
monkeyMap
|> Map.keys()
|> Enum.reduce(monkeyMap, fn monkeyID, map -> playTurn(monkeyID, map) end)
end
def playTurn(id, map) do
map[id].items
|> Enum.reduce(map, fn item, accMap -> processItem(accMap, item, id) end)
end
end