-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday18.ex
103 lines (87 loc) · 2.19 KB
/
day18.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
91
92
93
94
95
96
97
98
99
100
101
102
103
defmodule Elixir2016.Day18 do
use Bitwise
def parse(filename) do
File.read!(filename)
|> String.trim()
end
def part1(filename) do
parse(filename)
|> String.graphemes()
|> digitify()
|> part1_count(40)
end
def part2(filename) do
parse(filename)
|> String.graphemes()
|> digitify()
|> part1_count(400_000)
end
def part1_count(initial_row, num_rows), do: do_part1_count(initial_row, num_rows, 0)
def do_part1_count(_initial_row, 0, n), do: n
def do_part1_count(initial_row, num_rows, n) do
next_row = next_row(initial_row)
this_row_count =
initial_row
|> Enum.filter(fn x -> x == 0 end)
|> length()
do_part1_count(next_row, num_rows - 1, n + this_row_count)
end
@doc """
iex(2)> Day18.digitify([ ".", ".", "^", "^" ])
[0, 0, 1, 1]
"""
def digitify(list) do
list
|> Enum.map(fn x ->
case x do
"^" -> 1
"." -> 0
_ -> raise "Invalid input"
end
end)
end
@doc """
iex(2)> Day18.next_row([ 0, 0, 1, 1, 0 ])
[0, 1, 1, 1, 1]
"""
def next_row(list) do
list
|> extended_three_window()
|> Enum.map(fn [left, _mid, right] ->
left ^^^ right
end)
end
@doc """
window: Moves a sliding window over a list
iex(2)> ["a", "b", "c", "d", "e"] |> Day18.window(2)
[["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"]]
iex(3)> ["a", "b", "c", "d", "e"] |> Day18.window(3)
[["a", "b", "c"], ["b", "c", "d"], ["c", "d", "e"]]
"""
def window([_x | xs] = list, size) do
if length(list) < size do
[]
else
[Enum.take(list, size)] ++ window(xs, size)
end
end
def window(_, _), do: []
@doc """
Extended three window:
Adds "." and "." to the beginning/end of a list, then calls window 3 on that new list.
Gets you a list of 3 for each item in the original list,
with the original item being in the center of the three.
iex(10)> ["aa", "bb", "cc", "dd", "ee"] |> Day18.extended_three_window()
[
[".", "aa", "bb"],
["aa", "bb", "cc"],
["bb", "cc", "dd"],
["cc", "dd", "ee"],
["dd", "ee", "."]
]
"""
def extended_three_window(list) do
([0 | list] ++ [0])
|> window(3)
end
end