clean up: formatting

This commit is contained in:
Baipyrus 2024-01-10 20:02:15 +01:00
parent 138b4e2944
commit cbd3a2929b
11 changed files with 314 additions and 314 deletions

View File

@ -1,38 +1,38 @@
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt")
.toString()
.split(/\r*\n/)
.filter((e) => e.length > 0)
.map((e) => e.split(""));
const expand = (arr) =>
arr.reduce(
(a, i) => [
...a,
...(i.some((e) => e !== ".") ? [i] : [i, Array(i.length).fill(".")]),
],
[]
);
const flip = (current) =>
Array.from({ length: current[0].length }, (_, x) =>
Array.from({ length: current.length }, (_, y) => current[y][x])
);
const universe = flip(expand(flip(expand(input))));
const galaxies = universe
.map((a, y) => a.map((b, x) => ({ s: b, x, y })).filter((b) => b.s === "#"))
.filter((e) => e.length > 0)
.reduce((a, i) => [...a, ...i], []);
const distances = galaxies.map((c, i) =>
galaxies.reduce(
(a, n, j) => a + (j > i ? Math.abs(c.x - n.x) + Math.abs(c.y - n.y) : 0),
0
)
);
const sum = distances.reduce((a, i) => a + i);
console.log(sum);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt")
.toString()
.split(/\r*\n/)
.filter((e) => e.length > 0)
.map((e) => e.split(""));
const expand = (arr) =>
arr.reduce(
(a, i) => [
...a,
...(i.some((e) => e !== ".") ? [i] : [i, Array(i.length).fill(".")]),
],
[]
);
const flip = (current) =>
Array.from({ length: current[0].length }, (_, x) =>
Array.from({ length: current.length }, (_, y) => current[y][x])
);
const universe = flip(expand(flip(expand(input))));
const galaxies = universe
.map((a, y) => a.map((b, x) => ({ s: b, x, y })).filter((b) => b.s === "#"))
.filter((e) => e.length > 0)
.reduce((a, i) => [...a, ...i], []);
const distances = galaxies.map((c, i) =>
galaxies.reduce(
(a, n, j) => a + (j > i ? Math.abs(c.x - n.x) + Math.abs(c.y - n.y) : 0),
0
)
);
const sum = distances.reduce((a, i) => a + i);
console.log(sum);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);

View File

@ -1,44 +1,44 @@
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt")
.toString()
.split(/\r*\n/)
.filter((e) => e.length > 0)
.map((a, j) => a.split("").map((b, i) => ({ s: b, x: i, y: j })));
const expand = (arr) => {
let expansion = 0;
return arr.map((a) => {
const c = !a.some((b) => b.s !== ".");
if (c) expansion += 999999;
return a.map((b) => {
const { s, x, y } = b;
return { s, x, y: y + expansion };
});
});
};
const flip = (current) =>
Array.from({ length: current[0].length }, (_, cx) =>
Array.from({ length: current.length }, (_, cy) => {
const { s, x, y } = current[cy][cx];
return { s, x: y, y: x };
})
);
const universe = flip(expand(flip(expand(input))));
const galaxies = universe
.map((a) => a.filter((b) => b.s === "#"))
.filter((e) => e.length > 0)
.reduce((a, i) => [...a, ...i], []);
const distances = galaxies.map((c, i) =>
galaxies.reduce(
(a, n, j) => a + (j > i ? Math.abs(c.x - n.x) + Math.abs(c.y - n.y) : 0),
0
)
);
const sum = distances.reduce((a, i) => a + i);
console.log(sum);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt")
.toString()
.split(/\r*\n/)
.filter((e) => e.length > 0)
.map((a, j) => a.split("").map((b, i) => ({ s: b, x: i, y: j })));
const expand = (arr) => {
let expansion = 0;
return arr.map((a) => {
const c = !a.some((b) => b.s !== ".");
if (c) expansion += 999999;
return a.map((b) => {
const { s, x, y } = b;
return { s, x, y: y + expansion };
});
});
};
const flip = (current) =>
Array.from({ length: current[0].length }, (_, cx) =>
Array.from({ length: current.length }, (_, cy) => {
const { s, x, y } = current[cy][cx];
return { s, x: y, y: x };
})
);
const universe = flip(expand(flip(expand(input))));
const galaxies = universe
.map((a) => a.filter((b) => b.s === "#"))
.filter((e) => e.length > 0)
.reduce((a, i) => [...a, ...i], []);
const distances = galaxies.map((c, i) =>
galaxies.reduce(
(a, n, j) => a + (j > i ? Math.abs(c.x - n.x) + Math.abs(c.y - n.y) : 0),
0
)
);
const sum = distances.reduce((a, i) => a + i);
console.log(sum);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);

