generated from eric/adventofcode2023
Compare commits
5 Commits
ea72130ebc
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f06e5b5f64 | ||
|
|
7bbedb47a5 | ||
|
|
42b2af4452 | ||
|
|
462508c90f | ||
|
|
83911a58ec |
135
06/code.js
Normal file
135
06/code.js
Normal file
@@ -0,0 +1,135 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
let total = 0;
|
||||
|
||||
const problems = [];
|
||||
for (const row of usedArray) {
|
||||
const contents = row.trim().split(/\s+/);
|
||||
|
||||
for (let i = 0; i < contents.length; i++) {
|
||||
const element = contents[i];
|
||||
if (!problems[i]) {
|
||||
problems[i] = [];
|
||||
}
|
||||
problems[i].push(element);
|
||||
}
|
||||
}
|
||||
|
||||
for (let problem of problems) {
|
||||
problem = problem.reverse();
|
||||
const operator = problem.shift();
|
||||
let answer = operator === '+' ? 0 : 1;
|
||||
for (const number of problem) {
|
||||
if (operator === '+') {
|
||||
answer += Number.parseInt(number);
|
||||
} else {
|
||||
answer *= Number.parseInt(number);
|
||||
}
|
||||
}
|
||||
total += answer;
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(total);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
|
||||
let total2 = 0;
|
||||
|
||||
let newInput = JSON.parse(JSON.stringify(usedArray));
|
||||
for (let rowIndex = 0; rowIndex < newInput.length; rowIndex++) {
|
||||
const row = newInput[rowIndex];
|
||||
newInput[rowIndex] = row.split("");
|
||||
}
|
||||
|
||||
const problemStarts = [];
|
||||
const operatorRowArray = newInput[usedArray.length - 1];
|
||||
for (let i = 0; i < operatorRowArray.length; i++) {
|
||||
if (["*", "+"].includes(operatorRowArray[i])) {
|
||||
problemStarts.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
for (let rowIndex = 0; rowIndex < usedArray.length; rowIndex++) {
|
||||
const row = usedArray[rowIndex];
|
||||
for (let i = 0; i < row.length; i++) {
|
||||
const char = row[i];
|
||||
if (char === " " && !problemStarts.includes(i + 1)) {
|
||||
newInput[rowIndex][i] = '#';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let rowIndex = 0; rowIndex < newInput.length; rowIndex++) {
|
||||
const row = newInput[rowIndex];
|
||||
newInput[rowIndex] = row.join("");
|
||||
}
|
||||
|
||||
const problems2 = [];
|
||||
for (const row of newInput) {
|
||||
const contents = row.trim().split(/\s+/);
|
||||
for (let i = 0; i < contents.length; i++) {
|
||||
const element = contents[i];
|
||||
if (!problems2[i]) {
|
||||
problems2[i] = [];
|
||||
}
|
||||
problems2[i].push(element);
|
||||
}
|
||||
}
|
||||
|
||||
let problemsVertical = [];
|
||||
for (let i = 0; i < problems2.length; i++) {
|
||||
const problem = problems2[i];
|
||||
let vertProblem = [];
|
||||
for (let j = 0; j < problem.length - 1; j++) {
|
||||
const newElement =
|
||||
(problem[0] && problem[0][j] && problem[0][j] !== '#' ? problem[0][j] : '') +
|
||||
(problem[1] && problem[1][j] && problem[1][j] !== '#' ? problem[1][j] : '') +
|
||||
(problem[2] && problem[2][j] && problem[2][j] !== '#' ? problem[2][j] : '') +
|
||||
(problem[3] && problem[3][j] && problem[3][j] !== '#' ? problem[3][j] : '') +
|
||||
(problem[4] && problem[4][j] && problem[4][j] !== '#' ? problem[4][j] : '');
|
||||
if (Number.parseInt(newElement)) {
|
||||
vertProblem.push(Number.parseInt(newElement));
|
||||
}
|
||||
}
|
||||
vertProblem.push(problem[problem.length - 1]);
|
||||
problemsVertical.push(vertProblem);
|
||||
}
|
||||
|
||||
for (let problem of problemsVertical) {
|
||||
problem = problem.reverse();
|
||||
const operator = problem.shift();
|
||||
let answer = operator.includes('+') ? 0 : 1;
|
||||
for (const number of problem) {
|
||||
if (operator.includes('+')) {
|
||||
answer += Number.parseInt(number);
|
||||
} else {
|
||||
answer *= Number.parseInt(number);
|
||||
}
|
||||
}
|
||||
total2 += answer;
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(total2);
|
||||
|
||||
// functions
|
||||
103
07/code.js
Normal file
103
07/code.js
Normal file
@@ -0,0 +1,103 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
let newInput = JSON.parse(JSON.stringify(usedArray));
|
||||
for (let i = 0; i < newInput.length; i++) {
|
||||
newInput[i] = newInput[i].split("");
|
||||
}
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if ((newInput[i - 1][j] === '|' || newInput[i - 1][j] === 'S') && newInput[i][j] === '.') {
|
||||
newInput[i][j] = '|';
|
||||
}
|
||||
if (newInput[i - 1][j] === '|' && newInput[i][j] === '^') {
|
||||
if (j > 0 && newInput[i][j - 1] === '.') {
|
||||
newInput[i][j - 1] = '|';
|
||||
}
|
||||
}
|
||||
if (newInput[i - 1][j] === '|' && newInput[i][j] === '^') {
|
||||
if (j < newInput[i].length - 1 && newInput[i][j + 1] === '.') {
|
||||
newInput[i][j + 1] = '|';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let splits = 0;
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if (newInput[i][j] === '^' && newInput[i - 1][j] === '|') {
|
||||
splits++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(splits);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
|
||||
newInput = JSON.parse(JSON.stringify(usedArray));
|
||||
for (let i = 0; i < newInput.length; i++) {
|
||||
newInput[i] = newInput[i].split("");
|
||||
}
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if (newInput[i - 1][j] === 'S') {
|
||||
newInput[i][j] = 1;
|
||||
}
|
||||
if (Number.parseInt(newInput[i - 1][j]) > 0 && Number.parseInt(newInput[i][j]) > 0) {
|
||||
newInput[i][j] = newInput[i][j] + newInput[i - 1][j];
|
||||
}
|
||||
if ((Number.parseInt(newInput[i - 1][j]) > 0) && newInput[i][j] === '.') {
|
||||
newInput[i][j] = newInput[i - 1][j];
|
||||
}
|
||||
if (Number.parseInt(newInput[i - 1][j]) > 0 && newInput[i][j] === '^') {
|
||||
if (j > 0 && newInput[i][j - 1] === '.') {
|
||||
newInput[i][j - 1] = newInput[i - 1][j];
|
||||
} else if (j > 0 && Number.parseInt(newInput[i][j - 1]) > 0) {
|
||||
newInput[i][j - 1] = Number.parseInt(newInput[i][j - 1]) + newInput[i - 1][j];
|
||||
}
|
||||
if (j < newInput[i].length - 1 && newInput[i][j + 1] === '.') {
|
||||
newInput[i][j + 1] = newInput[i - 1][j];
|
||||
} else if (j < newInput[i].length - 1 && Number.parseInt(newInput[i][j + 1]) > 0) {
|
||||
newInput[i][j + 1] = Number.parseInt(newInput[i][j + 1]) + newInput[i - 1][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let timelines = 0;
|
||||
|
||||
const bottomRow = newInput[newInput.length - 1];
|
||||
for (let i = 0; i < bottomRow.length; i++) {
|
||||
if (Number.parseInt(bottomRow[i]) > 0) {
|
||||
timelines += Number.parseInt(bottomRow[i]);
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(timelines);
|
||||
|
||||
// functions
|
||||
120
08/code.js
Normal file
120
08/code.js
Normal file
@@ -0,0 +1,120 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
let coordinatesArray = [];
|
||||
|
||||
for (const element of usedArray) {
|
||||
const coordinates = element.split(",");
|
||||
coordinatesArray.push({ x: Number.parseInt(coordinates[0]), y: Number.parseInt(coordinates[1]), z: Number.parseInt(coordinates[2]) });
|
||||
}
|
||||
|
||||
let segments = [];
|
||||
let parent = {};
|
||||
|
||||
for (let i = 0; i < coordinatesArray.length; i++) {
|
||||
for (let j = i + 1; j < coordinatesArray.length; j++) {
|
||||
const x2 = Math.pow(coordinatesArray[i].x - coordinatesArray[j].x, 2);
|
||||
const y2 = Math.pow(coordinatesArray[i].y - coordinatesArray[j].y, 2);
|
||||
const z2 = Math.pow(coordinatesArray[i].z - coordinatesArray[j].z, 2);
|
||||
const distance = Math.sqrt(x2 + y2 + z2);
|
||||
segments.push({ from: i, to: j, distance });
|
||||
}
|
||||
}
|
||||
|
||||
segments.sort((a, b) => a.distance - b.distance);
|
||||
const shortest = segments.slice(0, sampleMode ? 10 : 1000);
|
||||
|
||||
for (const seg of shortest) {
|
||||
union(seg.from, seg.to);
|
||||
}
|
||||
|
||||
const circuits = {};
|
||||
for (const seg of shortest) {
|
||||
const root = find(seg.from);
|
||||
circuits[root] = circuits[root] || new Set();
|
||||
circuits[root].add(seg.from);
|
||||
circuits[root].add(seg.to);
|
||||
}
|
||||
|
||||
const circuitList = Object.values(circuits);
|
||||
const sizes = circuitList.map(nodes => nodes.size).sort((a, b) => b - a);
|
||||
const result = sizes[0] * sizes[1] * sizes[2];
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(result);
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
|
||||
coordinatesArray = [];
|
||||
|
||||
for (const element of usedArray) {
|
||||
const coordinates = element.split(",");
|
||||
coordinatesArray.push({ x: Number.parseInt(coordinates[0]), y: Number.parseInt(coordinates[1]), z: Number.parseInt(coordinates[2]) });
|
||||
}
|
||||
|
||||
segments = [];
|
||||
parent = {};
|
||||
|
||||
for (let i = 0; i < coordinatesArray.length; i++) {
|
||||
for (let j = i + 1; j < coordinatesArray.length; j++) {
|
||||
const x2 = Math.pow(coordinatesArray[i].x - coordinatesArray[j].x, 2);
|
||||
const y2 = Math.pow(coordinatesArray[i].y - coordinatesArray[j].y, 2);
|
||||
const z2 = Math.pow(coordinatesArray[i].z - coordinatesArray[j].z, 2);
|
||||
const distance = Math.sqrt(x2 + y2 + z2);
|
||||
segments.push({ from: i, to: j, distance });
|
||||
}
|
||||
}
|
||||
|
||||
segments.sort((a, b) => a.distance - b.distance);
|
||||
let circuitCount = coordinatesArray.length;
|
||||
let lastSegment;
|
||||
for (const seg of segments) {
|
||||
const rootFrom = find(seg.from);
|
||||
const rootTo = find(seg.to);
|
||||
|
||||
if (rootFrom !== rootTo) {
|
||||
union(seg.from, seg.to);
|
||||
circuitCount--;
|
||||
lastSegment = seg;
|
||||
|
||||
if (circuitCount === 1) break;
|
||||
}
|
||||
}
|
||||
|
||||
const result2 = coordinatesArray[lastSegment.from].x * coordinatesArray[lastSegment.to].x;
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(result2);
|
||||
|
||||
// functions
|
||||
|
||||
function find(x) {
|
||||
if (parent[x] === undefined) parent[x] = x;
|
||||
if (parent[x] !== x) parent[x] = find(parent[x]);
|
||||
return parent[x];
|
||||
}
|
||||
|
||||
function union(a, b) {
|
||||
const rootA = find(a);
|
||||
const rootB = find(b);
|
||||
if (rootA !== rootB) {
|
||||
parent[rootA] = rootB;
|
||||
}
|
||||
}
|
||||
151
09/code.js
Normal file
151
09/code.js
Normal file
@@ -0,0 +1,151 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
let coordinatesArray = [];
|
||||
|
||||
for (const element of usedArray) {
|
||||
const coordinates = element.split(",");
|
||||
coordinatesArray.push({ x: Number.parseInt(coordinates[0]), y: Number.parseInt(coordinates[1]) });
|
||||
}
|
||||
|
||||
let segments = [];
|
||||
|
||||
for (let i = 0; i < coordinatesArray.length; i++) {
|
||||
for (let j = i + 1; j < coordinatesArray.length; j++) {
|
||||
const dx = Math.abs(coordinatesArray[i].x - coordinatesArray[j].x) + 1;
|
||||
const dy = Math.abs(coordinatesArray[i].y - coordinatesArray[j].y) + 1;
|
||||
const area = dx * dy;
|
||||
segments.push({ dx: dx, dy: dy, area: area });
|
||||
}
|
||||
}
|
||||
|
||||
segments.sort((a, b) => a.area > b.area ? -1 : 1);
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(segments[0].area);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
|
||||
// Only collect the RED tile coordinates (endpoints)
|
||||
const xSet = new Set();
|
||||
const ySet = new Set();
|
||||
|
||||
for (const tile of coordinatesArray) {
|
||||
xSet.add(tile.x);
|
||||
ySet.add(tile.y);
|
||||
}
|
||||
|
||||
const xCoords = [...xSet].sort((a, b) => a - b);
|
||||
const yCoords = [...ySet].sort((a, b) => a - b);
|
||||
|
||||
// Store horizontal and vertical line segments for boundary checking
|
||||
const hLines = []; // {y, x1, x2}
|
||||
const vLines = []; // {x, y1, y2}
|
||||
|
||||
for (let i = 0; i < coordinatesArray.length; i++) {
|
||||
const curr = coordinatesArray[i];
|
||||
const next = coordinatesArray[(i + 1) % coordinatesArray.length];
|
||||
|
||||
if (curr.x === next.x) {
|
||||
vLines.push({ x: curr.x, y1: Math.min(curr.y, next.y), y2: Math.max(curr.y, next.y) });
|
||||
} else {
|
||||
hLines.push({ y: curr.y, x1: Math.min(curr.x, next.x), x2: Math.max(curr.x, next.x) });
|
||||
}
|
||||
}
|
||||
|
||||
// Check if a point is on the boundary
|
||||
function isOnBoundary(x, y) {
|
||||
for (const line of hLines) {
|
||||
if (y === line.y && x >= line.x1 && x <= line.x2) return true;
|
||||
}
|
||||
for (const line of vLines) {
|
||||
if (x === line.x && y >= line.y1 && y <= line.y2) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if a point is inside using ray casting (count vertical line crossings to the left)
|
||||
function isInside(x, y) {
|
||||
let crossings = 0;
|
||||
for (const line of vLines) {
|
||||
if (line.x < x && y >= line.y1 && y < line.y2) {
|
||||
crossings++;
|
||||
}
|
||||
}
|
||||
return crossings % 2 === 1;
|
||||
}
|
||||
|
||||
// Check if a point is valid (on boundary or inside)
|
||||
function isValid(x, y) {
|
||||
return isOnBoundary(x, y) || isInside(x, y);
|
||||
}
|
||||
|
||||
// Check if entire rectangle is valid
|
||||
function isRectValid(x1, y1, x2, y2) {
|
||||
// Check all corner and edge points at compressed coordinates within the rectangle
|
||||
for (const y of yCoords) {
|
||||
if (y < y1 || y > y2) continue;
|
||||
for (const x of xCoords) {
|
||||
if (x < x1 || x > x2) continue;
|
||||
if (!isValid(x, y)) return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Also check if any boundary line crosses through the rectangle's interior
|
||||
// creating an "outside" region
|
||||
for (const line of vLines) {
|
||||
if (line.x > x1 && line.x < x2) {
|
||||
// Vertical line inside rectangle - check if it creates a gap
|
||||
if (line.y1 > y1 || line.y2 < y2) {
|
||||
// Line doesn't span full height - might create invalid region
|
||||
if (line.y1 > y1 && !isValid(line.x, y1)) return false;
|
||||
if (line.y2 < y2 && !isValid(line.x, y2)) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Find largest valid rectangle with red corners
|
||||
let maxArea = 0;
|
||||
|
||||
for (let i = 0; i < coordinatesArray.length; i++) {
|
||||
for (let j = i + 1; j < coordinatesArray.length; j++) {
|
||||
const r1 = coordinatesArray[i];
|
||||
const r2 = coordinatesArray[j];
|
||||
|
||||
const x1 = Math.min(r1.x, r2.x);
|
||||
const x2 = Math.max(r1.x, r2.x);
|
||||
const y1 = Math.min(r1.y, r2.y);
|
||||
const y2 = Math.max(r1.y, r2.y);
|
||||
|
||||
if (isRectValid(x1, y1, x2, y2)) {
|
||||
const area = (x2 - x1 + 1) * (y2 - y1 + 1);
|
||||
if (area > maxArea) maxArea = area;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(maxArea);
|
||||
|
||||
// functions
|
||||
Reference in New Issue
Block a user