day 9 solution
parent
61aa26cad8
commit
bbb8298cca
|
@ -0,0 +1,8 @@
|
||||||
|
all: day9.beam
|
||||||
|
erl -noshell -s day9 start -s init stop
|
||||||
|
|
||||||
|
day9.beam:
|
||||||
|
erlc day9.erl
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f day9.beam
|
|
@ -0,0 +1,37 @@
|
||||||
|
-module(day9).
|
||||||
|
-export([start/0]).
|
||||||
|
|
||||||
|
calc_group_score(String)
|
||||||
|
-> calc_group_score(re:split(String, ""), 0, false, 0, false, 0).
|
||||||
|
calc_group_score([<<>>], Score, _LastCharWasEx, _Depth, _FoundGarbage, CharCount)
|
||||||
|
-> {Score, CharCount};
|
||||||
|
%calc_group_score(List, Score, LastCharWasEx, Depth, FoundGarbage, CharCount)
|
||||||
|
% when Score < 0 -> calc_group_score(List, 0, LastCharWasEx, Depth, FoundGarbage, CharCount);
|
||||||
|
calc_group_score([_Head|Tail], Score, LastCharWasEx, Depth, FoundGarbage, CharCount)
|
||||||
|
when LastCharWasEx == true, FoundGarbage == true -> calc_group_score(Tail, Score, false, Depth, true, CharCount);
|
||||||
|
calc_group_score([Head|Tail], Score, _LastCharWasEx, Depth, FoundGarbage, CharCount)
|
||||||
|
when Head == <<"!">> -> calc_group_score(Tail, Score, true, Depth, FoundGarbage, CharCount);
|
||||||
|
calc_group_score([Head|Tail], Score, _LastCharWasEx, Depth, FoundGarbage, CharCount)
|
||||||
|
when Head == <<"{">>, FoundGarbage == false -> calc_group_score(Tail, Score, false, Depth + 1, false, CharCount);
|
||||||
|
calc_group_score([Head|Tail], Score, _LastCharWasEx, Depth, FoundGarbage, CharCount)
|
||||||
|
when Head == <<"}">>, FoundGarbage == false -> calc_group_score(Tail, Score + Depth, false, Depth - 1, false, CharCount);
|
||||||
|
calc_group_score([Head|Tail], Score, _LastCharWasEx, Depth, FoundGarbage, CharCount)
|
||||||
|
when Head == <<"<">>, FoundGarbage == false -> calc_group_score(Tail, Score, false, Depth, true, CharCount);
|
||||||
|
calc_group_score([Head|Tail], Score, _LastCharWasEx, Depth, _FoundGarbage, CharCount)
|
||||||
|
when Head == <<">">> -> calc_group_score(Tail, Score, false, Depth, false, CharCount);
|
||||||
|
calc_group_score([_Head|Tail], Score, _LastCharWasEx, Depth, FoundGarbage, CharCount)
|
||||||
|
when FoundGarbage == true -> calc_group_score(Tail, Score, false, Depth, FoundGarbage, CharCount + 1);
|
||||||
|
calc_group_score([_Head|Tail], Score, _LastCharWasEx, Depth, FoundGarbage, CharCount)
|
||||||
|
-> calc_group_score(Tail, Score, false, Depth, FoundGarbage, CharCount).
|
||||||
|
|
||||||
|
start()
|
||||||
|
-> io:fwrite("~p~n", [calc_group_score("{}")]),
|
||||||
|
io:fwrite("~p~n", [calc_group_score("{{{}}}")]),
|
||||||
|
io:fwrite("~p~n", [calc_group_score("{{},{}}")]),
|
||||||
|
io:fwrite("~p~n", [calc_group_score("{{{},{},{{}}}}")]),
|
||||||
|
io:fwrite("~p~n", [calc_group_score("{<a>,<a>,<a>,<a>}")]),
|
||||||
|
io:fwrite("~p~n", [calc_group_score("{{<ab>},{<ab>},{<ab>},{<ab>}}")]),
|
||||||
|
io:fwrite("~p~n", [calc_group_score("{{<!!>},{<!!>},{<!!>},{<!!>}}")]),
|
||||||
|
io:fwrite("~p~n", [calc_group_score("{{<a!>},{<a!>},{<a!>},{<ab>}}")]),
|
||||||
|
{ok, File} = file:read_file("input"),
|
||||||
|
io:fwrite("~p~n", [calc_group_score(unicode:characters_to_list(File))]).
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue