Day 6 & 7
This commit is contained in:
38
2024/05/index.ts
Normal file
38
2024/05/index.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||||
|
|
||||||
|
const rules: Array<[number, number]> = [];
|
||||||
|
const updates: Array<Array<number>> = [];
|
||||||
|
|
||||||
|
let rulesSection = true;
|
||||||
|
input.split("\n").forEach((line: string) => {
|
||||||
|
if (!line) {
|
||||||
|
rulesSection = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (rulesSection) {
|
||||||
|
rules.push(line.split('|').map((s) => parseInt(s)) as [number, number]);
|
||||||
|
} else {
|
||||||
|
updates.push(line.split(',').map((s) => parseInt(s)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let result = 0;
|
||||||
|
|
||||||
|
updates.forEach((update) => {
|
||||||
|
for (let rule of rules) {
|
||||||
|
const minIndex = update.indexOf(rule[0]);
|
||||||
|
const maxIndex = update.indexOf(rule[1]);
|
||||||
|
if (
|
||||||
|
minIndex !== -1
|
||||||
|
&& maxIndex !== -1
|
||||||
|
&& minIndex > maxIndex
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result += update[(update.length - 1) / 2];
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
42
2024/05/index2.ts
Normal file
42
2024/05/index2.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||||
|
|
||||||
|
const rules: Array<[number, number]> = [];
|
||||||
|
const updates: Array<Array<number>> = [];
|
||||||
|
|
||||||
|
let rulesSection = true;
|
||||||
|
input.split("\n").forEach((line: string) => {
|
||||||
|
if (!line) {
|
||||||
|
rulesSection = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (rulesSection) {
|
||||||
|
rules.push(line.split('|').map((s) => parseInt(s)) as [number, number]);
|
||||||
|
} else {
|
||||||
|
updates.push(line.split(',').map((s) => parseInt(s)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let result = 0;
|
||||||
|
|
||||||
|
updates.forEach((update) => {
|
||||||
|
let newUpdate: Array<number> = [];
|
||||||
|
for (let page of update) {
|
||||||
|
const relevantRules = rules.filter((rule) => rule[0] === page || rule[1] === page);
|
||||||
|
let i = 0;
|
||||||
|
for (; i < newUpdate.length; i++) {
|
||||||
|
const otherPage = newUpdate[i];
|
||||||
|
const rule = relevantRules.find((rule) => rule[0] === otherPage || rule[1] === otherPage);
|
||||||
|
if (rule && rule[0] === page) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newUpdate = [...newUpdate.slice(0, i), page, ...newUpdate.slice(i)]
|
||||||
|
}
|
||||||
|
if (update.join(',') !== newUpdate.join(',')) {
|
||||||
|
result += newUpdate[(newUpdate.length - 1) / 2];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
134
2024/06/index.ts
Normal file
134
2024/06/index.ts
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||||
|
|
||||||
|
interface Coord { x: number, y: number };
|
||||||
|
enum Dir {
|
||||||
|
Up,
|
||||||
|
Right,
|
||||||
|
Down,
|
||||||
|
Left
|
||||||
|
}
|
||||||
|
|
||||||
|
const obs: Array<Coord> = [];
|
||||||
|
let guard: { pos: Coord, dir: Dir } = { pos: { x: 0, y: 0}, dir: Dir.Up };
|
||||||
|
|
||||||
|
const keyMap: { [key: string]: (pos: Coord) => any } = {
|
||||||
|
"#": (pos: Coord) => obs.push(pos),
|
||||||
|
"^": (pos: Coord) => guard = { pos, dir: Dir.Up },
|
||||||
|
">": (pos: Coord) => guard = { pos, dir: Dir.Up },
|
||||||
|
"v": (pos: Coord) => guard = { pos, dir: Dir.Up },
|
||||||
|
"<": (pos: Coord) => guard = { pos, dir: Dir.Up },
|
||||||
|
}
|
||||||
|
|
||||||
|
let x = 0;
|
||||||
|
let y = 0;
|
||||||
|
for (let i = 0; i <= input.length; i++) {
|
||||||
|
const char = input[i];
|
||||||
|
if (char === "\n") {
|
||||||
|
y++;
|
||||||
|
x = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (keyMap[char]) {
|
||||||
|
keyMap[char]({ x, y });
|
||||||
|
}
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
const width = x + 1;
|
||||||
|
const height = y + 1;
|
||||||
|
const guardStart = guard.pos;
|
||||||
|
|
||||||
|
|
||||||
|
const positions: Array<Coord> = [guard.pos];
|
||||||
|
|
||||||
|
function coodsMatch(a: Coord, b: Coord) {
|
||||||
|
return a.x === b.x && a.y === b.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNextPosition() {
|
||||||
|
if (guard.dir === Dir.Up) {
|
||||||
|
return { x: guard.pos.x, y: guard.pos.y - 1 };
|
||||||
|
}
|
||||||
|
if (guard.dir === Dir.Right) {
|
||||||
|
return { x: guard.pos.x + 1, y: guard.pos.y };
|
||||||
|
}
|
||||||
|
if (guard.dir === Dir.Down) {
|
||||||
|
return { x: guard.pos.x, y: guard.pos.y + 1 };
|
||||||
|
}
|
||||||
|
return { x: guard.pos.x - 1, y: guard.pos.y };
|
||||||
|
}
|
||||||
|
|
||||||
|
function rotateGuard() {
|
||||||
|
if (guard.dir === Dir.Up) {
|
||||||
|
guard.dir = Dir.Right;
|
||||||
|
} else if (guard.dir === Dir.Right) {
|
||||||
|
guard.dir = Dir.Down;
|
||||||
|
} else if (guard.dir === Dir.Down) {
|
||||||
|
guard.dir = Dir.Left;
|
||||||
|
} else {
|
||||||
|
guard.dir = Dir.Up;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isObs(coord: Coord, otherObs: Array<Coord>|null = null) {
|
||||||
|
return (otherObs || obs).some((obsCoord) => coodsMatch(obsCoord, coord));
|
||||||
|
}
|
||||||
|
|
||||||
|
function offMap(coord: Coord) {
|
||||||
|
return coord.x >= width
|
||||||
|
|| coord.x < 0
|
||||||
|
|| coord.y >= height
|
||||||
|
|| coord.y < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function alreadyVisited(coord: Coord) {
|
||||||
|
return positions.some((pos) => coodsMatch(pos, coord));
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const nextPos = getNextPosition();
|
||||||
|
if (isObs(nextPos)) {
|
||||||
|
rotateGuard();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (offMap(nextPos)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
guard.pos = nextPos;
|
||||||
|
if (!alreadyVisited(nextPos)) {
|
||||||
|
positions.push(nextPos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part 2
|
||||||
|
let loops = 0;
|
||||||
|
positions.slice(1).forEach((pos, i) => {
|
||||||
|
console.log(Math.floor(`${i / positions.length * 100}%`));
|
||||||
|
guard.pos = guardStart;
|
||||||
|
guard.dir = Dir.Up;
|
||||||
|
const newObs = [...obs, pos];
|
||||||
|
const guardPositions: Array<string> = [];
|
||||||
|
while (true) {
|
||||||
|
const nextPos = getNextPosition();
|
||||||
|
if (isObs(nextPos, newObs)) {
|
||||||
|
rotateGuard();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (offMap(nextPos)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const guardPos = `${guard.pos.x},${guard.pos.y},${guard.dir}`;
|
||||||
|
if (guardPositions.includes(guardPos)) {
|
||||||
|
loops++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
guardPositions.push(guardPos);
|
||||||
|
guard.pos = nextPos;
|
||||||
|
}
|
||||||
|
x++;
|
||||||
|
});
|
||||||
|
// End Part 2
|
||||||
|
|
||||||
|
console.log(positions.length);
|
||||||
|
console.log(loops);
|
||||||
34
2024/07/index.ts
Normal file
34
2024/07/index.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||||
|
|
||||||
|
const equations: Array<string> = input.split("\n");
|
||||||
|
|
||||||
|
let sum = 0;
|
||||||
|
|
||||||
|
const operators = [(a: number, b: number) => a * b, (a: number, b: number) => a + b, /** Part 2 */ (a: number, b: number) => parseInt(''+a+b)];
|
||||||
|
|
||||||
|
function recurseSum(carry: number, remaining: Array<number>, target: number, sum: string = ''): boolean {
|
||||||
|
if (!remaining.length) {
|
||||||
|
return target === carry;
|
||||||
|
}
|
||||||
|
const nextNum = remaining[0];
|
||||||
|
for (let o of operators) {
|
||||||
|
const newResult = recurseSum(o(carry, nextNum), remaining.slice(1), target);
|
||||||
|
if (newResult) {
|
||||||
|
return newResult;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
equations.forEach((eq) => {
|
||||||
|
const [testStr, numStr] = eq.split(':');
|
||||||
|
const test = parseInt(testStr);
|
||||||
|
const nums = numStr.trim().split(' ').map((n) => parseInt(n));
|
||||||
|
if (recurseSum(nums[0], nums.slice(1), test)) {
|
||||||
|
sum += test;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(sum);
|
||||||
Reference in New Issue
Block a user