diff --git a/README.md b/README.md index 1f01100..0a38fd5 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,6 @@ I'll probably try to vary languages a bit to get some variety. ### [Day 7](day07/) **Language**: JavaScript (Node) -**Note**: Yes, I used JSDoc for type-hints. But I think only insane people use JavaScript without JSDoc. - ### [Day 8](day08/) **Language**: Bash @@ -47,4 +45,7 @@ I'll probably try to vary languages a bit to get some variety. **Language**: Rust ### [Day 10](day10/) -**Language**: Lua \ No newline at end of file +**Language**: Lua + +### [Day 11](day11/) +**Language**: Java (17) \ No newline at end of file diff --git a/day11/.gitignore b/day11/.gitignore new file mode 100644 index 0000000..5241a72 --- /dev/null +++ b/day11/.gitignore @@ -0,0 +1 @@ +*.class \ No newline at end of file diff --git a/day11/eg.txt b/day11/eg.txt new file mode 100644 index 0000000..a0bda53 --- /dev/null +++ b/day11/eg.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file diff --git a/day11/input.txt b/day11/input.txt new file mode 100644 index 0000000..1d2dc25 --- /dev/null +++ b/day11/input.txt @@ -0,0 +1,140 @@ +.....#..........#...........................................#................#.....................................#...............#........ +.........................................#...........................................#........................................#............. +..........................#........................................................................#........................................ +..................................................#.......................................#.............................#................... +..........................................................#............................................................................#.... +.......................................#............................#...........................#........................................... +...................................................................................#..................#............#........................ +...#..........#.....#.......................................................#................................#................#............. +................................................................#........................................................................... +.........................#.....................#......................#..................................#...........................#...... +..................................#.........................................................#............................................... +...........................................#................................................................................................ +............................................................#............................................................................#.. +........#..................#.............................................#......................#..................#........................ +....................................#...............#...............#........................................................#.............. +..................................................................................#............................#............................ +.#...........#......#....................................#.......................................................................#.......... +.........................................#............................................#.................#............#...................... +.................................#.............................................#...........................................#..............#. +....#....................................................................................................................................... +.........................#.............................#..........#.....#...................................#............................... +............................................................#....................................#.......................................... +.................#............#...................................................................................................#......... +......................................#...............................................#...............#.................................#... +...............................................................#..................................................#.....#.....#............. +#.....................#..............................#...........................#.......................................................... +...............................................#.....................#...............................................................#...... +.........................................#...............#..................................#............................................... +......#............................#........................................................................................................ +..........................................................................#...........................................#..................... +..................#...................................#.............................#..............#........#...................#........... +...........................#......................................#......................................................................... +..............................................#...........#....................................#...........................#..............#. +....#..................................#.................................................................................................... +................#.....#.....................................................#.............................#........#........................ +...............................................................#......#..................................................................... +............................................#..................................................................................#............ +.......#........................#....................................................................................................#...... +..............................................................................#...............#...........................#................. +.....................#.........................#............................................................................................ +..........................#.................................#.....#...............................................#......................... +...................................................................................................#..............................#......... +..................#...............#.......#............#.................................................................................... +.........................................................................................#....................#...............#............. +............#............................................................................................................................#.. +......................#......................#..........................#......#..........................................#................. +............................................................................................................................................ +...#...............................................................#..............................................#......................... +..........................................#..................#.............#..........................................................#..... +..................#............................#............................................................................................ +......................................................#.................................#.........#......................................... +......................................#.................................#.........#........................#................................ +#..........................#..........................................................................#....................#................ +...........#................................#...............................#........................................#.............#........ +...................................#....................................................................................................#... +............................................................................................................................................ +...............#.........................#.................................................................................................. +.................................................................#.......#......#........................................................... +#.....................................................................................#........................................#............ +.........................#...........................................................................#...............................#...... +....................#..................#..........#.........#............................................................................... +..........#......................#.........................................................................#...............#................ +.............................................#................................................#............................................. +......#...............................................#........................#...................................................#........ +............................................................................................................................................ +..............................................................#..........................#.................................................. +............#.....#....................#...........................................#........................................................ +..#................................................#......#....................................................#............................ +..........................#...................................................................................................#..........#.. +....................................................................#....................................................................... +....................#.........................#........#...........................................................................#........ +#.......................................#..........................................................#.....#..........#....................... +............................................................#............#............#..................................................... +............................................................................................................................................ +............................................................................................................#........................#...... +..............................#..............................................#.........................#.....................#.............. +.......#..............#.............#...............#....................................................................................... +............................................................................................................................................ +..................#......................................................#....................#................#............................ +...........................#.....#......#........................#.............................................................#............ +............................................................................................................................................ +#...................................................................................#......................#..........#................#.... +............#..........#......................#............................................................................................. +.............................................................#..........#.........................................................#......... +....#.............#......................................................................................................................... +................................#......#.........................................#..................#...........#..........#................ +............................................................................................................................................ +#..............#......................................#...............................................................#..................... +..........#..................................#..............................#............................................................... +............................................................................................................................................ +.................................................#..............#....................#.........#.......................................#.... +............................................................................................................................................ +............................................................................................................................................ +.............#............#................................#.........#......................#.............#................#................ +#..............................................#................................#........................................................... +..........................................#...............................#.......................................#......................#.. +..............................#........................#..........#......................................................................... +.........#.......#.......................................................................................................................... +.........................#................................................................#...................................#............. +.............................................................................#.....................................................#........ +....................................................................................................#....................................... +......#.....................#.....................................................................................#......................... +......................................#...................#...............................................................#................. +......................................................................#.....................#............................................#.. +..#...............................#............#...............#...................#..............................................#......... +..............#..........................#.............#....................................................#.........#..................... +...............................................................................................#............................................ +........................#.........................#...............#...........................................................#............. +.........#.................................................................................................................................. +.....................................#...............................................................#...................................... +.........................................................#..................#...................................#........................... +.................#.......................................................................................................................... +..............................#.................#....................#...................#...............................#.................. +...............................................................................................#.................................#.......... +.........#..........................................#.....................................................#................................. +....#................#............#..............................................#.......................................................... +..........................#............#.............................................................................#...................... +...............#...............................................#...........#...................................................#............ +.......................................................................................................................................#.... +...............................#...............#.....................................#............#......................................... +..#.........................................................................................................#............................... +...........................#...............#..............................................#..............................#.................. +................#......................................#.........................................................................#.......... +.......................#..............#...............................#..................................................................... +...............................................................#................#.........................#.........#....................... +...............................#............................................................#......#........................................ +......#............#....................................................................................................#................... +...........................#..........................................................#................................................#.... +....................................................#..................#...............................#.................................... +..............................................................................#............................................................. +...#............................#..............................#..................................#.................#........#.............. +....................................................................#.........................................#............................. +...........#.....#...............................#..........................................#............................................... +.....................................#...........................................................................................#.......... +....................................................................................................#...................................#... +.....#..................#.....................#............................#......#...............................#........#................ +.............#...................................................#.............................#............................................ +..............................#...........................#.............................................#..................................# +......................................#...............................................#........................................#............ +...................#......#........................#.................#................................................#..................... \ No newline at end of file diff --git a/day11/solution.java b/day11/solution.java new file mode 100644 index 0000000..db2e0b1 --- /dev/null +++ b/day11/solution.java @@ -0,0 +1,90 @@ +import java.io.IOException; +import java.math.BigInteger; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; + +class Solution { + public static void main(String[] args) throws IOException { + String contents = Files.readString(Path.of("input.txt")); + String[] lines = contents.split("\n"); + List galaxies = new ArrayList(); + SortedSet xs = new TreeSet(); + SortedSet ys = new TreeSet(); + for (int x = 0; x < lines[0].length(); x++) { + xs.add(x); + } + for (int y = 0; y < lines.length; y++) { + ys.add(y); + for (int x = 0; x < lines[y].length(); x++) { + char c = lines[y].charAt(x); + if (c == '#') { + galaxies.add(new Galaxy(x, y)); + ys.remove(y); + xs.remove(x); + } + } + } + + // Part 1 + List galaxiesPart1 = expandSpaces(galaxies, xs, ys, 2); + int sum1 = 0; + for (Galaxy g1 : galaxiesPart1) { + for (Galaxy g2 : galaxiesPart1) { + sum1 += g1.distance(g2); + } + } + System.out.println(String.format("Part 1: %d", sum1 / 2)); + + // Part 2 + List galaxiesPart2 = expandSpaces(galaxies, xs, ys, 1000000); + BigInteger sum2 = BigInteger.ZERO; + for (Galaxy g1 : galaxiesPart2) { + for (Galaxy g2 : galaxiesPart2) { + int d = g1.distance(g2); + sum2 = sum2.add(BigInteger.valueOf(d)); + } + } + System.out.println(String.format("Part 2: %d", sum2.divide(BigInteger.TWO))); + } + + private static List expandSpaces(Iterable galaxies, Iterable xs, Iterable ys, int times) { + List newGalaxies = new ArrayList(); + for (Galaxy g : galaxies) { + int dx = 0; + int dy = 0; + for (Integer x : xs) { + if (x < g.x) { + dx += times - 1; + } else { + break; + } + } + for (Integer y : ys) { + if (y < g.y) { + dy += times - 1; + } else { + break; + } + } + newGalaxies.add(new Galaxy(g.x + dx, g.y + dy)); + } + return newGalaxies; + } +} + +class Galaxy { + public int x; + public int y; + public Galaxy(int x, int y) { + this.x = x; + this.y = y; + } + + public int distance(Galaxy other) { + return Math.abs(other.x - x) + Math.abs(other.y - y); + } +} \ No newline at end of file