diff --git a/day10/input.txt.test b/day10/input.txt.test deleted file mode 100644 index b1518d9..0000000 --- a/day10/input.txt.test +++ /dev/null @@ -1,10 +0,0 @@ -[({(<(())[]>[[{[]{<()<>> -[(()[<>])]({[<{<<[]>>( -{([(<{}[<>[]}>{[]{[(<()> -(((({<>}<{<{<>}{[]{[]{} -[[<[([]))<([[{}[[()]]] -[{[{({}]{}}([{[{{{}}([] -{<[[]]>}<{[{[{[]{()[[[] -[<(<(<(<{}))><([]([]() -<{([([[(<>()){}]>(<<{{ -<{([{{}}[<[[[<>{}]]]>[]] diff --git a/day17/day17.erl b/day17/day17.erl new file mode 100644 index 0000000..f887a61 --- /dev/null +++ b/day17/day17.erl @@ -0,0 +1,68 @@ +%% to compile: erlc day3A.erl +%% to run: erl -noshell -s day5 solve +%% +-module(day17). + +-export ([solve/0, solve/1, solve/2]). +-compile ([export_all]). + +solve() -> + solve(['1']), + solve(['2']), + init:stop(). + +solve(A) -> + solve(A, read_input()). + +solve(['1'], D) -> + io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]); +solve(1, D) -> + solution1(D); +solve(['2'], D) -> + io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]); +solve(2, D) -> + solution2(D). + +solution1({X,Y}) -> + Xs = find_x(X), + Ys = find_y(Y), + {T, X1, Y1} = lists:max([{Key, X1, proplists:get_value(Key,Ys)} || {Key, X1} <- Xs, lists:keysearch(Key, 1, Ys) /= false]), + y_max(T,Y1,0). + +solution2({X,Y}) -> + Xs = find_x(X), + Ys = find_y(Y), + length(lists:usort([{X,Y} || {Ty, Y} <- Ys, {T,X} <- lists:filter(fun({T,X}) -> T == Ty end, Xs)])). + +read_input() -> + {{124,174},{-123, -86}}. +%% {{20,30},{-10, -5}}. + +find_x({Xmin, Xmax}) -> + [ {T, Xstart} || Xstart <- lists:seq(0,400), T <- lists:seq(1,1000), x(Xstart, T) >= Xmin, x(Xstart, T) =< Xmax ]. + +find_y({Ymin, Ymax}) -> + [ {T, Ystart} || Ystart <- lists:seq(-400,400), T <- lists:seq(1,1000), y(Ystart, T) >= Ymin, y(Ystart, T) =< Ymax ]. + +x(Xstart, T) -> + Last = + case (Xstart - T + 1) of + L when L < 0 -> 0; + L -> L + end, + (Xstart - Last + 1) * (Xstart + Last) div 2. + +y(Ystart, T) -> + Last = (Ystart - T + 1), + (Ystart - Last + 1) * (Ystart + Last) div 2. + +y_max(0, _, Ymax) -> Ymax; +y_max(T, Y, Ymax) -> + NewY = y(Y, T), + NewMax = + case NewY of + M when M > Ymax -> M; + _ -> Ymax + end, + y_max(T-1, Y, NewMax). + \ No newline at end of file diff --git a/day17/diagram.txt b/day17/diagram.txt new file mode 100644 index 0000000..845b9a9 --- /dev/null +++ b/day17/diagram.txt @@ -0,0 +1,26 @@ + 1 2 3 +4 0123456789012345678901234567890 +3 .............#....#............ +2 .......#..............#........ +1 ............................... +0 S........................#..... +-1 ............................... +-2 ............................... +-3 ...........................#... +-4 ............................... +-5 ....................TTTTTTTTTTT +-6 ....................TTTTTTTTTTT +-7 ....................TTTTTTTT#TT +-8 ....................TTTTTTTTTTT +-9 ....................TTTTTTTTTTT +-10 ....................TTTTTTTTTTT + +T X y +0 0 0 = 0 +1 7 +6 = 7 +2 13 +5 = 7 + 6 +3 18 +4 = 7 + 6 + 5 +4 22 +3 = 7 + 6 + 5 + 4 +5 25 +2 +6 27 +1 +7 28 +0 \ No newline at end of file diff --git a/day17/input.txt b/day17/input.txt new file mode 100644 index 0000000..ac12128 --- /dev/null +++ b/day17/input.txt @@ -0,0 +1 @@ +target area: x=124..174, y=-123..-86 \ No newline at end of file diff --git a/day18/day18.erl b/day18/day18.erl new file mode 100644 index 0000000..cc4223c --- /dev/null +++ b/day18/day18.erl @@ -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. \ No newline at end of file diff --git a/day18/input.txt b/day18/input.txt new file mode 100644 index 0000000..a49398e --- /dev/null +++ b/day18/input.txt @@ -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]]] \ No newline at end of file diff --git a/day5/day5 b/day5/day5 deleted file mode 100755 index c531a5d..0000000 Binary files a/day5/day5 and /dev/null differ diff --git a/day9/input.txt.test b/day9/input.txt.test deleted file mode 100644 index 6dee4a4..0000000 --- a/day9/input.txt.test +++ /dev/null @@ -1,5 +0,0 @@ -2199943210 -3987894921 -9856789892 -8767896789 -9899965678