1
0
Fork 0

day25 solution

main
Andrew Coleman 2017-12-25 16:19:04 -05:00
parent a31b1a4142
commit c35022d195
2 changed files with 111 additions and 0 deletions

8
day25/Makefile Normal file
View File

@ -0,0 +1,8 @@
all: day25.beam
erl -noshell -s day25 start -s init stop
day25.beam:
erlc day25.erl
clean:
rm -f day25.beam

103
day25/day25.erl Normal file
View File

@ -0,0 +1,103 @@
-module(day25).
-export([start/0]).
part_1()
-> state_a(maps:new(), 0, 0, 6).
state_a(Tape, _Index, _CurrentBit, Steps)
when Steps == 0 -> Tape;
state_a(Tape, Index, CurrentBit, Steps)
when CurrentBit == 0 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index + 1,
state_b(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1);
state_a(Tape, Index, CurrentBit, Steps)
when CurrentBit == 1 -> NewTape = maps:put(Index, 0, Tape),
NewIndex = Index - 1,
state_b(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1).
state_b(Tape, _Index, _CurrentBit, Steps)
when Steps == 0 -> Tape;
state_b(Tape, Index, CurrentBit, Steps)
when CurrentBit == 0 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index - 1,
state_a(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1);
state_b(Tape, Index, CurrentBit, Steps)
when CurrentBit == 1 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index + 1,
state_a(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1).
diagnostic_checksum(Tape)
-> maps:fold(fun(_K, V, Sum) -> Sum + V end, 0, Tape).
part_2()
-> state_a2(maps:new(), 0, 0, 12861455).
state_a2(Tape, _Index, _CurrentBit, Steps)
when Steps == 0 -> Tape;
state_a2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 0 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index + 1,
state_b2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1);
state_a2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 1 -> NewTape = maps:put(Index, 0, Tape),
NewIndex = Index - 1,
state_b2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1).
state_b2(Tape, _Index, _CurrentBit, Steps)
when Steps == 0 -> Tape;
state_b2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 0 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index - 1,
state_c2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1);
state_b2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 1 -> NewTape = maps:put(Index, 0, Tape),
NewIndex = Index + 1,
state_e2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1).
state_c2(Tape, _Index, _CurrentBit, Steps)
when Steps == 0 -> Tape;
state_c2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 0 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index + 1,
state_e2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1);
state_c2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 1 -> NewTape = maps:put(Index, 0, Tape),
NewIndex = Index - 1,
state_d2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1).
state_d2(Tape, _Index, _CurrentBit, Steps)
when Steps == 0 -> Tape;
state_d2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 0 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index - 1,
state_a2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1);
state_d2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 1 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index - 1,
state_a2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1).
state_e2(Tape, _Index, _CurrentBit, Steps)
when Steps == 0 -> Tape;
state_e2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 0 -> NewTape = maps:put(Index, 0, Tape),
NewIndex = Index + 1,
state_a2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1);
state_e2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 1 -> NewTape = maps:put(Index, 0, Tape),
NewIndex = Index + 1,
state_f2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1).
state_f2(Tape, _Index, _CurrentBit, Steps)
when Steps == 0 -> Tape;
state_f2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 0 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index + 1,
state_e2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1);
state_f2(Tape, Index, CurrentBit, Steps)
when CurrentBit == 1 -> NewTape = maps:put(Index, 1, Tape),
NewIndex = Index + 1,
state_a2(NewTape, NewIndex, maps:get(NewIndex, NewTape, 0), Steps - 1).
start()
-> io:fwrite("sample diagnostic checksum ~p~n", [diagnostic_checksum(part_1())]),
io:fwrite("problem diagnostic checksum ~p~n", [diagnostic_checksum(part_2())]).