# Day 8: Memory maneuver - Advent of Code

Stavros Aronis 08.12.2018

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

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

main(Args) ->
Sol =
case Args of
["2"] -> value([Input], [1]);
end,
io:format("~p~n", [Sol]).

{ok, [ChildrenCount, MetaCount]} = io:fread("", "~d ~d"),
fun() ->
M
end,
Meta = [ReadMeta() || _ <- lists:seq(1, MetaCount)],
[{Children, Meta} | Rest].

C = lists:sum(Meta),
A + B + C.

value([], Meta) ->
lists:sum(Meta);
value(Children, Which) ->
Marked = select(Which, Children),
lists:sum([value(C, M) || {C, M} <- Marked]).

select(Which, Children) ->
select(Which, Children, []).

select([], _, Acc) ->
Acc;
select([N|R], Children, Acc) ->
try
C = lists:nth(N, Children),
select(R, Children, [C|Acc])
catch
_:_ ->
select(R, Children, Acc)
end.
``````

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 12: Subterranean Sustainability - Advent of Code 2018

Article by Stavros Aronis

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