re-organise repo

This commit is contained in:
2023-11-16 10:48:53 +00:00
parent f62c52d8a9
commit f133e157a1
87 changed files with 11792 additions and 0 deletions

130
2021/day18/day18.erl Normal file
View File

@@ -0,0 +1,130 @@
-module(day18).
-export ([solve/0, solve/1]).
-compile ([export_all]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(['1']) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1)]);
solve(1) ->
solution1();
solve(['2']) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2)]);
solve(2) ->
solution2().
solution1() ->
{ok, IO} = file:open("input.txt", 'read'),
Ans = maybe_reduce(read_line(IO), IO),
file:close(IO),
magnitude(Ans).
solution2() ->
{ok, IO} = file:open("input.txt", 'read'),
Numbers = read_all_lines(read_line(IO), IO, []),
file:close(IO),
io:format("total:~p lines: ~p~n", [length(Numbers), Numbers]),
max_magnitude(Numbers).
read_all_lines('eof', _IO, Acc) ->
Acc;
read_all_lines(Line, IO, Acc) ->
read_all_lines(read_line(IO), IO, [Line|Acc]).
read_line(IO) ->
case file:read_line(IO) of
{ok, Line} ->
{ok, Tokens, _} = erl_scan:string(Line ++ "."),
{ok, Term} = erl_parse:parse_term(Tokens),
Term;
'eof' -> 'eof'
end.
max_magnitude(Numbers) when is_list(Numbers) ->
lists:max([ max_magnitude(lists:split(I, Numbers)) || I <- lists:seq(1, length(Numbers))]);
max_magnitude({List1, List2}) ->
[H|T] = lists:reverse(List1),
max_magnitude(H, T ++ List2).
max_magnitude(Number, Rest) ->
lists:foldl(fun(N, MaxMag) ->
case magnitude(reduce([Number, N])) of
Mag when Mag > MaxMag -> Mag;
_ -> MaxMag
end
end, 0, Rest).
magnitude(A) when is_integer(A) -> A;
magnitude([A,B]) ->
(3 * magnitude(A)) + (2 * magnitude(B)).
maybe_reduce(A, IO) ->
case read_line(IO) of
'eof' -> A;
B ->
maybe_reduce(reduce([A,B]), IO)
end.
reduce(N) ->
case maybe_explode(N, 0) of
{true, New, _, _} ->
reduce(New);
false ->
case maybe_split(N) of
{true, New} ->
reduce(New);
false ->
N
end
end.
maybe_explode([A,B], Depth) when Depth >= 4, is_integer(A), is_integer(B) ->
{true, 0, A, B};
maybe_explode([A,B], Depth) ->
case maybe_explode(A, Depth+1) of
{true, New, AddA, AddB} ->
{true, [New, add_explode({b, AddB}, B)], AddA, 0};
false ->
case maybe_explode(B, Depth+1) of
{true, New, AddA, AddB} ->
{true, [add_explode({a, AddA}, A), New], 0, AddB};
false ->
false
end
end;
maybe_explode(N, _) when is_integer(N) ->
false.
add_explode({_, Add}, Num) when is_integer(Num) ->
Add + Num;
add_explode({b, Add}, [A,B]) ->
[add_explode({b, Add}, A), B];
add_explode({a, Add}, [A,B]) ->
[A, add_explode({a,Add}, B)].
maybe_split(N) when is_integer(N) ->
case N > 9 of
true ->
Left = N div 2,
Right = case Left*2 == N of true -> Left; false -> Left+1 end,
{true, [Left, Right]};
false ->
false
end;
maybe_split([A, B]) ->
case maybe_split(A) of
{true, NewA} ->
{true, [NewA, B]};
false ->
case maybe_split(B) of
{true, NewB} ->
{true, [A, NewB]};
false ->
false
end
end.

100
2021/day18/input.txt Normal file
View File

