+1
2015/08/part1-tests/input1
+1
2015/08/part1-tests/input1
···
1
+
""
+1
2015/08/part1-tests/input2
+1
2015/08/part1-tests/input2
···
1
+
"abc"
+1
2015/08/part1-tests/input3
+1
2015/08/part1-tests/input3
···
1
+
"aaa\"aaa"
+1
2015/08/part1-tests/input4
+1
2015/08/part1-tests/input4
···
1
+
"\x27"
+1
2015/08/part2-tests/input1
+1
2015/08/part2-tests/input1
···
1
+
""
+1
2015/08/part2-tests/input2
+1
2015/08/part2-tests/input2
···
1
+
"abc"
+1
2015/08/part2-tests/input3
+1
2015/08/part2-tests/input3
···
1
+
"aaa\"aaa"
+1
2015/08/part2-tests/input4
+1
2015/08/part2-tests/input4
···
1
+
"\x27"
+51
2015/08/solution.escript
+51
2015/08/solution.escript
···
1
+
#!/usr/bin/env escript
2
+
-mode(compile).
3
+
4
+
% --- Part 1 Code
5
+
part1(Input) -> io:format("Part 1: ~b~n", [code_interp_diff(Input, 0)]).
6
+
7
+
interp_length([]) -> 0;
8
+
interp_length([$\\, $\\ | Rest]) -> 1 + interp_length(Rest);
9
+
interp_length([$\\, $" | Rest]) -> 1 + interp_length(Rest);
10
+
interp_length([$\\, $x, _, _ | Rest]) -> 1 + interp_length(Rest);
11
+
interp_length([_ | Rest]) -> 1 + interp_length(Rest).
12
+
13
+
code_interp_diff([], Acc) ->
14
+
Acc;
15
+
code_interp_diff([Line | Rest], Acc) ->
16
+
code_interp_diff(Rest, Acc + (string:length(Line) - (interp_length(Line) - 2))).
17
+
18
+
% --- Part 2 Code
19
+
part2(Input) -> io:format("Part 2: ~b~n", [escape_code_diff(Input, 0)]).
20
+
21
+
escape_length([]) -> 0;
22
+
escape_length([$\\ | Rest]) -> 2 + escape_length(Rest);
23
+
escape_length([$" | Rest]) -> 2 + escape_length(Rest);
24
+
escape_length([_Char | Rest]) -> 1 + escape_length(Rest).
25
+
26
+
escape_code_diff([], Acc) ->
27
+
Acc;
28
+
escape_code_diff([Line | Rest], Acc) ->
29
+
escape_code_diff(Rest, Acc + ((escape_length(Line) + 2) - string:length(Line))).
30
+
31
+
% --- Read Input
32
+
read_input() -> read_input("").
33
+
read_input(Prev) ->
34
+
case io:get_line("") of
35
+
eof -> Prev;
36
+
{error, _} -> halt(1);
37
+
Data when Prev == "" -> read_input([Data]);
38
+
Data -> read_input(Prev ++ [Data])
39
+
end.
40
+
41
+
main(Args) ->
42
+
Input = read_input(),
43
+
case Args of
44
+
["1"] ->
45
+
part1(Input);
46
+
["2"] ->
47
+
part2(Input);
48
+
_ ->
49
+
part1(Input),
50
+
part2(Input)
51
+
end.