-
Notifications
You must be signed in to change notification settings - Fork 3
/
elixir_day05.ex
93 lines (72 loc) · 1.55 KB
/
elixir_day05.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
defmodule ElixirDay05 do
@moduledoc """
Documentation for ElixirDay05.
"""
@doc """
Hello world.
## Examples
iex> ElixirDay05.hello()
:world
"""
def hello do
:world
end
def main do
IO.puts("Small part1, expect 5: ")
parse("../input_small.txt")
|> part1
|> IO.puts()
IO.puts("Small part2, expect 10: ")
parse("../input_small.txt")
|> part2
|> IO.puts()
IO.puts("Large part1: ")
parse("../input.txt")
|> part1
|> IO.puts()
IO.puts("Large part2: ")
parse("../input.txt")
|> part2
|> IO.puts()
end
def part1(jumps) do
do_part1(jumps, 0, 0)
end
def do_part1(jumps, location, steps) do
value = Array.get(jumps, location)
if value == nil do
steps
else
jumps = Array.set(jumps, location, value + 1)
do_part1(jumps, location + value, steps + 1)
end
end
def part2(jumps) do
do_part2(jumps, 0, 0)
end
def do_part2(jumps, location, steps) do
value = Array.get(jumps, location)
if value == nil do
steps
else
new_val =
if value >= 3 do
value - 1
else
value + 1
end
jumps = Array.set(jumps, location, new_val)
do_part2(jumps, location + value, steps + 1)
end
end
def set(vector, index, new_value) do
Array.set(vector, index, new_value)
end
def parse(filename) do
File.stream!(filename)
|> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1)
|> Enum.map(fn x -> x end)
|> Array.from_list()
end
end