-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.ex
54 lines (46 loc) · 1.15 KB
/
day08.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
defmodule Y2015.Day08 do
use Advent.Day, no: 8
@doc """
iex> Day08.part1("test/y2015/input/day08.txt")
12
"""
def part1(filename) do
filename
|> parse_input
|> Enum.map(&raw_length/1)
|> Enum.sum()
end
def part2(filename) do
filename
|> parse_input
|> Enum.map(&cooked_length/1)
|> Enum.sum()
end
def parse_input(filename) do
Stream.resource(
fn -> File.open!(filename, [:binary]) end,
fn file -> parse_line(file) end,
fn file -> File.close(file) end
)
end
defp parse_line(file) do
case IO.binread(file, :line) do
:eof -> {:halt, file}
line -> {[String.trim(line)], file}
end
end
def raw_length(raw) do
{cooked, _} = Code.eval_string(raw)
String.length(raw) - String.length(cooked)
end
def cooked_length(raw) do
cooked =
raw
|> String.replace("\\", "\\\\")
|> String.replace("\"", "\\\"")
# 2 - extra quotes needed after the old ones were escaped
String.length(cooked) - String.length(raw) + 2
end
def part1_verify, do: part1("lib/y2015/input/day08.txt")
def part2_verify, do: part2("lib/y2015/input/day08.txt")
end