]> fbox.kageds.com Git - adventofcode.git/commitdiff
commit
authorAlan Evans <alan@kageds.com>
Mon, 3 Oct 2022 10:30:54 +0000 (10:30 +0000)
committerAlan Evans <alan@kageds.com>
Mon, 3 Oct 2022 10:30:54 +0000 (10:30 +0000)
16 files changed:
day11/day11.erl [new file with mode: 0644]
day11/input.txt [new file with mode: 0644]
day12/day12.erl [new file with mode: 0644]
day12/input.txt [new file with mode: 0644]
day13/commmands.txt [new file with mode: 0644]
day13/day13.erl [new file with mode: 0644]
day13/input.txt [new file with mode: 0644]
day14/day14.erl [new file with mode: 0644]
day14/input.txt [new file with mode: 0644]
day15/day15.erl [new file with mode: 0644]
day15/input.test [new file with mode: 0644]
day15/input.txt [new file with mode: 0644]
day16/day16.erl [new file with mode: 0644]
day16/input.txt [new file with mode: 0644]
day3/day3
day5/day5 [new file with mode: 0755]

diff --git a/day11/day11.erl b/day11/day11.erl
new file mode 100644 (file)
index 0000000..5f566c1
--- /dev/null
@@ -0,0 +1,144 @@
+%% to compile: erlc day3A.erl
+%% to run: erl -noshell -s day3 solve
+%%
+-module(day11).
+
+-export ([solve/0, solve/1, solve/2]).
+-export ([read_input/0]).
+
+-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).
+
+read_input() ->
+       {ok, IO} = file:open("input.txt", 'read'),
+       Data = read_input(IO),
+       file:close(IO),
+        Data.
+
+read_input(IO) ->
+       read_input(IO, []).
+
+read_input(IO, Input) ->
+       case read_line(IO) of
+               'eof' -> Input;
+               Line -> read_input(IO, Input ++ [Line])
+       end.
+
+read_line(IO) ->
+       case file:read_line(IO) of
+               'eof' -> 'eof';
+               {ok, Line} -> [ X - $0 || X <- Line, [X] /= "\n"]
+       end.
+
+solution1(Input) ->
+    Coords = coords(Input),
+    Data = [ {X, init_data(X, Input)} || X <- Coords],
+    put(flash, 0),
+    sn1_step(Data, 0),
+    get(flash).
+
+sn1_step(Data, 100) -> Data;
+sn1_step(Data, Count) ->
+    Step1 = step1(Data),
+    Step2 = step2(Step1),
+    sn1_step(Step2, Count + 1).
+
+solution2(Input) ->
+    Coords = coords(Input),
+    Data = [ {X, init_data(X, Input)} || X <- Coords],
+    put(flash, 0),
+    sn2_step(Data, 0).
+
+sn2_step(Data, Count) ->
+    Step1 = step1(Data),
+    Step2 = step2(Step1),
+    case all_flash(Step2) of
+           'true'-> Count + 1;
+           'false' -> sn2_step(Step2, Count + 1)
+    end.
+
+all_flash(Data) ->
+       lists:all(fun({_,V}) -> V == 0 end, Data).
+
+coords([H|_] =  Table) ->
+  X = length(H),
+  Y = length(Table),
+  lists:flatten([[{A,B} || A <- lists:seq(1,X)] || B <- lists:seq(1,Y)]).
+
+
+step1(Data) ->
+       lists:map(fun({C, X}) -> case X of 9 -> incr_flash(), {C, 'flash'}; _ -> {C, X + 1} end end, Data).
+
+step2(Data) ->
+       case is_flash(Data) of
+               'true' -> flash(Data);
+               'false' -> clear_flashed(Data)
+       end.
+
+is_flash(Data) ->
+       lists:any(fun({_, X}) -> X == 'flash' end, Data).
+
+flash(Data) ->
+        NewData = flash(Data, Data),
+        step2(flashed(Data, NewData)).
+
+flash([], Acc) -> Acc;
+flash([{{X,Y},V}|T], Acc) when V  == 'flash' ->
+    flash(T, increase_neighbors({X,Y}, Acc));
+flash([_|T], Acc) -> flash(T, Acc).
+
+clear_flashed(Data) ->
+       clear_flashed(Data, []).
+
+clear_flashed([], Acc)  -> lists:reverse(Acc);
+clear_flashed([{C, V}|T], Acc) when V == 'flashed' ->
+       clear_flashed(T, [{C, 0}|Acc]);
+clear_flashed([H|T], Acc) ->
+       clear_flashed(T, [H|Acc]).
+
+
+flashed(OldData, NewData)  -> 
+    lists:zipwith(fun({C, V}, N) -> case V of 'flash' -> {C, 'flashed'}; _ -> N end end, OldData, NewData).
+
+increase_neighbors({X,Y}, Data) ->
+    Neigbours = [{X+1,Y},{X-1,Y},{X,Y+1},{X,Y-1},{X+1,Y+1},{X+1,Y-1},{X-1,Y+1},{X-1,Y-1}],
+    increase_neighbor(Neigbours, Data).
+
+increase_neighbor([], Data) -> Data;
+increase_neighbor([{X,Y}|T], Data)  when X < 1; Y < 1; X > 10; Y > 10 -> io:format("ignore: ~p~n",[{X,Y}]), increase_neighbor(T,Data);
+increase_neighbor([H|T], Data) ->
+    case  get_value(H, Data) of
+           'flash' -> increase_neighbor(T, Data);
+           'flashed' -> increase_neighbor(T, Data);
+           9 -> incr_flash(), increase_neighbor(T, set_value(H, Data, 'flash'));
+           V -> increase_neighbor(T, set_value(H, Data, V + 1))
+    end.
+
+get_value(C, Table) ->
+       {C, V} = lists:keyfind(C, 1, Table),
+       V.
+
+set_value(C, Table, V) ->
+        lists:keyreplace(C, 1, Table, {C, V}).
+
+init_data({X,Y}, Table) ->
+        lists:nth(X, lists:nth(Y, Table)).
+
+incr_flash() ->
+       put(flash, get(flash) + 1).
diff --git a/day11/input.txt b/day11/input.txt
new file mode 100644 (file)
index 0000000..c671d4a
--- /dev/null
@@ -0,0 +1,10 @@
+6617113584
+6544218638
+5457331488
+1135675587
+1221353216
+1811124378
+1387864368
+4427637262
+6778645486
+3682146745
diff --git a/day12/day12.erl b/day12/day12.erl
new file mode 100644 (file)
index 0000000..99fff84
--- /dev/null
@@ -0,0 +1,100 @@
+%% to compile: erlc day3A.erl
+%% to run: erl -noshell -s day5 solve
+%%
+-module(day12).
+
+-export ([solve/0, solve/1, solve/2]).
+-export ([read_input/0]).
+
+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).
+
+read_input() ->
+    {ok, IO} = file:open("input.txt", 'read'),
+    Data = read_input(IO),
+    file:close(IO),
+    Data.
+
+read_input(IO) ->
+       read_input(IO, []).
+
+read_input(IO, Input) ->
+    case read_line(IO) of
+       'eof' -> Input;
+       Line -> read_input(IO, Input ++ [Line])
+    end.
+
+read_line(IO) ->
+       case file:read_line(IO) of
+               'eof' -> 'eof';
+               {ok, Line} -> parse_line(Line)
+       end.
+
+parse_line(Line) ->
+    [Cave1, Cave2] = string:tokens(Line, "-\n"),
+    {Cave1, Cave2}.
+
+solution1(Caves) ->
+   CaveMap = insert(Caves),
+   count_paths("start", #{}, 'some', CaveMap).
+
+solution2(Caves) ->
+   CaveMap = insert(Caves),
+   count_paths("start", #{}, 'none', CaveMap).
+
+
+insert(Caves) -> insert(Caves, maps:new()).
+
+insert([], CaveMap) -> CaveMap;
+insert([{Cave1, Cave2}|Rest], CaveMap) ->
+       NewMap = 
+       case maps:is_key(Cave1, CaveMap) of
+               'false' -> maps:put(Cave1, [Cave2], CaveMap);
+               'true' -> maps:put(Cave1, [Cave2|maps:get(Cave1, CaveMap)], CaveMap)
+       end,
+       NewMap2 = 
+        case maps:is_key(Cave2, NewMap) of
+                'false' -> maps:put(Cave2, [Cave1], NewMap);
+                'true' -> maps:put(Cave2, [Cave1|maps:get(Cave2, CaveMap)], NewMap)
+        end,
+       insert(Rest, NewMap2).
+
+visit_vertex(Name, Visited, VisitedTwice, Graph) ->
+    NewVisited =
+        case hd(Name) >= $a of
+            true -> Visited#{Name => true};
+            false -> Visited
+        end,
+    #{Name := Neighbours} = Graph,
+    lists:sum([count_paths(N, NewVisited, VisitedTwice, Graph) || N <- Neighbours]).
+
+count_paths("end", _, _, _) ->
+    % We have found an entire path through the graph.
+    1;
+count_paths("start", Visited, _, _) when map_size(Visited) > 0 ->
+    % We cannot visit the start cave more than once.
+    0;
+count_paths(Name, Visited, VisitedTwice, Graph) when hd(Name) >= $a andalso is_map_key(Name, Visited) ->
+    % We can visit only one small cave twice.
+    case VisitedTwice of
+        'none' -> visit_vertex(Name, Visited, Name, Graph);
+         _ -> 0
+    end;
+count_paths(Name, Visited, VisitedTwice, Graph) ->
+    % Small caves can only be visited once, but large caves may be visited any
+    % number of times.
+    visit_vertex(Name, Visited, VisitedTwice, Graph).
diff --git a/day12/input.txt b/day12/input.txt
new file mode 100644 (file)
index 0000000..a409ba5
--- /dev/null
@@ -0,0 +1,21 @@
+TR-start
+xx-JT
+xx-TR
+hc-dd
+ab-JT
+hc-end
+dd-JT
+ab-dd
+TR-ab
+vh-xx
+hc-JT
+TR-vh
+xx-start
+hc-ME
+vh-dd
+JT-bm
+end-ab
+dd-xx
+end-TR
+hc-TR
+start-vh
diff --git a/day13/commmands.txt b/day13/commmands.txt
new file mode 100644 (file)
index 0000000..0346a69
--- /dev/null
@@ -0,0 +1,12 @@
+fold along x=655
+fold along y=447
+fold along x=327
+fold along y=223
+fold along x=163
+fold along y=111
+fold along x=81
+fold along y=55
+fold along x=40
+fold along y=27
+fold along y=13
+fold along y=6
diff --git a/day13/day13.erl b/day13/day13.erl
new file mode 100644 (file)
index 0000000..02b0da9
--- /dev/null
@@ -0,0 +1,88 @@
+%% to compile: erlc day3A.erl
+%% to run: erl -noshell -s day5 solve
+%%
+-module(day13).
+
+-export ([solve/0, solve/1, solve/2]).
+-export ([read_input/0]).
+
+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).
+
+read_input() ->
+    {ok, IO} = file:open("input.txt", 'read'),
+    Data = read_input(IO),
+    file:close(IO),
+    Data.
+
+read_input(IO) ->
+       read_input(IO, []).
+
+read_input(IO, Input) ->
+    case read_line(IO) of
+       'eof' -> Input;
+       Line -> read_input(IO, Input ++ [Line])
+    end.
+
+read_line(IO) ->
+       case file:read_line(IO) of
+               'eof' -> 'eof';
+               {ok, Line} -> parse_line(Line)
+       end.
+
+parse_line(Line) ->
+    Points = string:tokens(Line, " ,\n"),
+    [X, Y] = [list_to_integer(X) || X <- Points],
+    {X, Y}.
+
+solution1(Input) ->
+    io:format("Input ~p~n", [Input]),
+    F1 = fold(x, 655, Input),
+    length(F1).
+
+solution2(Input) ->
+    I = fold(y,6,fold(y,13,fold(y,27,fold(x,40,fold(y,55,fold(x,81,fold(y,111,fold(x,163,fold(y,223,fold(x,327,fold(y,447,fold(x,655, Input)))))))))))),
+    MaxX = lists:foldl(fun({T, _}, Acc) -> case T > Acc of 'true' -> T; _ -> Acc end end, 0, I),
+    MaxY = lists:foldl(fun({_, T}, Acc) -> case T > Acc of 'true' -> T; _ -> Acc end end, 0, I),
+    Coords = [{X,Y} || Y <- lists:seq(0,MaxY), X <- lists:seq(0,MaxX)],
+    output(Coords, I, MaxX).
+
+output([], I, MaxX) -> ok;
+output([{X,Y}|Rest], I, MaxX) ->
+       case lists:member({X,Y}, I) of
+               'true' -> io:format("#");
+               'false' -> io:format(" ")
+       end,
+       case X == MaxX of
+               'true' -> io:format("~n");
+               'false' -> ok
+       end,
+       output(Rest, I, MaxX).
+
+fold(y, N, Input) ->
+    L1 = [ {X, (Y * -1) + N}  || {X, Y} <- Input],
+    {Sat, NotSat} = lists:partition(fun({_,Y}) -> Y < 0 end, L1),
+    Sat2 = [{X, Y * -1}|| {X, Y} <- Sat],
+    L2 = lists:usort(NotSat ++ Sat2),
+    [ {X, (Y - N) * -1}  || {X,Y} <- L2];
+fold(x, N, Input) ->
+    L1 = [ {(X * -1) + N, Y}  || {X, Y} <- Input],
+    {Sat, NotSat} = lists:partition(fun({X,_}) -> X < 0 end, L1),
+    Sat2 = [{X * -1, Y}|| {X, Y} <- Sat],
+    L2 = lists:usort(NotSat ++ Sat2),
+    [ {(X - N) * -1, Y}  || {X,Y} <- L2].
+
diff --git a/day13/input.txt b/day13/input.txt
new file mode 100644 (file)
index 0000000..a1b1fcf
--- /dev/null
@@ -0,0 +1,907 @@
+949,224
+398,211
+402,700
+900,890
+1197,304
+333,809
+681,705
+769,864
+975,465
+639,523
+445,313
+912,99
+502,894
+703,343
+572,598
+1232,759
+277,640
+700,761
+919,429
+678,141
+1054,795
+934,750
+760,73
+268,627
+336,859
+1096,135
+646,176
+55,404
+932,872
+1168,338
+569,842
+904,863
+647,168
+509,606
+42,525
+607,343
+947,861
+1002,537
+1230,368
+186,274
+363,690
+1073,663
+868,312
+646,718
+385,210
+961,660
+1277,0
+470,228
+127,705
+894,89
+500,704
+994,361
+87,582
+985,378
+557,607
+75,872
+1068,647
+686,138
+358,84
+957,415
+325,516
+711,169
+1287,252
+619,242
+329,516
+821,714
+514,844
+632,358
+549,252
+442,302
+619,652
+5,809
+276,530
+15,75
+653,266
+632,536
+731,648
+237,231
+1266,19
+966,205
+360,37
+522,3
+1255,725
+833,840
+898,728
+805,162
+294,261
+1250,833
+403,151
+1288,564
+7,465
+350,274
+1133,478
+408,526
+1079,493
+310,205
+420,507
+1310,596
+639,824
+328,257
+30,417
+845,511
+196,683
+1002,201
+719,96
+15,457
+276,812
+760,43
+979,95
+319,429
+661,429
+654,298
+584,421
+372,355
+351,252
+430,453
+113,304
+701,772
+107,628
+549,700
+629,551
+492,851
+427,101
+278,666
+246,856
+194,82
+417,439
+433,712
+425,229
+308,89
+1275,652
+1193,316
+482,190
+955,245
+465,511
+688,37
+788,525
+92,235
+644,417
+1247,877
+25,327
+1255,404
+502,0
+960,760
+431,570
+741,702
+728,606
+1086,453
+1198,694
+181,208
+401,327
+959,194
+729,863
+1295,819
+7,271
+263,456
+132,868
+318,849
+242,726
+1133,392
+631,294
+410,302
+654,582
+649,465
+622,709
+805,284
+1300,533
+731,22
+579,470
+177,392
+1079,849
+252,397
+898,871
+634,618
+427,653
+433,630
+1228,851
+686,865
+1079,32
+105,252
+372,761
+15,864
+644,222
+1134,122
+572,851
+1303,429
+991,726
+304,620
+962,446
+899,586
+667,44
+786,228
+719,93
+411,742
+1031,110
+33,0
+646,483
+482,288
+885,47
+1250,609
+632,330
+594,833
+1178,868
+68,542
+482,480
+818,851
+278,592
+898,726
+141,660
+360,857
+515,651
+30,532
+865,540
+1168,500
+1022,525
+80,526
+975,429
+401,159
+1256,523
+1300,809
+410,598
+537,540
+482,606
+25,551
+229,709
+1096,555
+952,756
+23,70
+584,38
+810,288
+495,416
+278,358
+900,43
+550,43
+895,582
+1221,213
+1205,252
+1180,732
+524,3
+850,641
+1054,421
+865,581
+376,37
+751,233
+72,64
+969,717
+1032,666
+1170,50
+112,200
+279,784
+818,43
+810,704
+972,222
+738,296
+1285,791
+162,403
+333,85
+1168,786
+1161,51
+840,498
+3,351
+966,644
+522,891
+666,222
+751,592
+842,50
+480,446
+885,847
+7,623
+137,638
+1208,703
+668,716
+477,416
+1307,351
+981,555
+117,578
+430,254
+1280,417
+428,809
+743,312
+634,620
+1225,724
+10,869
+631,700
+87,312
+1237,689
+388,285
+358,810
+1111,894
+1133,499
+113,752
+666,670
+1198,200
+196,451
+1079,302
+1098,521
+987,95
+1295,523
+1275,591
+1131,855
+440,583
+1171,691
+955,201
+112,135
+291,861
+50,588
+1032,358
+678,330
+689,446
+30,815
+411,152
+505,610
+251,246
+1052,430
+427,793
+1173,383
+1143,431
+607,791
+189,32
+1057,7
+1307,543
+1173,374
+430,677
+313,289
+624,756
+1048,429
+1205,70
+231,862
+947,690
+966,250
+1220,851
+1253,210
+269,427
+850,701
+1034,812
+748,211
+868,73
+279,110
+664,624
+971,506
+1009,327
+214,555
+80,632
+1136,648
+1133,854
+557,5
+997,605
+1079,45
+174,648
+159,351
+341,717
+934,218
+549,194
+1079,862
+774,467
+423,177
+1126,50
+1039,255
+154,717
+1256,75
+1006,620
+1136,757
+73,831
+65,441
+95,415
+0,596
+455,33
+460,641
+528,427
+15,437
+627,201
+142,786
+714,386
+922,144
+97,606
+455,57
+1084,309
+840,725
+840,396
+428,533
+679,600
+607,103
+965,662
+674,542
+199,894
+691,425
+728,382
+636,352
+251,85
+149,527
+181,686
+253,119
+1124,172
+1059,757
+25,383
+15,371
+540,347
+793,638
+1305,809
+1221,231
+681,479
+870,107
+818,512
+427,457
+174,757
+559,233
+1263,343
+11,626
+174,246
+44,875
+211,453
+1000,250
+160,714
+802,698
+181,9
+883,101
+994,415
+1262,106
+715,548
+934,228
+378,82
+262,429
+23,252
+495,351
+885,665
+656,582
+1111,837
+609,324
+704,788
+994,733
+348,620
+877,642
+971,850
+1242,430
+513,234
+341,157
+221,476
+443,441
+159,767
+907,295
+1287,824
+622,485
+1297,659
+863,455
+1266,133
+596,386
+1042,85
+566,805
+711,404
+1019,204
+904,479
+1310,760
+1164,877
+1054,235
+986,392
+952,84
+468,50
+231,302
+1159,716
+979,799
+1178,815
+686,29
+336,35
+796,844
+1059,375
+445,130
+644,533
+865,537
+542,86
+1130,129
+1196,750
+162,627
+95,849
+339,492
+1173,256
+489,597
+972,415
+877,630
+885,717
+726,38
+1288,834
+934,302
+870,364
+711,649
+773,130
+410,43
+1133,395
+1091,714
+900,296
+258,240
+1144,852
+982,596
+1129,885
+960,386
+786,443
+378,872
+1130,765
+348,274
+1230,526
+415,312
+729,31
+1180,162
+793,704
+448,525
+149,630
+816,180
+555,121
+974,45
+1143,687
+214,87
+48,788
+499,367
+412,728
+796,396
+1260,812
+679,376
+65,383
+591,96
+512,52
+420,828
+589,889
+435,649
+149,591
+1114,666
+231,849
+1151,351
+64,205
+1240,856
+970,436
+145,457
+579,648
+50,193
+1168,108
+22,834
+885,495
+1218,235
+70,347
+1293,353
+632,834
+278,857
+1213,453
+971,268
+514,396
+957,479
+619,649
+490,736
+1302,849
+1086,640
+890,380
+619,591
+671,70
+159,655
+10,421
+862,369
+441,192
+818,64
+1183,705
+679,824
+1196,185
+114,302
+633,660
+1228,40
+435,245
+256,235
+651,351
+410,296
+947,21
+992,859
+89,213
+482,177
+112,459
+999,630
+774,203
+763,628
+985,516
+1261,640
+828,187
+562,211
+923,10
+87,309
+1146,736
+512,500
+305,234
+1295,371
+647,271
+1096,584
+508,353
+1297,812
+972,448
+688,485
+1081,512
+104,403
+401,703
+1073,231
+311,883
+833,812
+1032,857
+239,582
+503,716
+343,152
+664,176
+1168,672
+167,207
+401,775
+1116,474
+398,683
+331,799
+952,532
+1228,647
+1287,600
+908,642
+402,642
+355,245
+335,465
+788,891
+678,536
+8,493
+318,859
+1235,872
+177,873
+748,659
+242,168
+796,641
+440,331
+162,491
+1171,203
+477,840
+350,162
+524,666
+591,877
+22,330
+962,274
+629,567
+977,809
+828,480
+291,413
+1211,569
+1193,764
+468,274
+805,591
+395,45
+589,511
+1299,388
+883,437
+1302,483
+1097,702
+868,821
+423,401
+972,446
+30,79
+376,218
+899,742
+339,44
+721,63
+67,54
+795,651
+353,415
+805,723
+246,247
+261,5
+1047,456
+706,82
+344,644
+1170,760
+984,596
+493,297
+253,735
+349,234
+155,840
+349,660
+736,695
+994,670
+199,837
+221,712
+423,65
+1083,101
+386,732
+346,245
+1133,873
+1121,63
+1255,714
+880,441
+514,641
+1299,836
+1108,33
+1278,400
+241,500
+788,79
+1056,569
+810,190
+584,856
+1043,548
+1285,103
+1288,249
+1004,274
+1034,530
+244,890
+1192,691
+401,31
+155,591
+72,736
+351,642
+487,416
+934,37
+319,255
+1136,137
+442,73
+169,170
+1295,75
+862,525
+388,526
+831,58
+1119,649
+411,84
+902,247
+253,7
+840,617
+1000,196
+821,138
+1000,205
+710,819
+13,235
+914,654
+1161,527
+1262,261
+492,830
+455,705
+622,185
+1047,793
+140,760
+1275,649
+316,733
+345,662
+267,884
+1141,618
+1299,58
+440,107
+991,175
+852,451
+23,824
+375,110
+468,172
+541,590
+1001,801
+773,481
+442,373
+788,369
+500,288
+960,610
+894,252
+224,631
+1111,189
+1059,533
+1029,716
+114,709
+768,883
+541,752
+1076,514
+1277,894
+1133,306
+202,861
+798,52
+845,383
+647,726
+833,416
+584,473
+841,234
+1245,5
+30,756
+818,267
+885,512
+572,488
+417,614
+1155,840
+774,691
+149,51
+786,732
+10,701
+689,33
+440,364
+681,63
+643,44
+294,633
+786,666
+411,586
+256,795
+793,714
+32,494
+768,135
+378,530
+196,228
+164,736
+822,490
+495,767
+105,264
+1073,679
+445,242
+719,877
+146,698
+1262,358
+736,526
+1237,831
+965,93
+177,588
+877,712
+75,22
+887,65
+915,849
+1016,261
+22,141
+227,457
+540,99
+8,411
+701,570
+629,343
+261,453
+154,707
+117,255
+72,830
+691,652
+751,302
+447,455
+890,507
+209,354
+1238,736
+338,448
+574,526
+1000,698
+324,502
+1066,4
+44,19
+632,60
+768,459
+25,567
+1034,364
+949,670
+231,829
+924,610
+166,42
+703,103
+738,851
+212,521
+898,168
+664,718
+683,201
+358,756
+45,527
+1235,22
+666,533
+686,756
+132,369
+1252,715
+1016,270
+828,288
+226,309
+358,138
+1066,247
+960,162
+517,399
+401,607
+301,327
+8,483
+482,704
+882,585
+744,805
+406,863
+1148,627
+44,133
+1253,684
+1129,9
+1193,191
+815,351
+738,406
+388,592
+515,243
+155,303
+447,439
+917,677
+1041,609
+10,361
+137,383
+858,861
+1012,441
+1173,404
+1041,427
+137,520
+442,750
+166,852
+147,263
+835,396
+912,211
+209,481
+879,570
+959,600
+495,655
+1119,21
+253,241
+631,642
+492,512
+719,798
+344,205
+599,245
+997,289
+703,791
+1124,274
+1071,582
+909,607
diff --git a/day14/day14.erl b/day14/day14.erl
new file mode 100644 (file)
index 0000000..000406c
--- /dev/null
@@ -0,0 +1,168 @@
+%% to compile: erlc day3A.erl
+%% to run: erl -noshell -s day5 solve
+%%
+-module(day14).
+
+-export ([solve/0, solve/1, solve/2]).
+-export ([expand_pair/4, read_input/0]).
+-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).
+
+read_input() ->
+    {ok, IO} = file:open("input.txt", 'read'),
+    Data = read_input(IO),
+    file:close(IO),
+    Data.
+
+read_input(IO) ->
+       read_input(IO, []).
+
+read_input(IO, Input) ->
+    case read_line(IO) of
+       'eof' -> make_map(Input);
+       Line -> read_input(IO, Input ++ [Line])
+    end.
+
+read_line(IO) ->
+       case file:read_line(IO) of
+               'eof' -> 'eof';
+               {ok, Line} -> parse_line(Line)
+       end.
+
+parse_line(Line) ->
+    string:tokens(Line, " ->\n").
+
+make_map(Input) -> make_map(Input, maps:new()).
+
+make_map([], Map) -> Map;
+make_map([[P, [Res]]|Rest], Map) ->
+       NewMap = maps:put(P, Res, Map),
+        make_map(Rest, NewMap).
+
+solution1(PolyMap) ->
+   Ets = ets:new(polymer, [set, public, named_table]),
+   Polymer = "FNFPPNKPPHSOKFFHOFOC",
+   init_counters(Ets, PolyMap),
+   io:format("~p~n", [ets:tab2list(Ets)]),
+   Pairs = make_pairs(Polymer, []),
+   io:format("expand pairs: ~p~n",[Pairs]),
+   count_pairs(Pairs, Ets),
+   [count_elems(Pair, Ets, 1) || Pair <- Pairs],
+   loop(Ets, PolyMap, 10),
+   get_max_elm(Ets) - get_min_elm(Ets).
+
+solution2(PolyMap) ->
+   Ets = ets:new(polymer, [set, public, named_table]),
+   Polymer = "FNFPPNKPPHSOKFFHOFOC",
+   init_counters(Ets, PolyMap),
+   io:format("~p~n", [ets:tab2list(Ets)]),
+   Pairs = make_pairs(Polymer, []),
+   io:format("expand pairs: ~p~n",[Pairs]),
+   count_pairs(Pairs, Ets),
+   [count_elems(Pair, Ets, 1) || Pair <- Pairs],
+   loop(Ets, PolyMap, 40),
+   get_max_elm(Ets) - get_min_elm(Ets).
+
+get_max_elm(Ets) ->
+    Ents = ets:tab2list(Ets),
+    {V, _} = lists:max([{V,X} || {{elem, X},V} <- Ents]),
+    V.
+
+get_min_elm(Ets) ->
+    Ents = ets:tab2list(Ets),
+    {V, _} = lists:min([{V,X} || {{elem, X},V} <- Ents]),
+    V.
+
+loop(Ets, _PolyMap, 0) -> ets:tab2list(Ets);
+loop(Ets, PolyMap, Count) ->
+       Counters = get_counters(Ets),
+       [update_ets(Ets, PolyMap, Counter) || Counter <- Counters],
+       loop(Ets, PolyMap, Count - 1).
+
+init_counters(Ets, PolyMap) ->
+     maps:foreach(fun([X,Y] = Key, _Value) -> ets:insert(Ets, {{elem,X},0}), ets:insert(Ets, {{elem,Y},0}), ets:insert(Ets, {{pair, Key}, 0}) end, PolyMap).
+
+update_ets(_Ets, _PolyMap, {_Key, 0}) -> ok;
+update_ets(Ets, PolyMap, {[X,Y] = Key, Value}) ->
+       V = maps:get(Key, PolyMap),
+       count_pair([X,V],Ets, Value),
+       count_pair([V,Y],Ets, Value),
+       count_pair(Key,Ets, -1 * Value),
+       count_elem(V, Ets, Value).
+       
+get_counters(Ets) ->
+    Ents = ets:tab2list(Ets),
+    [{X,V} || {{pair, X},V} <- Ents].
+
+make_pairs([_], Pairs) -> Pairs;
+make_pairs([X,Y|Rest], Pairs) -> make_pairs([Y|Rest], [[X,Y]|Pairs]).
+
+count_pairs([], _Ets) -> ok;
+count_pairs([P|Rest], Ets) ->
+       count_pair(P, Ets, 1),
+       count_pairs(Rest, Ets).
+
+count_pair(P, Ets, V) ->
+       ets:update_counter(Ets, {pair,P}, {2,V}).
+
+count_elems([X,Y], Ets, V) ->
+       count_elem(X, Ets, V),
+       count_elem(Y, Ets, V).
+
+count_elem(Elem, Ets, V) ->
+       ets:update_counter(Ets, {elem,Elem}, {2,V}).
+
+expand_pair(_P, _PolyMap, _Ets, 0) -> ok;
+expand_pair([P1,P2], PolyMap, Ets, Count) when is_list(P1), is_list(P2) ->
+       expand_pair(P1,PolyMap, Ets, Count - 1);
+expand_pair([X,Y] = P, PolyMap, Ets, Count) ->
+       [V] = maps:get(P, PolyMap),
+       ets:update_counter(Ets, [V], {2,1}),       
+       [expand_pair(Pa, PolyMap, Ets, Count) || Pa <- [[X,V]] ++ [[V,Y]]].
+
+
+pairs([], _PolyMap, Pairs, _Ets, 0) ->
+       Pairs;
+pairs([], PolyMap, Pairs, Ets, Count) ->
+       io:format("new pairs: ~p~n", [Pairs]),
+       pairs(Pairs, PolyMap, [], Ets, Count - 1);
+pairs([[X,Y] = P|Rest], PolyMap, Pairs, Ets, Count) ->
+       [V] = maps:get(P, PolyMap),
+       ets:update_counter(Ets, [V], {2,1}),       
+       pairs(Rest, PolyMap, Pairs ++ [[X,V]] ++ [[V,Y]], Ets, Count).
+
+count(Polymer) ->
+       Ps = lists:usort(Polymer),
+       [ {count_p(P, Polymer), [P]} || P  <- Ps].
+
+count_p(P, Polymer) ->
+       lists:foldl(fun(X, Acc) -> case X == P of 'true' -> Acc + 1; _ -> Acc end end, 0, Polymer).
+
+steps(Polymer, _PolyMap, 0) -> Polymer;
+steps(Polymer, PolyMap, Count) ->
+   Insert = get_insert_list(Polymer, PolyMap),
+   steps(lists:flatten(lists:zipwith(fun(A, B) -> [A,B] end, Polymer, Insert)), PolyMap, Count - 1).
+
+get_insert_list(Polymer, PolyMap) ->
+       get_insert_list(Polymer, PolyMap, []).
+
+get_insert_list([_], _PolyMap, InsertList) -> lists:reverse([[]|InsertList]);
+get_insert_list([P1,P2|Rest], PolyMap, InsertList) ->
+       V = maps:get([P1,P2], PolyMap),
+       get_insert_list([P2|Rest], PolyMap, [V|InsertList]).
diff --git a/day14/input.txt b/day14/input.txt
new file mode 100644 (file)
index 0000000..ce8cf45
--- /dev/null
@@ -0,0 +1,100 @@
+VS -> B
+SV -> C
+PP -> N
+NS -> N
+BC -> N
+PB -> F
+BK -> P
+NV -> V
+KF -> C
+KS -> C
+PV -> N
+NF -> S
+PK -> F
+SC -> F
+KN -> K
+PN -> K
+OH -> F
+PS -> P
+FN -> O
+OP -> B
+FO -> C
+HS -> F
+VO -> C
+OS -> B
+PF -> V
+SB -> V
+KO -> O
+SK -> N
+KB -> F
+KH -> C
+CC -> B
+CS -> C
+OF -> C
+FS -> B
+FP -> H
+VN -> O
+NB -> N
+BS -> H
+PC -> H
+OO -> F
+BF -> O
+HC -> P
+BH -> S
+NP -> P
+FB -> C
+CB -> H
+BO -> C
+NN -> V
+SF -> N
+FC -> F
+KK -> C
+CN -> N
+BV -> F
+FK -> C
+CF -> F
+VV -> B
+VF -> S
+CK -> C
+OV -> P
+NC -> N
+SS -> F
+NK -> V
+HN -> O
+ON -> P
+FH -> O
+OB -> H
+SH -> H
+NH -> V
+FF -> B
+HP -> B
+PO -> P
+HB -> H
+CH -> N
+SN -> P
+HK -> P
+FV -> H
+SO -> O
+VH -> V
+BP -> V
+CV -> P
+KP -> K
+VB -> N
+HV -> K
+SP -> N
+HO -> P
+CP -> H
+VC -> N
+CO -> S
+BN -> H
+NO -> B
+HF -> O
+VP -> K
+KV -> H
+KC -> F
+HH -> C
+BB -> K
+VK -> P
+OK -> C
+OC -> C
+PH -> H
diff --git a/day15/day15.erl b/day15/day15.erl
new file mode 100644 (file)
index 0000000..ac0af93
--- /dev/null
@@ -0,0 +1,166 @@
+%% to compile: erlc day3A.erl
+%% to run: erl -noshell -s day3 solve
+%%
+-module(day15).
+
+-export ([solve/0, solve/1, solve/2]).
+-export ([read_input/0]).
+
+-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).
+
+read_input() ->
+       {ok, IO} = file:open("input.txt", 'read'),
+       Data = read_input(IO),
+       file:close(IO),
+        Data.
+
+read_input(IO) ->
+       read_input(IO, []).
+
+read_input(IO, Input) ->
+       case read_line(IO) of
+               'eof' -> Input;
+               Line -> read_input(IO, Input ++ [Line])
+       end.
+
+read_line(IO) ->
+       case file:read_line(IO) of
+               'eof' -> 'eof';
+               {ok, Line} -> [ X - $0 || X <- Line, [X] /= "\n"]
+       end.
+
+solution1(Input) ->
+    Graph = astar:graph(Input),
+    astar:search(Graph, {0, 0}, {99, 99}).
+
+solution2(Input) ->
+    Graph = astar:graph(Input),
+    astar:search(Graph, {0, 0}, {499, 499}).
+
+coords([H|_] =  Table) ->
+  X = length(H),
+  Y = length(Table),
+  lists:flatten([[{A,B} || A <- lists:seq(1,X)] || B <- lists:seq(1,Y)]).
+
+
+%init_data({X,Y}, Table) when X =< 100 andalso Y =< 100 ->
+%    lists:nth(X, lists:nth(Y, Table));
+init_data({X,Y}, Table) ->
+    X1 = 
+    case (X rem 100) of
+        0 -> 100;
+        A -> A
+    end,
+    Y1 =
+    case (Y rem 100) of
+        0 -> 100;
+        B -> B
+    end,
+    D = lists:nth(X1, lists:nth(Y1, Table)),
+    D1 = (D + ((X-1) div 100) + ((Y-1) div 100)) rem 9,
+    D2 = 
+    case D1 of
+           0 -> 9;
+           C -> C
+    end,
+%%    io:format("~p -> ~p -> ~p~n", [{X,Y},{X1,Y1},{D,D2}]),
+    D2.
+
+get_value({X,Y}, _Table)  when X < 1; Y < 1; X > 500; Y > 500 -> 'invalid';
+get_value({X,Y}, Table) ->
+%%        {C, V} = lists:keyfind(C, 1, Data),
+%%       V.
+    X1 =
+    case (X rem 100) of
+        0 -> 100;
+        A -> A
+    end,
+    Y1 =
+    case (Y rem 100) of
+        0 -> 100;
+        B -> B
+    end,
+    D = lists:nth(X1, lists:nth(Y1, Table)),
+    D1 = (D + ((X-1) div 100) + ((Y-1) div 100)) rem 9,
+    D2 =
+    case D1 of
+            0 -> 9;
+            C -> C
+    end,
+%%    io:format("~p -> ~p -> ~p~n", [{X,Y},{X1,Y1},{D,D2}]),
+    D2.
+
+get_neighbors({X,Y}, Data) ->
+    Neigbours = [{X+1,Y},{X-1,Y},{X,Y+1},{X,Y-1}],
+    lists:filter(fun({_,E}) -> E /= 'invalid' end, [{C, get_value(C, Data)} || C <- Neigbours]).
+
+build_graph(Coords, Data) ->
+       Graph = digraph:new(),
+       [digraph:add_vertex(Graph, C) || C <- Coords],
+       add_edges(Graph, Data, Coords).
+
+make_coords(Coords, Loop) ->
+       [{X  * Loop , Y * Loop} || {X,Y} <- Coords].
+
+add_edges(Graph, Data, []) -> Graph;
+add_edges(Graph, Data, [C|Rest]) ->
+    Neighbors = get_neighbors(C, Data),
+    [digraph:add_edge(Graph, C, X, V) || {X,V} <- Neighbors],
+    add_edges(Graph, Data, Rest).
+
+dijkstra(Graph,Start_node_name) ->
+    Paths = dict:new(),
+    Unvisited = gb_sets:new(),
+    Unvisited_nodes = gb_sets:insert({0,Start_node_name,root},Unvisited),
+    Paths_updated = loop_through_nodes(Graph,Paths,Unvisited_nodes),
+    Paths_updated.
+
+
+loop_through_nodes(Graph,Paths,Unvisited_nodes) ->
+    %% We need this condition to stop looping through the Unvisited nodes if it is empty
+    case gb_sets:is_empty(Unvisited_nodes) of
+        false -> 
+            {{Current_weight,Current_name,Previous_node}, Unvisited_nodes_updated} = gb_sets:take_smallest(Unvisited_nodes),
+              case dict:is_key(Current_name,Paths) of
+                false ->
+                    Paths_updated = dict:store(Current_name,{Previous_node,Current_weight},Paths),
+                    Out_edges = digraph:out_edges(Graph,Current_name),
+                    Unvisited_nodes_updated_2 = loop_through_edges(Graph,Out_edges,Paths_updated,Unvisited_nodes_updated,Current_weight),
+                    loop_through_nodes(Graph,Paths_updated,Unvisited_nodes_updated_2);
+                true ->
+                    loop_through_nodes(Graph,Paths,Unvisited_nodes_updated)
+           end;
+        true -> 
+            Paths
+    end.
+
+loop_through_edges(Graph,[],Paths,Unvisited_nodes,Current_weight) ->
+    Unvisited_nodes;
+
+loop_through_edges(Graph,Edges,Paths,Unvisited_nodes,Current_weight) ->
+    [Current_edge|Rest_edges] = Edges,
+    {Current_edge,Current_node,Neighbour_node,Edge_weight} = digraph:edge(Graph,Current_edge),
+    case dict:is_key(Neighbour_node,Paths) of
+        false ->
+            Unvisited_nodes_updated = gb_sets:insert({Current_weight+Edge_weight,Neighbour_node,Current_node},Unvisited_nodes),
+            loop_through_edges(Graph,Rest_edges,Paths,Unvisited_nodes_updated,Current_weight);
+        true ->
+            loop_through_edges(Graph,Rest_edges,Paths,Unvisited_nodes,Current_weight)
+    end.
diff --git a/day15/input.test b/day15/input.test
new file mode 100644 (file)
index 0000000..ab80887
--- /dev/null
@@ -0,0 +1,10 @@
+1163751742
+1381373672
+2136511328
+3694931569
+7463417111
+1319128137
+1359912421
+3125421639
+1293138521
+2311944581
diff --git a/day15/input.txt b/day15/input.txt
new file mode 100644 (file)
index 0000000..e3cdb36
--- /dev/null
@@ -0,0 +1,100 @@
+6919598227838199913855119231126554696792992136695118448313191841922775524417825151216891429923213541
+9837948917619787189935571922197132977185355128371858691255934311214863828372926993213996998139912118
+9712819911516295249274925911896922213911165843181262181868447395254293349493421938929117229988638933
+2476951876931175825312533142569137931721739713725799446851119715122115753938166842994429692731365577
+3691396937919199853599315812613951281125263711868971256541511653441136543245312424117567151668999674
+9294972854229491498411271321351998983919929272128753711198282397882539157287554149474186951291188213
+1135428214811459993621216218435832221856114139399136248687314682119118892393618575918692412341569411
+2929921158912711182518311271272489732271565129299979967487113812154971756485549913921162939114391994
+5735115881479239217981525718898929221191614222551693914913779439617933359521941311111857328992347824
+1324749414111313198724813856727141934339899617632138746132396173529281922347716643138211113979119317
+3263594941789195782731783751728147419342113519153994137499331274281145119193372917298632271917871525
+5479228191148416221119313323857818519129966727666999721348148577292717477671479138864751785262494159
+7796954213299199792437719283546721912268871561199118418913219166184792518146351339119739123994185981
+9629911853953289553892412891922852724926162185929113819714489559692115112436841311998121591212249911
+2328397217172391111288411918123121111329776726473176893942742194591917882178943718116232271214679483
+8741141538382111791315111111217879577863971972167912427154411516117129128518217911997124249159922143
+1112181727959169888722989121578121121151936427211518922174664147225132238128294879313132991431289481
+3747188357529199195611794131377544398823262189627134618228417441399177613261778632518726968681261168
+2481629985392983495331692197839119129614185425229598989835611557184114492293122195456638299631886877
+9147391211998228191112524911758739341511911573198239147932292971618769152596224169136819211191864121
+5339661183639791381383226488915834355926218173956911914294222986717347842733923158213319912313779114
+5243921116119511826295361157329514912924232445973183228141342999868999739939499749595714929628949881
+1717822297616989111681634446964759597245148141299649442375393616675299961783422585213969182389813979
+9223718278792883969179179831171225547156272493833129568562192687845341671181118782551343181335147329
+2949471233325124981953749291711218145971445981149411417976569225114219698356762679863952545995247124
+5813891161912133536215537881164329141679198579211417483995393171751229139515148551616451451699471962
+9119929859113721717292587192359667211794197356859166313415988219993559879467128483625838972963998323
+3895252513734165951664259349139491161549113632418318988572115611474628679584659333822336264442417161
+1321331549981649293931789932115621285146921639311329258269531211367526813124695472815288349283153957
+9422143143834321616229212182481242959489182311359152235561234325491988881677689362294958313949842287
+2933761954489328463913592166236523429629187282841912685667184712699837199911744132197611144273159928
+9154223693315635418997851912395881642814193154944229782123418943428143338499556944668943742669929821
+7139199155869873192534891585999171659544292341128472349512234761139621941897245995229121715921128221
+6121476298449615119179315279931141828241794783538353767662277163821548137442111115139516117411432154
+2721461614458352311522959322145231893925321191149936561182971937587678991778119969116199785178211249
+2487528361345231828917513711239639891526811157458691118988793531821939487349991122139681411829212122
+8196784988189916854591415361246889241677468137989661782462692224114367584511836128916614929335987963
+1428262253911238199139222239127987864548198367553991618567992112895712539846223268343291112612477881
+4311151291221542315119195139919638851217471142313611628999671999898223151846129413511721416129385444
+9223929419139196512617954741195118176182238863959367576997199569784421619179812898678142615193816961
+3739957281196232999991168188488998188924191959881122119917862162473281529652914854499515417543148211
+1169927398515215962874725929618422947517373431917599117392259119991783219193841762221551623313812112
+1779521141994763421294833117197299835113728789243129423866794976144315588151693917287864658143995545
+1159529199535138199525951914847892791117273918293125411365889356724137416981217218648382141469719663
+1441364449664913814639642968343767978911851669683136385812815592539216123382714745918827782372785978
+6318957234985141489118421889191571779911215699151212991414982923995699851169649235572492514461152781
+7412156483212717161922514861277814977411642186522941564119197181981973336314373991372153411986995122
+8897991112125241713878834617986429813629284553444782988769119316585492828131921114225955339181119115
+7125797134691266318245163258271753447285747349147564189981125236198721219161583235846326176241283216
+9525321171524757914385236188595897464111726911869954146959524288653932322972921175292381219559151292
+3194414111319217199913317211199349199115439486152468914947617154512189983891512878579271819721897263
+2491297212663627482329238221159172168981257191127613528687982291228575192766566677617649837761491989
+1121617881229121329144711662747663125911994277165915132843328895684121414218596361211122111151522917
+1162139629471896711146596151128354981329212568252314552651841992789919922154257915321939255343321996
+8957911396911314198222223175527176652889162319834385792196891372879635461922911398421776885879797419
+7322963541269954962232189653112791497811679512721118141634598993612942952829283995138219829599889896
+1569981859182313594573978911399869123929216923327226499129485126875325886837652725166648848111998112
+1288121399799771382611161143238967371619191852948117758798124866815182266412112994241352156467592687
+9782613685639942816392876435869597448311475929186556881281992238479474821817198267189176811611345259
+8297985291593249832442977492543196751332831189541119469439887728521432991519343374611228237815267521
+4523444992382282221121212292493497226928544945515424945782491119898752191347166219638988944397819329
+1194152129661126528417217522222823524312191626887412455713921129631844996221721711241299731957631929
+1781148994623342655141379243991813598498169894269285915113668982571141718118953985931825748891192931
+7833796895687353814141792216629798237329497911457911619894313529713318488672351472297297179132273189
+9926932996114129354986957691171917584919694139244291829925819193912511541921314292341872312211981115
+2226319158194127721821519888721116941969121519196797413362226317939127758283193749485693516626493951
+3879811393834677889446991895193312948783741921874992118657173911651519457119526249932991374694221158
+3978213824919914931146214425131552342993968699129184834945118939217714393215687129491255112693917989
+2158824391323818951718191147228251313192392116229926329592191642213389282938125431317515594129855892
+2451172759927711235717449878121951114556117149429163518191331765282139179182223474536877622144431352
+2329714311789716815472699771921951445911563498316498268435161242192511134612561842937129461433486692
+9129479162281325294261155111911112642211493851211871231213119335358891917149811789318556889897859341
+3131721472764729312915112753898151151419189179139342154299471832731815961421341134532764798679814835
+2217431514225191812445931219331361111219893144143242969585922236148982273386967781788171191337195416
+1488382763322171811791496513637271436981329581196884183931387898461131393171817212859131421121588819
+3919585217425217131651936339227398135223761428891742188679177616726195628645462878211341521251196219
+1912957445827151319119291131211879781559926431573113691921911897931126976153352174896222328435211744
+8323394392199159213314229519951213933192989466113182874918939393519799992338482811596611494443251292
+9984916453772991188215712124613985265583131698589662217749113142971842194779131373528882783188719171
+3913899817991926921213654613559196179434112197559381516917322458299126529795921728131229913442812145
+5261955321792184295644715972199271318912197112951581231816254223846887618715212638468619569563671225
+9917489994523968382612199487887184382861222418313922422473349162379172311619221982567741824957119114
+9968113457218183711391471141522113356149799928981688713318973119319334121489381212791911983159993251
+5249142618957491198134219822522879839256251251436381811362976119514169213884284373893443751176727125
+2392796498327162272298169491815314812269718889131918964451489919132342117242821633487187151658118395
+2249487296138221889993479576818554862713916146291439161279886228579321318212135384293111391332314217
+8897514868449987835128195533898941832793897879991713214683219511961725984113517647629518912372993316
+1222365183812221239236493126981329745983157171198118191517444419871498512716164392661878952149182524
+4629547769226193136881192975138212468396429891334928131276933521182542469561849129314987648579679814
+4749932538499119949794629935997432846571872718898849574149926819691699199813998733192839462193345835
+9591811516417941413919173999998125925256922487211975464577143226941827972121634167224618848544866381
+1199639929547111431756233793468711319694611716322117618413912797189249351723264174991798896126128931
+9148618989823822894126381717841681947679515811112512179728652138551112276241388773414937399838169113
+2116766171211521721492313143976327275198231584381986141259139981152183551411875421171911111231991471
+9989831137686686132681193914183774211627118849879121936974812614974731981977534928127129133558532219
+4741174282322196153167332691899826553963194736319492127196372119621459827218931141194633228429712327
+8911816487224543464638721721725597897179615822212948111174159112193164482824873198414218689949816972
+9435522297626213549132166931213828698726112979125659542928778787719513418334539737811551126112515196
+7992595817832629549819319199131117919697522116215291142239947517891729618121315294175432217519225146
+1211761163187331332248351681117516631676788555961569685827143481198812137751721875926238173159343258
diff --git a/day16/day16.erl b/day16/day16.erl
new file mode 100644 (file)
index 0000000..d861971
--- /dev/null
@@ -0,0 +1,179 @@
+%% to compile: erlc day3A.erl
+%% to run: erl -noshell -s day5 solve
+%%
+-module(day16).
+
+-compile(export_all).
+
+-export ([solve/0, solve/1, solve/2]).
+-export ([read_input/0]).
+
+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).
+
+read_input() ->
+    {ok, IO} = file:open("input.txt", 'read'),
+    {ok, Line}  = file:read_line(IO),
+    file:close(IO),
+    hexstr_to_bin(string:strip(Line, 'both', $\n)).
+
+hexstr_to_bin(S) ->
+  hexstr_to_bin(S, []).
+hexstr_to_bin([], Acc) ->
+  list_to_binary(lists:reverse(Acc));
+hexstr_to_bin([X,Y|T], Acc) ->
+  {ok, [V], []} = io_lib:fread("~16u", [X,Y]),
+  hexstr_to_bin(T, [V | Acc]);
+hexstr_to_bin([X|T], Acc) ->
+  {ok, [V], []} = io_lib:fread("~16u", lists:flatten([X,"0"])),
+  hexstr_to_bin(T, [V | Acc]).
+
+bin_to_hexstr(Bin) ->
+  lists:flatten([io_lib:format("~2.16.0B", [X]) ||
+    X <- binary_to_list(Bin)]).
+
+solution1(Input) ->
+  D = unpack(Input, [], not is_zero_bitstring(Input)),
+  io:format("Pkt: ~p~n", [D]),
+  count_versions(D, 0).
+
+solution2(Input) ->
+  D = unpack(Input, [], not is_zero_bitstring(Input)),
+  io:format("Pkt: ~p~n", [D]),
+  calc(D).
+
+calc({_,literal, V}) ->
+  V;
+calc({_,Op, V}) ->
+  ?MODULE:Op(V);
+calc([{_,Op, V}]) ->
+  ?MODULE:Op(V).
+
+sum(Args) ->
+  io:format("sum: ~p~n", [Args]),
+  lists:foldl(fun(X, Acc) -> calc(X) + Acc end, 0, Args).
+
+product(Args) ->
+  io:format("product: ~p~n", [Args]),
+  lists:foldl(fun(X, Acc) -> calc(X) * Acc end, 1, Args).
+
+min(Args) ->
+  io:format("min: ~p~n", [Args]),
+  lists:min([calc(X) || X <- Args]).
+
+max(Args) ->
+  io:format("max: ~p~n", [Args]),
+  lists:max([calc(X) || X <- Args]).
+
+lt([A,B] = Args) ->
+  io:format("lt: ~p~n", [Args]),
+  case calc(A) < calc(B) of
+    'true' -> 1;
+    'false' -> 0
+  end.
+
+gt([A,B] = Args) ->
+  io:format("gt: ~p~n", [Args]),
+  case calc(A) > calc(B) of
+    'true' -> 1;
+    'false' -> 0
+  end.
+
+eq([A,B] = Args) ->
+  io:format("eq: ~p~n", [Args]),
+  case calc(A) == calc(B) of
+    'true' -> 1;
+    'false' -> 0
+  end.
+
+count_versions([{Ver, Type, Rest}|T], Acc)  ->
+    count_versions(T, count_versions(Rest, Acc) + Ver);
+count_versions(_, Acc) ->
+  Acc.
+
+is_zero_bitstring(BitString) ->
+    Size = erlang:bit_size(BitString),
+    <<0:Size>> =:= BitString.
+
+unpack(Data, [], 'false') ->
+  Data;
+unpack(Input, Acc, 'false') ->
+  Acc;
+
+unpack(Input, Acc, 'true') ->
+  {{Ver, Type, Data}, Rest} = unpack(Input),
+  io:format("unpacking data: ~p~n", [Data]),
+  lists:reverse(unpack(Rest, Acc ++ [{Ver, Type, unpack(Data, [], is_bitstring(Data) andalso not is_zero_bitstring(Data))}], not is_zero_bitstring(Rest))).
+
+%% Literal
+unpack(<<V:3, 4:3, Rest/bitstring>>) ->
+  io:format("V:~p, T:literal~n", [V]),
+  unpack_literal(V, Rest);
+
+%% Operator length type id 0
+unpack(<<V:3, T:3, 0:1, Rest/bitstring>>) ->
+  io:format("V:~p, T:operator I:0~n", [V]),
+  unpack_operator_0(V, T, Rest);
+
+%% Operator length type id 1
+unpack(<<V:3, T:3, 1:1, Rest/bitstring>>) ->
+  io:format("V:~p, T:operator I:1~n", [V]),
+  unpack_operator_1(V, T, Rest).
+
+unpack_literal(V, Pkt) ->
+    unpack_literal(V, Pkt, <<>>, 0). 
+
+unpack_literal(V, <<1:1, Value:4, Rest/bitstring>>, Acc, Count) ->
+    unpack_literal(V, Rest, <<Acc/bitstring, Value:4>>, Count + 1);
+unpack_literal(V, <<0:1, Value:4, Rest/bitstring>>, Acc, Count) ->
+    L = <<Acc/bitstring, Value:4>>,
+    Size = (Count + 1) * 4,
+    io:format("unpack_literal value: ~p size:~p rest:~p~n", [Value, Size, Rest]),
+    <<X:(Size)>> = L,
+    {{V, literal, X}, Rest}. 
+
+unpack_operator_0(V, T, <<Length:15, Rest0/bitstring>>) ->
+    io:format("operator0 length:~p~n", [Length]),
+    <<Pkt:(Length)/bitstring, Rest/bitstring>> = Rest0,
+    {Rest1, Decoded} = unpack_len(Length, Rest0, []),
+    {{V, operator_type(T), Decoded}, Rest1}.
+
+unpack_operator_1(V, T, <<Num:11, Rest/bitstring>>) ->
+    {Rest1, Decoded} = unpack_count(Num, Rest, []),
+    io:format("operator1 num of packets:~p Decoded:~p, Rest:~p~n", [Num,Decoded,Rest1]),
+    {{V, operator_type(T), Decoded}, Rest1}.
+
+unpack_len(0, Pkt, Acc) ->
+  {Pkt, Acc};
+unpack_len(Length, Pkt, Acc) ->
+  {L, R}   = unpack(Pkt),
+  io:format("unpack_len: length:before ~p after: ~p~n", [Length, Length - bit_size(Pkt) + bit_size(R)]),
+  unpack_len(Length - bit_size(Pkt) + bit_size(R), R, Acc ++ [L]).
+
+unpack_count(0, Pkt, Acc) ->
+  {Pkt, Acc};
+unpack_count(Num, Pkt, Acc) ->
+  {L, R} = unpack(Pkt),
+  unpack_count(Num - 1, R, Acc ++ [L]).
+
+operator_type(0) -> 'sum';
+operator_type(1) -> 'product';
+operator_type(2) -> 'min';
+operator_type(3) -> 'max';
+operator_type(5) -> 'gt';
+operator_type(6) -> 'lt';
+operator_type(7) -> 'eq'.
diff --git a/day16/input.txt b/day16/input.txt
new file mode 100644 (file)
index 0000000..3528c00
--- /dev/null
@@ -0,0 +1 @@
+420D50000B318100415919B24E72D6509AE67F87195A3CCC518CC01197D538C3E00BC9A349A09802D258CC16FC016100660DC4283200087C6485F1C8C015A00A5A5FB19C363F2FD8CE1B1B99DE81D00C9D3002100B58002AB5400D50038008DA2020A9C00F300248065A4016B4C00810028003D9600CA4C0084007B8400A0002AA6F68440274080331D20C4300004323CC32830200D42A85D1BE4F1C1440072E4630F2CCD624206008CC5B3E3AB00580010E8710862F0803D06E10C65000946442A631EC2EC30926A600D2A583653BE2D98BFE3820975787C600A680252AC9354FFE8CD23BE1E180253548D057002429794BD4759794BD4709AEDAFF0530043003511006E24C4685A00087C428811EE7FD8BBC1805D28C73C93262526CB36AC600DCB9649334A23900AA9257963FEF17D8028200DC608A71B80010A8D50C23E9802B37AA40EA801CD96EDA25B39593BB002A33F72D9AD959802525BCD6D36CC00D580010A86D1761F080311AE32C73500224E3BCD6D0AE5600024F92F654E5F6132B49979802129DC6593401591389CA62A4840101C9064A34499E4A1B180276008CDEFA0D37BE834F6F11B13900923E008CF6611BC65BCB2CB46B3A779D4C998A848DED30F0014288010A8451062B980311C21BC7C20042A2846782A400834916CFA5B8013374F6A33973C532F071000B565F47F15A526273BB129B6D9985680680111C728FD339BDBD8F03980230A6C0119774999A09001093E34600A60052B2B1D7EF60C958EBF7B074D7AF4928CD6BA5A40208E002F935E855AE68EE56F3ED271E6B44460084AB55002572F3289B78600A6647D1E5F6871BE5E598099006512207600BCDCBCFD23CE463678100467680D27BAE920804119DBFA96E05F00431269D255DDA528D83A577285B91BCCB4802AB95A5C9B001299793FCD24C5D600BC652523D82D3FCB56EF737F045008E0FCDC7DAE40B64F7F799F3981F2490
\ No newline at end of file
index 393c99186c7afe8a79e53c62f6416d07ec6d05e7..b2194c3331eca8f1e0c30d0d03f4d14c5699410e 100755 (executable)
Binary files a/day3/day3 and b/day3/day3 differ
diff --git a/day5/day5 b/day5/day5
new file mode 100755 (executable)
index 0000000..c531a5d
Binary files /dev/null and b/day5/day5 differ