diff --git a/11/input.txt b/11/input.txt new file mode 100644 index 0000000..3a2450b --- /dev/null +++ b/11/input.txt @@ -0,0 +1,140 @@ +..........................#.........................#........................................................#.............................. +.............#.....................................................#.........#......................#....................................... +..................................#....................................................................................#...........#........ +....................#..........................#..................................................................#......................... +.......................................................#...............................................................................#.... +.....................................#.........................#......#..............#...................................................... +........#...............#......................................................#..........#................................#...............# +.........................................#................#..............................................#............#..................... +....#............................#.......................................................................................................... +............#......................................#..............................................#............#............................ +......................#.....................................................................#............................................... +.............................................................................#........#.........................................#........... +.................#.....................................................#.................................................#..............#... +................................#....................#.................................................#.................................... +.......................................#.............................................................................#...................... +..#.....................#................................................................................................................... +............................................#.....................#...........#.....................#....................................... +..................................................................................................................#..................#...... +....................................#....................#................................................................................#. +.........#.....................................#..............#......................#......................................#............... +..............#..........................................................#......................#.....#..................................... +.......................................................................................................................................#.... +..................................................................#.........................#...................#........................... +....#............................#.....#.....................................#................................................#............. +.......................#..........................#......................................................................................... +..................................................................................#.....#...................#.............#................. +................#.............................................#.................................#........................................... +#.....................................................................................................#...................................#. +...........#..............#.....#.............#.....................................................................#....................... +...........................................................................#................#.....................................#......... +...................................................#........................................................................................ +..............#....................................................................................#........................................ +.....................................#................................................#.......................#............................. +.....................................................................................................................................#.....# +........#................#....................................#.................................#.........#................................. +....................................................#..............#............#....................................#...................... +....#...............#........#..........................................................#.......................................#........... +..........................................#.....#.......................#..........................#.....................................#.. +.......................................................#.................................................................................... +#......#...................................................................................................................#................ +............#...............................................................................#..........#..........................#......... +............................................................................................................#............................... +.....................................#....................#........#............................#........................................... +........................#...........................................................#................................#....................#. +......#...............................................#.....................#........................#...................................... +.................................#.....................................#..................................................#................. +...................#......................#..................#..........................#.............................................#..... +.............................#.....................#..............#...........................................#............................. +.........................................................#.......................................................................#.......... +...............................................................................#.......................#.................................#.. +............................................................................................................................................ +............................................................................................................................................ +.................#.............................................................................................#.......#.............#...... +....#.....#....................................................#........#...............#....................................#.............. +..................................#..........#........................................................#..................................... +............................................................................................................................................ +...................#..............................................................................#......................#.................. +............................................................................................................................................ +..#..............................................#......#.....#...........................#..............#....................#............. +................#......#.........#.......................................................................................................... +.........................................................................#......................................#.........................#. +....................................................#..............#............................#........................................... +.....#....................#..........................................................................#...................................... +............#...........................#......#............................................................................................ +............................................................................................................................................ +.....................#.........................................................#.........................#........#.............#........... +.........#..............................................................#.....................#..........................................#.. +......................................#...........#.....................................#................................................... +.#...............................#.......................................................................................................... +..............#............#....................................#..................................#........................#............... +.....................................................................#...........................................#.......................... +............................................................................................................................................ +.........#........#........................#.....#........................#.......................................................#.....#... +............................................................................................................................................ +...................................#........................................................................................................ +..............#..........#..............#....................................#..........#.....#............................................. +....#........................................#............#...................................................................#............. +............................................................................................................................................ +#........#......................................................#........#.................................................................# +....................#.........#..........................................................................#.......#......#................... +............................................................................................................................................ +.......................................#..............#...........................................#......................................... +................................................#.............................................................#.......................#..... +.......................#...................#..............................#................................................................. +...............#...................#....................................................#.............................#..................... +....#......................................................................................................#................................ +.............................#.........................................#....................#.............................#.....#........... +..............................................#..................................................#...................................#...... +...................#...................#..........................................#.................................#....................... +............#.....................#....................#..................#................................................................. +...........................................#........................................................#....................................... +...........................................................................................................#................................ +...#............................................................#.............................................................#............. +.........................#..............................................................#................................................#.. +...............#............................................................................................................................ +.......................................#.......#.............#.............................................................................. +.....................#...........#.............................................#............#.................#.........#...........#....... +..........................................................................#.......................#......................................... +.......#............................................................................#....................................................... +....................................................#....................................................................................#.. +.................#........................................#.....#...........................................................#............... +.#.........#..................................#.................................#..............#.........#.....#......#..................... +.....................#............#.......................................................#.........#....................................... +............................................................................#.......................................................#....... +.....................................................................................#....................................#................. +............................#................................................................#..................................#........... +.....#.........................................................#............................................................................ +......................#..................#............................................................................#..................... +................................................................................#.....................................................#..... +#.................................#.......................................#................................................................. +...................#......#.......................#...............#...........................................#............................. +.............................................................#........................................#......................#.............. +.................................................................................................#.......................................... +..#..................................#....................................................................#......#..................#....... +..........................................................#..................#........................................#..................... +..................#........#........................................................#..........................................#............ +.................................#.............#.......................#...................................................................# +................................................................................................#.........................#................. +....#.....#................................................................................................................................. +.......................#....................................................#............................................................... +....................................................#....................................................................................... +.............#..............................#..........................................#.................................................... +...............................#............................................................................................#........#...... +.#.......................................................#.........#...........#.................................#.......................... +....................................#.............#..............................................................................#.......... +.....................#.........................................................................#......#..................................... +..........................#...........................#.........#.................#......................................................... +..........#.................................#................................#.............................................................. +....#...........................................................................................................#..................#........ +........................................................................#..............#.....................................#.............. +..................#.......................................#................................................................................. +........................................................................................................................................#... +....................................................#.......................#..........................#.................................... +.....#..............................#.........................#..................................#.......................................... +..........................#............................................#........................................#.......#................... +..........................................#................................................................................................. +......................................................................................................................................#..... +.......#...........................................#................................#........................................#.............. +#..................#..........................#..........................#.....#............................#............................... +...................................#.....................................................................................#.................. diff --git a/11/part1.pl b/11/part1.pl new file mode 100644 index 0000000..8d1e0a0 --- /dev/null +++ b/11/part1.pl @@ -0,0 +1,34 @@ +:- table galaxy/2. +:- table no_galaxy_row/2. +:- table no_galaxy_col/2. +:- use_module(library(pio)). +:- initialization(start, main). + +start :- + input('input.txt', Map), + findall(Dist, distance(Map, _, _, Dist), Dists), + sum_list(Dists, Answer), + writef('Answer=%w\n', [Answer]). + +distance(Map, X1-Y1, X2-Y2, Dist) :- + galaxy(Map, X1-Y1), + galaxy(Map, X2-Y2), + (X1 < X2; X1 =:= X2, Y1 < Y2), + findall(X, no_galaxy_row(Map, X), EmptyXs), + findall(Y, no_galaxy_col(Map, Y), EmptyYs), + include(between(X1, X2), EmptyXs, ExpandedX), length(ExpandedX, RowsToAdd), + include(between(Y1, Y2), EmptyYs, ExpandedY), length(ExpandedY, ColsToAdd), + Dist is abs(X1 - X2) + abs(Y1 - Y2) + RowsToAdd + ColsToAdd. + +between(End1, End2, N) :- End1 < N, N < End2; End1 > N, N > End2. +no_galaxy_row(Map, X) :- nth0(X, Map, Row), maplist([46]>>(true), Row). +no_galaxy_col(Map, Y) :- maplist({Y}/[Row]>>(nth0(Y, Row, 46)), Map). + +galaxy(Map, X-Y) :- nth0(X, Map, Row), nth0(Y, Row, 35). + +input(FileName, Map) :- phrase_from_file(lines(Map), FileName). +lines([]) --> eos, !. +lines([Line|Lines]) --> line(Line), lines(Lines). +eos([], []). +line([]) --> ( "\n" ; eos ), !. +line([L|Ls]) --> [L], line(Ls). diff --git a/11/test.txt b/11/test.txt new file mode 100644 index 0000000..986aad4 --- /dev/null +++ b/11/test.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#.....