@@ -0,0 +1,100 @@
[[[9,2],[[2,9],0]],[1,[[2,3],0]]]
[[[[2,0],2],[[6,4],[7,3]]],[0,[[3,0],[0,6]]]]
[[[[7,2],2],[9,[6,5]]],[[2,4],5]]
[[[[7,8],2],1],[[[5,4],[2,9]],[7,8]]]
[[[0,7],[1,[6,6]]],[[[0,7],9],4]]
[[[3,[9,6]],[5,1]],[[[0,1],6],[[7,6],0]]]
[[[3,0],[7,[4,0]]],[4,[[6,6],[5,3]]]]
[[[1,[4,8]],[2,[5,8]]],[[[3,6],[2,2]],[[3,8],[7,0]]]]
[9,[[[5,0],[0,3]],[2,[2,6]]]]
[[[3,[8,2]],[[8,0],5]],[[[7,6],[4,9]],[7,5]]]
[[7,[[4,1],9]],[5,1]]
[[[5,[7,5]],1],[8,[5,8]]]
[[[[0,2],7],[[1,4],[9,8]]],[[3,[0,3]],7]]
[[[[4,3],[7,4]],[6,[6,4]]],[8,0]]
[[[1,1],1],[[5,[2,7]],7]]
[[[5,4],5],[[7,[6,3]],[[8,4],6]]]
[[[7,9],[[4,4],[0,0]]],[[[8,6],6],[2,[6,4]]]]
[[[[4,7],[4,9]],3],[[[7,1],[8,6]],[9,[8,2]]]]
[6,[6,[2,9]]]
[[4,[[5,5],[5,0]]],[[[3,4],[9,5]],[8,6]]]
[2,[0,[2,5]]]
[[[4,[7,1]],[2,8]],[[7,0],[[1,6],1]]]
[[[3,4],[[7,8],[6,7]]],[[[6,2],[1,2]],5]]
[[[8,[0,8]],[[9,9],0]],[[[3,5],[4,2]],7]]
[[0,[[0,3],2]],[4,1]]
[[[[0,4],6],7],[[4,[9,1]],3]]
[[0,[[7,0],8]],[2,[8,[8,2]]]]
[[[[3,6],2],[9,4]],[6,[[7,9],[4,5]]]]
[[[[4,9],1],[[9,6],[8,8]]],[[7,[7,6]],[[8,3],[9,0]]]]
[2,0]
[[[[8,2],0],[3,5]],[[7,2],0]]
[[[[1,9],9],6],[9,[[9,3],[8,7]]]]
[[[9,[4,0]],[[7,1],[4,4]]],[[4,[2,3]],[8,7]]]
[[[[9,7],[5,6]],[4,[6,7]]],7]
[5,[[[8,2],8],[6,[7,9]]]]
[0,[[9,[0,1]],[[8,3],7]]]
[[[[4,5],[4,2]],[[5,2],[3,1]]],[[[3,1],[8,5]],8]]
[[0,4],[[2,[2,6]],[[1,1],3]]]
[[[0,8],[7,[5,8]]],7]
[[[7,2],[[6,6],[2,7]]],[[0,[9,3]],2]]
[[[[0,9],2],[[6,0],4]],3]
[[5,[[9,6],9]],[[6,[1,2]],[1,[6,2]]]]
[[[[3,9],5],[9,[7,2]]],[5,[[3,4],[0,6]]]]
[[2,[6,7]],[0,[[2,0],7]]]
[[2,[[5,4],[2,1]]],[2,[[8,7],[5,3]]]]
[[[[0,4],[2,5]],[1,2]],[5,[8,[0,3]]]]
[[[[9,2],[3,2]],[[2,9],4]],5]
[[[[8,9],5],1],[9,3]]
[[5,2],[3,[[8,5],2]]]
[[[0,1],[7,8]],[[[6,2],4],[[6,2],[9,5]]]]
[[[[9,6],5],2],2]
[[[[3,2],3],3],[[[0,1],1],[[8,4],8]]]
[[4,[2,[3,0]]],[[6,[7,0]],6]]
[[6,[[7,8],3]],[[[2,7],4],9]]
[0,2]
[[[9,1],[[3,7],[6,0]]],[[0,[4,1]],[[5,4],7]]]
[[[3,[9,4]],8],[[5,3],2]]
[[6,6],[[[0,5],[0,9]],[[5,5],4]]]
[[[[1,2],4],[[2,4],[8,0]]],[0,[[4,4],[5,8]]]]
[0,[[[9,0],3],[8,4]]]
[[4,5],[[[9,9],[3,5]],[8,[1,4]]]]
[[7,8],[[[3,1],[7,0]],[[4,7],[9,1]]]]
[[4,[2,[1,9]]],[[6,[6,1]],[[0,3],3]]]
[[[5,[0,9]],6],[[[3,4],[9,6]],[[4,0],[0,4]]]]
[[[1,5],[8,[2,8]]],[[5,[0,8]],[[0,7],[4,6]]]]
[[9,[0,2]],[[3,3],[3,1]]]
[[[[2,8],[5,9]],[2,[1,5]]],9]
[[3,[[8,9],[3,1]]],[[[9,0],7],[[0,4],3]]]
[[[[1,5],2],[5,[5,9]]],[5,[[0,1],[0,2]]]]
[6,[[[0,4],8],[[8,2],[5,5]]]]
[[[[7,7],5],[[8,2],7]],[2,5]]
[[[1,1],[[7,8],0]],3]
[[6,[[4,2],9]],[[[5,4],4],3]]
[[[[5,8],3],[[0,4],9]],[[[2,9],2],[3,4]]]
[[0,[4,8]],6]
[[[[9,5],[1,9]],[[3,7],[5,5]]],8]
[[1,9],6]
[[[4,[1,5]],3],0]
[[[2,[6,9]],5],[[5,7],[5,[7,1]]]]
[[[[3,1],[7,3]],[[1,0],[4,6]]],[[[4,9],[4,1]],[9,[2,0]]]]
[[[5,0],[[9,4],6]],[1,[[0,4],[9,9]]]]
[[[[9,8],3],[7,5]],[[[9,5],2],[9,9]]]
[[8,[[8,0],[2,3]]],[[[3,8],[2,6]],[[1,0],0]]]
[[[7,[7,1]],[[6,6],[2,9]]],[[5,[2,0]],[[3,9],[7,4]]]]
[1,[4,[[9,7],[1,3]]]]
[[0,3],[[[4,1],7],[[4,1],[3,0]]]]
[[0,[[7,7],6]],[[4,9],2]]
[[0,8],[4,[4,5]]]
[[[8,[0,5]],[[1,3],[0,5]]],[[2,6],[1,5]]]
[[[[7,6],8],[0,[2,7]]],8]
[8,[[[5,4],8],[[2,1],[7,5]]]]
[[[[7,3],[7,1]],0],[[[7,9],2],3]]
[[8,5],[6,6]]
[[[[5,2],8],7],[[[6,8],[1,0]],[[0,0],1]]]
[[[[1,0],1],6],[9,8]]
[[[[1,2],7],[1,[2,8]]],[[8,1],[[7,5],2]]]
[[0,6],[[2,8],[9,0]]]
[[[0,[7,7]],[2,[0,8]]],[[[7,4],4],[7,[4,0]]]]
[[[2,[9,3]],[[3,7],3]],[[[9,7],[5,6]],8]]
[[2,[[8,7],2]],[[8,[1,8]],[[7,2],1]]]