-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent3.exs
72 lines (60 loc) · 2.54 KB
/
advent3.exs
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
defmodule Advent3 do
def generate(lastNum, next_element_fn),
do: generate({1, 0, 0}, %{{0, 0} => 1}, 0, 0, 0, 0, lastNum, "right", next_element_fn)
def generate({num, x, y}, _, _, _, _, _, lastNum, _, _) when num >= lastNum do
IO.inspect(abs(x) + abs(y))
IO.inspect(num)
end
def generate({num, x, y}, map, top, right, bottom, left, lastNum, "right", next_element_fn) do
nextValue = next_element_fn.({num, x + 1, y}, map)
nextElement = {nextValue, x + 1, y}
map = Map.put(map, {x + 1, y}, nextValue)
if x + 1 > right do
generate(nextElement, map, top, right + 1, bottom, left, lastNum, "up", next_element_fn)
else
generate(nextElement, map, top, right, bottom, left, lastNum, "right", next_element_fn)
end
end
def generate({num, x, y}, map, top, right, bottom, left, lastNum, "up", next_element_fn) do
nextValue = next_element_fn.({num, x, y + 1}, map)
nextElement = {nextValue, x, y + 1}
map = Map.put(map, {x, y + 1}, nextValue)
if y + 1 > top do
generate(nextElement, map, top + 1, right, bottom, left, lastNum, "left", next_element_fn)
else
generate(nextElement, map, top, right, bottom, left, lastNum, "up", next_element_fn)
end
end
def generate({num, x, y}, map, top, right, bottom, left, lastNum, "left", next_element_fn) do
nextValue = next_element_fn.({num, x - 1, y}, map)
nextElement = {nextValue, x - 1, y}
map = Map.put(map, {x - 1, y}, nextValue)
if x - 1 < left do
generate(nextElement, map, top, right, bottom, left - 1, lastNum, "down", next_element_fn)
else
generate(nextElement, map, top, right, bottom, left, lastNum, "left", next_element_fn)
end
end
def generate({num, x, y}, map, top, right, bottom, left, lastNum, "down", next_element_fn) do
nextValue = next_element_fn.({num, x, y - 1}, map)
nextElement = {nextValue, x, y - 1}
map = Map.put(map, {x, y - 1}, nextValue)
if y - 1 < bottom do
generate(nextElement, map, top, right, bottom - 1, left, lastNum, "right", next_element_fn)
else
generate(nextElement, map, top, right, bottom, left, lastNum, "down", next_element_fn)
end
end
def part1_next_element({num, _, _}, _), do: num + 1
def part2_next_element({num, x, y}, map) do
Enum.sum(
for x_offset <- -1..1,
y_offset <- -1..1,
{:ok, value} <- [Map.fetch(map, {x + x_offset, y + y_offset})] do
value
end
)
end
end
Advent3.generate(368_078, &Advent3.part1_next_element/2)
Advent3.generate(368_078, &Advent3.part2_next_element/2)