1
0
Fork 0

day 6 solution

main
Andrew Coleman 2017-12-06 00:52:36 -05:00
parent 037e8b2099
commit 40f7ca289d
2 changed files with 47 additions and 0 deletions

8
day6/Makefile Normal file
View File

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

39
day6/day6.erl Normal file
View File

@ -0,0 +1,39 @@
-module(day6).
-export([start/0]).
index_of(Item, List) -> index_of(Item, List, 1).
index_of(_, [], _) -> not_found;
index_of(Item, [Item|_], Index) -> Index;
index_of(Item, [_|Tl], Index) -> index_of(Item, Tl, Index+1).
redistribute_banks(BankConfiguration)
-> Max = lists:max(BankConfiguration),
Index = index_of(Max, BankConfiguration) - 1,
%io:fwrite("max ~B index ~B~n", [Max, Index]),
RawArray = array:from_list(BankConfiguration),
redistribute_banks(array:set(Index, 0, RawArray), Index, Max, length(BankConfiguration)).
redistribute_banks(Array, _Index, RemainingBanks, _MaxSize)
when RemainingBanks == 0 -> array:to_list(Array);
redistribute_banks(Array, Index, RemainingBanks, MaxSize)
-> Target = (Index + 1) rem MaxSize,
OldBankValue = array:get(Target, Array),
%io:fwrite("target ~B oldbankvalue ~B~n", [Target, OldBankValue]),
redistribute_banks(array:set(Target, OldBankValue + 1, Array), Target, RemainingBanks - 1, MaxSize).
steps_until_repeated(Input)
-> steps_until_repeated(Input, [], []).
steps_until_repeated(Input, Visited, UniqList)
when length(Visited) > 0, length(UniqList) /= length(Visited) -> [_|PriorVisits] = Visited,
InputIndex = index_of(Input, PriorVisits),
VisitedLength = length(Visited),
{VisitedLength,Input,InputIndex};
steps_until_repeated(Input, Visited, _UniqList)
-> NewConfiguration = redistribute_banks(Input),
NewVisited = [NewConfiguration|Visited],
steps_until_repeated(NewConfiguration, NewVisited, lists:usort(NewVisited)).
start()
-> io:fwrite("~p~n", [steps_until_repeated([0,2,7,0])]),
io:fwrite("~p~n", [steps_until_repeated([4,1,15,12,0,9,9,5,5,8,7,3,14,5,12,3])]).