From c35022d1955d5f33a909133af31820b12d11c177 Mon Sep 17 00:00:00 2001 From: Andrew Coleman Date: Mon, 25 Dec 2017 16:19:04 -0500 Subject: [PATCH] day25 solution --- day25/Makefile | 8 ++++ day25/day25.erl | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 day25/Makefile create mode 100644 day25/day25.erl diff --git a/day25/Makefile b/day25/Makefile new file mode 100644 index 0000000..6548514 --- /dev/null +++ b/day25/Makefile @@ -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 diff --git a/day25/day25.erl b/day25/day25.erl new file mode 100644 index 0000000..87dca1f --- /dev/null +++ b/day25/day25.erl @@ -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())]).