# Day 12: Subterranean Sustainability - Advent of Code 2018

Stavros Aronis 12.12.2018

I did the Advent of Code 2018 day 12 challenge in Erlang! Parts one and two are as follows:

``````#!/usr/bin/env escript
-mode(native).

main(Args) ->
{ok, [Initial]} = io:fread("", "initial state: ~s"),
Sol =
case Args of
["2"] ->  sum_state(state_after(50 * 1000, {0, Initial}, Evolve));
_ -> sum_state(state_after(20, {0, Initial}, Evolve))
end,
io:format("~p~n", [Sol]).

eof -> lists:reverse(Acc)
end.

make_evolve(List) ->
Fold =
fun([[A],[B],[C],[D],[E],[F]], Map) ->
Map#{[A,B,C,D,E] => F}
end,
lists:foldl(Fold, #{}, List).

state_after(0, State, _Evolve) ->
State;
state_after(N, State, Evolve) ->
%io:format("~p~n", [State]),
NewState = evolve(State, Evolve),
state_after(N - 1, NewState, Evolve).

evolve({F, Line}, Evolve) ->
evolve("...." ++ Line ++ "....", Evolve, F-2, []).

evolve([A,B,C,D,E|Rest], Evolve, F, Acc) ->
NC = maps:get([A,B,C,D,E], Evolve, \$.),
{NF, NAcc} =
case Acc =:= [] andalso NC =:= \$. of
true -> {F + 1, []};
false -> {F, [NC|Acc]}
end,
evolve([B,C,D,E|Rest], Evolve, NF, NAcc);
evolve(_, _, F, Acc) ->
Pred = fun(C) -> C =:= \$. end,
{F, lists:reverse(lists:dropwhile(Pred, Acc))}.

sum_state({F, List}) ->
sum_state(F, List, 0).

sum_state(_, [], Sum) -> Sum;
sum_state(I, [C|Rest], Sum) ->
NSum =
case C =:= \$# of
true -> Sum + I;
false -> Sum
end,
sum_state(I+1, Rest, NSum).
``````

Author

### Stavros Aronis

Developer & Trainer at Erlang Solutions

## ARTICLES: 6

### Day 1: The Tyranny of the Rocket Equation

Article by Stavros Aronis

Day 1: The Tyranny of the Rocket Equation

### Day 18: Settlers of The North Pole - Advent of Code 2018

Article by Stavros Aronis

I did the Advent of Code 2018 day 18 challenge in Erlang! Parts one and two are as follows:

### Day 14: Chocolate Charts - Advent of Code 2018

Article by Stavros Aronis

I did the Advent of Code 2018 day 14 challenge in Erlang! Parts one and two are as follows:

Article by Stavros Aronis

I did the Advent of Code 2018 day 13 challenge in Erlang! Parts one and two are as follows:

### Day 9: Marble Mania - Advent of Code 2018

Article by Stavros Aronis

I did the Advent of Code 2018 day 9 challenge in Erlang! Parts one and two are as follows: