Browse Source

Add day 11 part i solution

ApisNecros 1 year ago
parent
commit
3cbafc4747
4 changed files with 470 additions and 0 deletions
  1. 242 0
      11/11_1.ts
  2. 44 0
      11/demo_1.out
  3. 44 0
      11/demo_2.out
  4. 140 0
      inputs/11.txt

+ 242 - 0
11/11_1.ts

@@ -0,0 +1,242 @@
+import { Vector2 } from "../3/3_common.ts";
+import { LoadInput, Inspect } from "../common.ts";
+
+async function main() {
+    const tests = [
+        '...#......',
+        '.......#..',
+        '#.........',
+        '..........',
+        '......#...',
+        '.#........',
+        '.........#',
+        '..........',
+        '.......#..',
+        '#...#.....'
+    ];
+
+    const test_reddit_1 = [
+        "...#......#....",
+        "...............",
+        "#..........#...",
+        "...............",
+        "...............",
+        "...............",
+        "......#.....#..",
+        ".#.............",
+        "#..............",
+        "...............",
+        "...............",
+        "...............",
+        "......#.#......",
+        "............#..",
+        "....#..........",
+    ];
+
+    const test_reddit_2 = [
+        "......#........",
+        ".........#.....",
+        "...............",
+        "...............",
+        "..........#...#",
+        "...............",
+        "...............",
+        "#...#..........",
+        "..#............",
+        "...............",
+        ".........#.....",
+        ".........#.....",
+        "...........#...",
+        "...............",
+        ".#...........#.",
+    ];
+
+    const input = await LoadInput(11);
+
+    const sm = new StarMap(input);
+    
+    console.log("Empty Columns found:");
+    Inspect(sm.emptyColumns);
+    console.log("Empty Rows found:");
+    Inspect(sm.emptyRows);
+    console.log("Galaxies found:");
+    console.log(sm.galaxyList);
+    
+    console.log(`The sum of the shortest paths between galaxies is: ${sm.solvePart1()}`);
+}
+
+class StarMap {
+    private originalMap: string[];
+    private mapDimensions: {width: number, height: number};
+    public galaxyList: Array<Vector2> = [];
+    public emptyColumns: number[] = [];
+    public emptyRows: number[] = [];
+
+    constructor(rawMap: string[]) {
+        this.originalMap = rawMap;
+        this.mapDimensions = {
+            width: this.originalMap[0].length,
+            height: this.originalMap.length
+        };
+        this.parse();
+        // Sort the galaxy list by Y values
+        this.galaxyList.sort((a, b) => a.Y - b.Y)
+    }
+
+    private parse() {
+        // Find the empty rows and columns
+        for (let col = 0; col < this.mapDimensions.width; col++) {
+            // Count of the number of empty cells in the column
+            let emptyCellsInColumn = 0;
+            for (let row = 0; row < this.mapDimensions.height; row++) {
+                // If current cell is a galaxy, add it to the list and move to the next row
+                if (this.isGalaxy(this.originalMap[row][col])) {
+                    this.galaxyList.push({
+                        X: col,
+                        Y: row
+                    });
+                    continue;
+                }
+                // Check if the current row is empty
+                // Only needs done during the first column
+                if (col == 0 && this.isEmpty(this.originalMap[row])) {
+                    this.emptyRows.push(row);
+                }
+
+                // Otherwise, add an empty char to the pivotted column
+                emptyCellsInColumn++;
+            }
+            // Check if the column was empty
+            if (emptyCellsInColumn == this.mapDimensions.height) {
+                this.emptyColumns.push(col);
+            }
+        }
+    }
+
+    private isEmpty(sector: string) {
+        return /^[.]+$/.test(sector);
+    }
+
+    private isGalaxy(symbol: string) {
+        return symbol == "#";
+    }
+
+    public getExpandedDistance(galaxyIdx1: number, galaxyIdx2: number, debug = false): number {
+        // Get the two galaxies being compared
+        const galaxy1 = this.galaxyList[galaxyIdx1];
+        const galaxy2 = this.galaxyList[galaxyIdx2];
+
+        // DEBUG
+        if (debug) {
+            console.log("DEBUG:");
+            console.log("Comparing ", galaxy1, "to ", galaxy2);
+        }
+
+        // Check for any empty rows or columns between the two
+        let yOffset = 0;
+        let xOffset = 0;
+        this.emptyColumns.forEach((col) => {
+            if (between(col, galaxy1.X, galaxy2.X)) {
+                yOffset += 1;
+                if (debug) {
+                    console.log(`Adding 2 for the empty column at ${col}`);
+                }
+            }
+        });
+        this.emptyRows.forEach((row) => {
+            if (between(row, galaxy1.Y, galaxy2.Y)) {
+                xOffset += 1;
+                if (debug) {
+                    console.log(`Adding 2 for the empty row at ${row}`);
+                }
+            }
+        });
+
+        if (debug) {
+            console.log(`DEBUG: xOffset - ${xOffset}`);
+            console.log(`DEBUG: yOffset - ${yOffset}`);
+        }
+
+        // Find the immediate deltas between the two galaxies
+        const delta: Vector2 = {
+            X: galaxy1.X - galaxy2.X,
+            Y: galaxy1.Y - galaxy2.Y
+        };
+
+        if (debug) {
+            console.log("DEBUG: delta values - ", delta);
+        }
+
+        return Math.abs(delta.X) + xOffset + Math.abs(delta.Y) + yOffset;
+    }
+
+    public solvePart1(debug = false):number {
+        let totalPairs = 0;
+        let totalShortestPaths = 0;
+        for (let idx1 = 0; idx1 < this.galaxyList.length - 1; idx1++) {
+            for (let idx2 = idx1 + 1; idx2 < this.galaxyList.length; idx2++) {
+                totalPairs++;
+                const manhatDist = this.getExpandedDistance(idx1, idx2, debug)
+                if (debug) {
+                    console.log(`Manhattan Distance between node ${idx1 + 1} and ${idx2 + 1} is: `, manhatDist);
+                }
+                totalShortestPaths += manhatDist;
+            }
+
+            if (debug) {
+                console.log("---------------------");
+            }
+        }
+
+        return totalShortestPaths;
+    }
+}
+
+/**
+ * Get the slope of a line going through two {@link Vector2}'s
+ *
+ * If the two are on the same x-axis, then the slope will be 0.
+ *
+ * If the two are on the same y-axis, then the slope will be `Infinity`.
+ * 
+ * @param {Vector2} node1 
+ * @param {Vector2} node2 
+ * @returns {number} A float or `Infinity`
+ */
+function getSlope(node1: Vector2, node2: Vector2): number {
+    const delta = {
+        X: node2.X - node1.X,
+        Y: node2.Y - node1.Y
+    };
+
+    if (delta.X == 0) { return Infinity; }
+
+    return delta.Y/delta.X;
+}
+
+function getDistance(node1: Vector2, node2: Vector2): number {
+    const delta: Vector2 = {
+        X: node2.X - node1.X,
+        Y: node2.Y - node1.Y
+    };
+
+    return Math.sqrt(Math.pow(delta.X, 2) + Math.pow(delta.Y, 2));
+}
+
+function getManhattanDistance(node1: Vector2, node2: Vector2): number {
+    const delta: Vector2 = {
+        X: node1.X - node2.X,
+        Y: node1.Y - node2.Y
+    };
+
+    const manhatDist = Math.abs(delta.X) + Math.abs(delta.Y);
+
+    return manhatDist;
+}
+
+function between(toCheck: number, boundary1: number, boundary2: number): boolean {
+    return (toCheck > boundary1 && toCheck < boundary2)
+        || (toCheck > boundary2 && toCheck < boundary1);
+}
+
+main();

+ 44 - 0
11/demo_1.out

@@ -0,0 +1,44 @@
+Manhattan Distance between node 1 and 2 is:  6
+Manhattan Distance between node 1 and 3 is:  6
+Manhattan Distance between node 1 and 4 is:  9
+Manhattan Distance between node 1 and 5 is:  9
+Manhattan Distance between node 1 and 6 is:  15
+Manhattan Distance between node 1 and 7 is:  15
+Manhattan Distance between node 1 and 8 is:  15
+Manhattan Distance between node 1 and 9 is:  12
+---------------------
+Manhattan Distance between node 2 and 3 is:  10
+Manhattan Distance between node 2 and 4 is:  5
+Manhattan Distance between node 2 and 5 is:  13
+Manhattan Distance between node 2 and 6 is:  9
+Manhattan Distance between node 2 and 7 is:  9
+Manhattan Distance between node 2 and 8 is:  19
+Manhattan Distance between node 2 and 9 is:  14
+---------------------
+Manhattan Distance between node 3 and 4 is:  11
+Manhattan Distance between node 3 and 5 is:  5
+Manhattan Distance between node 3 and 6 is:  17
+Manhattan Distance between node 3 and 7 is:  17
+Manhattan Distance between node 3 and 8 is:  9
+Manhattan Distance between node 3 and 9 is:  14
+---------------------
+Manhattan Distance between node 4 and 5 is:  8
+Manhattan Distance between node 4 and 6 is:  6
+Manhattan Distance between node 4 and 7 is:  6
+Manhattan Distance between node 4 and 8 is:  14
+Manhattan Distance between node 4 and 9 is:  9
+---------------------
+Manhattan Distance between node 5 and 6 is:  12
+Manhattan Distance between node 5 and 7 is:  12
+Manhattan Distance between node 5 and 8 is:  6
+Manhattan Distance between node 5 and 9 is:  9
+---------------------
+Manhattan Distance between node 6 and 7 is:  6
+Manhattan Distance between node 6 and 8 is:  16
+Manhattan Distance between node 6 and 9 is:  11
+---------------------
+Manhattan Distance between node 7 and 8 is:  10
+Manhattan Distance between node 7 and 9 is:  5
+---------------------
+Manhattan Distance between node 8 and 9 is:  5
+---------------------

+ 44 - 0
11/demo_2.out

@@ -0,0 +1,44 @@
+Manhattan Distance between node 1 and 2 is:  6
+Manhattan Distance between node 1 and 3 is:  6
+Manhattan Distance between node 1 and 4 is:  9
+Manhattan Distance between node 1 and 5 is:  9
+Manhattan Distance between node 1 and 6 is:  15
+Manhattan Distance between node 1 and 7 is:  15
+Manhattan Distance between node 1 and 8 is:  15
+Manhattan Distance between node 1 and 9 is:  12
+---------------------
+Manhattan Distance between node 2 and 3 is:  10
+Manhattan Distance between node 2 and 4 is:  5
+Manhattan Distance between node 2 and 5 is:  13
+Manhattan Distance between node 2 and 6 is:  9
+Manhattan Distance between node 2 and 7 is:  9
+Manhattan Distance between node 2 and 8 is:  19
+Manhattan Distance between node 2 and 9 is:  14
+---------------------
+Manhattan Distance between node 3 and 4 is:  11
+Manhattan Distance between node 3 and 5 is:  5
+Manhattan Distance between node 3 and 6 is:  17
+Manhattan Distance between node 3 and 7 is:  17
+Manhattan Distance between node 3 and 8 is:  9
+Manhattan Distance between node 3 and 9 is:  14
+---------------------
+Manhattan Distance between node 4 and 5 is:  8
+Manhattan Distance between node 4 and 6 is:  6
+Manhattan Distance between node 4 and 7 is:  6
+Manhattan Distance between node 4 and 8 is:  14
+Manhattan Distance between node 4 and 9 is:  9
+---------------------
+Manhattan Distance between node 5 and 6 is:  12
+Manhattan Distance between node 5 and 7 is:  12
+Manhattan Distance between node 5 and 8 is:  6
+Manhattan Distance between node 5 and 9 is:  9
+---------------------
+Manhattan Distance between node 6 and 7 is:  6
+Manhattan Distance between node 6 and 8 is:  16
+Manhattan Distance between node 6 and 9 is:  11
+---------------------
+Manhattan Distance between node 7 and 8 is:  10
+Manhattan Distance between node 7 and 9 is:  5
+---------------------
+Manhattan Distance between node 8 and 9 is:  5
+---------------------

+ 140 - 0
inputs/11.txt

