-
Notifications
You must be signed in to change notification settings - Fork 3
/
day01.ex
46 lines (39 loc) · 797 Bytes
/
day01.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
defmodule Elixir2015.Day01 do
def parse() do
File.read!("../inputs/01/input.txt")
|> String.graphemes()
end
def part1() do
parse()
|> Enum.reduce(0, fn char, count ->
cond do
char == "(" ->
count + 1
char == ")" ->
count - 1
true ->
raise "Unknown character"
end
end)
end
def part2() do
parse()
|> Enum.with_index(1)
|> Enum.reduce_while(0, fn {char, floor}, count ->
new_count =
cond do
char == "(" ->
count + 1
char == ")" ->
count - 1
true ->
raise "Unknown character"
end
if new_count == -1 do
{:halt, floor}
else
{:cont, new_count}
end
end)
end
end