diff --git a/Makefile b/Makefile index a76ca28..b929c50 100644 --- a/Makefile +++ b/Makefile @@ -98,6 +98,16 @@ day10part2: @echo ./build/day10part2 +day11part1: + fpc src/day11/part1.pas -obuild/day11part1 + @echo + ./build/day11part1 + +day11part2: + fpc src/day11/part2.pas -obuild/day11part2 + @echo + ./build/day11part2 + clean: rm build/* \ No newline at end of file diff --git a/resources/day11.txt b/resources/day11.txt new file mode 100644 index 0000000..21b77be --- /dev/null +++ b/resources/day11.txt @@ -0,0 +1,140 @@ +.....#...............#......................................................................#.............................#..........#...... +........................................#............................#......#............................................................... +..............................#..............#....................................#..............................................#.......... +..............#........................................................................#.................................................... +..#...........................................................................................#............................................. +..................#.....................................#.....#......................................#.........#.......#.................... +...........#......................................#.....................#...........#................................................#...... +.............................#............................................................#.....................................#........... +.....#..................#.................#.........................#......................................#................................ +....................................#.................#........................#..........................................................#. +................#..............................#....................................................#....................................... +..............................................................#.........................#.......................#.....................#..... +............#...............#......................#.....................#..............................#................................... +......................................#...............................................................................#..................... +.........................................................#.................................................................................. +..............................................#............................................................................................. +..#.................#.....................................................................#.....#..........#..............#......#.......... +..............................................................................#............................................................. +..........................................#.........................#..............................................#........................ +.....#........#.............#...........................#...........................................#....................................... +..................................#...............#.............#.................#..........................#.....................#........ +.......................................................................#.................................................................... +...............................................................................................................................#.........#.. +#.......................................#....................................................#.........................#.................... +........#...............#....................................#...........................................#.................................. +...............................#............................................................................................................ +.........................................................#.................................................................................. +..#..........................................#........................................#.............#............................#.......... +......................#............................#.................#........#............................................................. +.........#.......#...........................................................................#.....................#.........#.........#.... +................................#............................................................................#.............................. +.........................................................................................#.................................................. +.#.........................................#.........#..........#.......................................................#................... +....................#.................#...............................................................#..................................... +............................................................................#............................................................... +........................#.........................................................................................#.................#....... +.............#.......................................................#......................................................#............... +.......#.................................#.....#..............................................................#............................. +#...............................#...............................................................#.....................#..................... +.....................................#...................#.......................#.....#.......................................#............ +..........................#.................#......#...................................................................................#.... +..............#............................................................................................................................. +...#..........................................................................#............................................#................ +.............................................................................................#.....................#......................#. +.......................................#..........................................................................................#......... +.......................................................................................................#.................................... +.....#.........#.................#.................#............#.........................#.................#............................... +............................#.....................................................#......................................................... +...........................................#................................................................................................ +....................................#.......................#.........................#............................#.....#.................. +............................................................................................................................................ +.....................#..................#................................#......#.....................................................#..... +.......................................................#.........#...............................#.....#.................................... +............#.................................................................................................................#............. +..............................................#..............#.......#.................................................#.................... +................................................................................................................#........................... +.........#.............................................................................#............#....................................... +.............................#...........................................#.....#............................................................ +.....#..........#.....#...........#.............#.....#.............................................................................#....... +...........................................#............................................................#.....#............................. +......................................#.........................#.........................#.....................................#........... +............................................................................................................................................ +.......#.....#.....#........................................#.................................#............................................. +.........................................#................................................................#.....#......#.................... +........................#.......#..................#.................................#.......................................#.......#...... +..............................................#............................................................................................. +...............................................................................#............................................................ +......................................................#..........#.......................................................................... +...#......#.................#.............#.............................#..........................#...............................#........ +.............................................................#........................#..........................#.........#................ +......................................#....................................................#.............................................#.. +............................................................................................................................................ +...............#......#.......................#.....#.......................................................#............................... +.#......#.......................#.........................#.......#....................................#.................................... +...............................................................................................#........................#......#.......#.... +............#...............#............#....................#........#..........#......................................................... +...............................................................................................................#............................ +..........................................................................................................#.................#............... +......................#........#.........................................................................................................#.. +....................................................................................#.....#.....#....................#...................... +.#.........#...............#........................................#...............................................................#....... +..................#......................................................#..........................#....................................... +.................................#.............#................................................................#.......#................... +........................#......................................................#..............#................................#............ +.....#..........................................................#.....#.................#..................................................# +.....................................#......#.........#...............................................#..................................... +............................................................................................................................................ +............................................................................................................................................ +.................................#..........................................#................#...................#..............#........... +.......#....................#............#.............................................#...................................#................ +..#................#........................................#.......#..............................#........................................ +........................#...........................................................................................................#....... +...........#..........................................#.........................#......................#.................................... +...........................................#................................................................................................ +......#...............................................................................#.........#........................................... +............................#.....................#................#........................................#...........#................... +..................#.........................................#............................................................................... +........................#...............#................................#.......#..............................#............#.............. +............#......................#.................#..................................................#..................................# +..#................................................................................................#........................................ +.........................................................................................................................#.................. +..................................................................#................................................#................#....... +.......#.....................................#.............................................................................................. +.................#...............#......#.....................#............................................................................. +....................................................................................#..................................#.................... +..............................................................................................#............#................................ +....#......................#................................................................................................................ +......................................................#.................................#..........................#.............#.......... +..........#.................................................................................................................#............... +.................................#..........#..................................#............................................................ +.......................................................................................................................................#.... +.......................................#..................................................................#......#.......................... +.............................#.........................................................#.......#............................................ +.........#....................................#............#.................................................................#.............. +...#...............................................................................#.................#...................................... +..................................................................................................................................#......... +....................#.............#...................................................................................#....................# +....................................................#...........#...........#............................................................... +.........................................................................................................#.................................. +..........#...............................#............................#.................................................................... +.............................................................#....................#.............................#..............#............ +.#...............#..........................................................................#..........................................#.... +............................#............................................................................................................... +............#......................................#............#..................................................................#........ +.......#...............................................................................................................#.................... +..................................#.......#..........................#........#...............#..........#....................#............. +....................#....................................#............................#..........................#.......................#.. +..............................................................#............................................................................. +..#............................#............................................................................#............................... +..............#.....................#...............................................................................................#....... +..........................#........................................#....................................#...........#....................... +..........................................#.................................#.................#............................................. +................................................#...........#..........#.....................................................#.............. +.........#.................................................................................................................................. +...................................................................................#............................#.......#................... +....#.............................#.............................#.......................................................................#... +.........................................#.........................................................#........................................ +....................#...........................................................................................................#........... +..............#........................................#..................................#...................#......................#...... +.........#.................#........................................#...........#........................................#.................. \ No newline at end of file diff --git a/resources/day11sample.txt b/resources/day11sample.txt new file mode 100644 index 0000000..a0bda53 --- /dev/null +++ b/resources/day11sample.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file diff --git a/src/day11/part1.pas b/src/day11/part1.pas new file mode 100644 index 0000000..d4f9ee6 --- /dev/null +++ b/src/day11/part1.pas @@ -0,0 +1,113 @@ +program day11part1; +uses sysutils; + + +function dist_between(x1, x2: int32; empty_list: array of boolean): int32; +var + i: int32; +begin + + { swap operands so x1 is always smaller} + if (x2 < x1) then + begin + i := x2; + x2 := x1; + x1 := i; + end; + + { calculate distance } + dist_between := 0; + for i := x1 to x2 - 1 do + begin + + dist_between := dist_between + 1; + + { if gap, increment by an additional 1 } + if empty_list[i] then + dist_between := dist_between + 1; + + + end; +end; + +function galaxy_dist(g1, g2: array of int64; row_list, col_list: array of boolean): int32; +begin + galaxy_dist := dist_between(g1[0], g2[0], col_list) + dist_between(g1[1], g2[1], row_list); +end; + +var + file_: text; + + str: string; + + galaxies: array[0..1000, 0..1] of int64; + galaxy_count: int32; + + column_empty: array[1..1000] of boolean; + row_empty: array[1..1000] of boolean; + + i, j: int32; + count: int32; + + sum: int32; + +begin + { open inp.txt for reading } + assign(file_, 'resources/day11.txt'); + reset(file_); + + { set row/cols to empty } + for i := 0 to 1000 do + begin + column_empty[i] := true; + row_empty[i] := true; + end; + + count := 0; + galaxy_count := 0; + + { read file } + while not EOF(file_) do + begin + + count := count + 1; + + readln(file_, str); + + for i := 1 to length(str) do + begin + + if str[i] = '#' then + begin + + { record this galaxy } + galaxy_count := galaxy_count + 1; + + galaxies[galaxy_count][0] := i; + galaxies[galaxy_count][1] := count; + + { mark rows/cols as nonempty } + row_empty[count] := false; + column_empty[i] := false; + + end; + + end; + end; + + sum := 0; + { for each pair } + for i := 1 to galaxy_count do + begin + for j := i + 1 to galaxy_count do + begin + + { sum the pairwise distance } + sum := sum + galaxy_dist(galaxies[i], galaxies[j], row_empty, column_empty); + + end; + end; + + writeln('The sum is ', sum); +end. + diff --git a/src/day11/part2.pas b/src/day11/part2.pas new file mode 100644 index 0000000..71ce015 --- /dev/null +++ b/src/day11/part2.pas @@ -0,0 +1,113 @@ +program day11part1; +uses sysutils; + + +function dist_between(x1, x2: int32; empty_list: array of boolean): int64; +var + i: int32; +begin + + { swap operands so x1 is always smaller} + if (x2 < x1) then + begin + i := x2; + x2 := x1; + x1 := i; + end; + + { calculate distance } + dist_between := 0; + for i := x1 to x2 - 1 do + begin + + dist_between := dist_between + 1; + + { if gap, increment by an additional 999999 } + if empty_list[i] then + dist_between := dist_between + 999999; + + + end; +end; + +function galaxy_dist(g1, g2: array of int64; row_list, col_list: array of boolean): int64; +begin + galaxy_dist := dist_between(g1[0], g2[0], col_list) + dist_between(g1[1], g2[1], row_list); +end; + +var + file_: text; + + str: string; + + galaxies: array[0..1000, 0..1] of int64; + galaxy_count: int32; + + column_empty: array[1..1000] of boolean; + row_empty: array[1..1000] of boolean; + + i, j: int32; + count: int32; + + sum: int64; + +begin + { open inp.txt for reading } + assign(file_, 'resources/day11.txt'); + reset(file_); + + { set row/cols to empty } + for i := 0 to 1000 do + begin + column_empty[i] := true; + row_empty[i] := true; + end; + + count := 0; + galaxy_count := 0; + + { read file } + while not EOF(file_) do + begin + + count := count + 1; + + readln(file_, str); + + for i := 1 to length(str) do + begin + + if str[i] = '#' then + begin + + { record this galaxy } + galaxy_count := galaxy_count + 1; + + galaxies[galaxy_count][0] := i; + galaxies[galaxy_count][1] := count; + + { mark rows/cols as nonempty } + row_empty[count] := false; + column_empty[i] := false; + + end; + + end; + end; + + sum := 0; + { for each pair } + for i := 1 to galaxy_count do + begin + for j := i + 1 to galaxy_count do + begin + + { sum the pairwise distance } + sum := sum + galaxy_dist(galaxies[i], galaxies[j], row_empty, column_empty); + + end; + end; + + writeln('The sum is ', sum); +end. +