@@ -0,0 +1,140 @@
+.....................#......#...................#...............#........................................#..................................
+...........................................................#.............................................................#..................
+.....................................................................#.........#......#...........#.............#...........................
+.....................................................#.......................................#..............................................
+......#.................................#..........................................................................................#........
+....................#.........#.............................................................................#...............................
+..#.........................................................#.............................................................................#.
+........................................................................................#...........#..................#....................
+........#....................................#........................#.........................................................#...........
+..............................................................................#................................#............................
+..................................................#............#............................................................................
+.........................#......#........#................................................#..........................................#......
+......#..............................................................................#...............#......................................
+...................................................................#........................................................................
+....................#...........................#............................................................#..............#.....#.........
+..................................#....................#.........................................#......#..............................#....
+.#..........................................................................................................................................
+........#.....#........................#...........#.............#.....................#....................................................
+..........................#.................................#.....................#.........................................................
+.............................................................................................................................#.............#
+...................................#......................................................#....................#............................
+.....................#...................#............#..................#.............................................#...........#........
+..............................................................................................#.............................................
+....#..........#...............#..........................................................................#.................................
+.............................................#..............................................................................................
+...................................................................#..............#.................................#.......................
+..................#........................................................#................................................................
+..#.................................#...................#..............................#.........................................#.......#..
+...............................................................#...............#.....................#..........#.........#.................
+.........#......................#.............................................................#.............................................
+..............#...............................#.............................................................................................
+........................................#.................................#............................................................#....
+..............................................................................................................#......#......#...............
+.........................#..........#.............................................#........#................................................
+....................................................................#.....................................#.................................
+....#...............#........#...............................#........................#........................................#............
+........................................................#...................................................................................
+............................................#...........................#......................#.....#......................................
+.........................................................................................#.............................................#....
+...........#....................#..................................#.................................................#...........#..........
+.#..........................................................................................................................................
+................#........................#..........#.......................................................................................
+........................#........................................................................................#..........................
+........................................................................#.........#...................#...................#.................
+........................................................#......................................................................#............
+.....#.....................................................................................#................................................
+...............#...................................#.................#..............................................................#.......
+.........#..................#..................................#..................................................#.........................
+......................................#.......#....................................#......................................................#.
+..........................................................................................................#......................#..........
+...#..............#.......................#.....................................................#...........................................
+................................#...............................................#..........#................................................
+..........................#......................#......................#...................................................................
+..................................................................#................................#........................................
+......................................................#.................................#.........................#...................#.....
+..............#..........................#..................................................................................#...............
+......#......................#.................................................................#...........#................................
+.............................................................................#..............................................................
+.......................#..........................#......................................................................#..........#.......
+.#.................................#................................................................#....................................#..
+........#......#................................................#...............................................#...........................
+..........................................................................#.....#.........................#...........#.....#...............
+............................#..............#..........#....................................#................................................
+............................................................................................................................................
+............#..........#....................................................................................................................
+............................................................#.......................#..............#........................................
+.....................................#...............................#...................................#..................................
+.........#.....................................................................#.....................................#...............#.....#
+#.............#.............................................................................................................................
+......................................................#....................................#....................................#...........
+...................................................................#............................................#...........................
+............................................................................................................................................
+....................#............................#................................................#......................................#..
+.............................#...................................................#..........................................................
+.........................................................................#...............#.................#................................
+.............#.........#.........#.....#......................#............................................................#................
+............................................................................................................................................
+.............................................#..........#.....................#..................................#..........................
+............................................................................................................................................
+...................................#.............#................#.....#.....................................................#.............
+....#.....................#..................................#..............................................................................
+............................................................................................................................................
+#.....................................#...................................................................................#.................
+...............................................#......................#.....#...................................#...........................
+.........#...................#.....................................................#.................#......................................
+.......................#...............................#........................................#..............................#............
+...#.....................................#..................................................................#...............................
+...................................................#......................................#...........................................#.....
+.................#..........................................................................................................................
+...................................................................................................................#......#.................
+.................................#..............#.....#......................#.................#.........#..................................
+#....................................................................#........................................#...........................#.
+............................................................................................................................................
+.........................#..................................................................................................................
+...............................#.......................................................................................#....................
+..........#...................................#..................#.....................#....................#...............................
+............................................................................#.....#.........................................................
+....#............#............................................................................#.......#..........#..........................
+..................................................................................................................................#.........
+...................................#...........................................#............................................................
+.............................#..........................#.................................#........#........................................
+..............#....................................................#........................................#.............#...........#.....
+...............................................#.............#.......................................................#......................
+........#..............................................................#....................................................................
+.......................................................................................#........#...........................................
+......................#........#.........................#..................#........................#......................#...............
+.#.........#.....#..............................................#........................................................................#..
+.....................................................#......................................................................................
+.........................................#...........................#..............................................#.......................
+.............................................................#................................................#................#............
+......#.......................................#.......................................#...............#.....................................
+....................#.......#............................#................#...........................................................#.....
+...........#.....................................................................................................#..........................
+..........................................................................................#..............#..................................
+..................................................#..........................................................................#..............
+...............#............................................#.....................................................................#.........
+........................#...........................................#............................#..................#.......................
+................................................................................#..............................#.......................#....
+......................................#...................................#..............#..............#...................................
+..#............................................#..............#...........................................................#.................
+.........#........................#...................#.....................................................................................
+.............................#..............................................................................................................
+....................................................................................#.......................................................
+.........................#.............#............................................................#......#......#.........#......#........
+...................#...........................................................................#...........................................#
+.......#...............................................#.....#........#..........#..........................................................
+..............#....................#........................................................................................................
+........................................................................................................#.................#...........#.....
+..#...............................................................#.........#.......#.......................................................
+.........#......................#..........................#.....................................#............#.............................
+...................#.................................#.................................................................#....................
+....................................#.......................................................................................................
+...........................#......................................................................................................#.........
+............................................#.................#............#...................#.......#...................#................
+............#.....................................#.........................................................................................
+...........................................................................................................#......#.........................
+.....................................#..............................................#......#................................................
+.........................................................................#..............................................#...................
+#....................#........#...................................................................#.....................................#...
+.....#........................................#.........#........................#..........................................................