View File

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

View File

@ -1,8 +1,8 @@
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString();
console.log(input);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString();
console.log(input);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);

View File

@ -1,8 +1,8 @@
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString();
console.log(input);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString();
console.log(input);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);

View File

@ -1,8 +1,8 @@
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString();
console.log(input);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString();
console.log(input);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);

View File

@ -1,12 +1,12 @@
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString().split("\n")[0].split(",");
const hashes = input.map((e) =>
e.split("").reduce((a, i) => ((a + i.charCodeAt(0)) * 17) % 256, 0)
);
const sum = hashes.reduce((a, i) => a + i);
console.log(sum);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString().split("\n")[0].split(",");
const hashes = input.map((e) =>
e.split("").reduce((a, i) => ((a + i.charCodeAt(0)) * 17) % 256, 0)
);
const sum = hashes.reduce((a, i) => a + i);
console.log(sum);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);

View File

@ -1,12 +1,12 @@
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString().split("\n")[0].split(",");
const hashes = input.map((e) =>
e.split("").reduce((a, i) => ((a + i.charCodeAt(0)) * 17) % 256, 0)
);
const sum = hashes.reduce((a, i) => a + i);
console.log(sum);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString().split("\n")[0].split(",");
const hashes = input.map((e) =>
e.split("").reduce((a, i) => ((a + i.charCodeAt(0)) * 17) % 256, 0)
);
const sum = hashes.reduce((a, i) => a + i);
console.log(sum);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);

View File

@ -1,29 +1,29 @@
# Advent of Code 2023
## About
This repository stores my solutions to the daily challenges of [Advent of Code Calendar 2023](https://adventofcode.com/2023/).
## Execution
Any of the scripts can be executed directly using [NodeJS](https://nodejs.org/en/download/).
**Example:**
```bash
cd Day01/
node Part1.js
```
## File explanation
A single challenge consists of 3 files:
- `input.txt`: My input data, as provided by AoC.
- `Part1.js`: My solution to part 1 of the challenge.
- `Part2.js`: My solution to part 2 of the challenge.
Other special files:
- [`template.js`](template.js): A template file to use when initializing a new challenge.
- `sample.js`: Example or test code; Ignored by git (see [.gitignore](.gitignore)).
# Advent of Code 2023
## About
This repository stores my solutions to the daily challenges of [Advent of Code Calendar 2023](https://adventofcode.com/2023/).
## Execution
Any of the scripts can be executed directly using [NodeJS](https://nodejs.org/en/download/).
**Example:**
```bash
cd Day01/
node Part1.js
```
## File explanation
A single challenge consists of 3 files:
- `input.txt`: My input data, as provided by AoC.
- `Part1.js`: My solution to part 1 of the challenge.
- `Part2.js`: My solution to part 2 of the challenge.
Other special files:
- [`template.js`](template.js): A template file to use when initializing a new challenge.
- `sample.js`: Example or test code; Ignored by git (see [.gitignore](.gitignore)).

View File

@ -1,8 +1,8 @@
{
"name": "aoc23",
"version": "1.0.0",
"description": "Advent of Code",
"author": "Baipyrus",
"license": "ISC",
"type": "module"
{
"name": "aoc23",
"version": "1.0.0",
"description": "Advent of Code",
"author": "Baipyrus",
"license": "ISC",
"type": "module"
}

View File

@ -1,8 +1,8 @@
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString();
console.log(input);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);
import { readFileSync } from "fs";
const t0 = performance.now();
const input = readFileSync("input.txt").toString();
console.log(input);
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);