diff --git a/2024/05/index.ts b/2024/05/index.ts new file mode 100644 index 0000000..30b5109 --- /dev/null +++ b/2024/05/index.ts @@ -0,0 +1,38 @@ +const fs = require('fs'); + +const input = fs.readFileSync(__dirname + '/input.txt', 'utf8'); + +const rules: Array<[number, number]> = []; +const updates: Array> = []; + +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); \ No newline at end of file diff --git a/2024/05/index2.ts b/2024/05/index2.ts new file mode 100644 index 0000000..2e4a8bd --- /dev/null +++ b/2024/05/index2.ts @@ -0,0 +1,42 @@ +const fs = require('fs'); + +const input = fs.readFileSync(__dirname + '/input.txt', 'utf8'); + +const rules: Array<[number, number]> = []; +const updates: Array> = []; + +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 = []; + 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); \ No newline at end of file diff --git a/2024/06/index.ts b/2024/06/index.ts new file mode 100644 index 0000000..d66a224 --- /dev/null +++ b/2024/06/index.ts @@ -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 = []; +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 = [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|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 = []; + 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); \ No newline at end of file diff --git a/2024/07/index.ts b/2024/07/index.ts new file mode 100644 index 0000000..57dd795 --- /dev/null +++ b/2024/07/index.ts @@ -0,0 +1,34 @@ +const fs = require('fs'); + +const input = fs.readFileSync(__dirname + '/input.txt', 'utf8'); + +const equations: Array = 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, 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); \ No newline at end of file