Last active
December 18, 2024 11:15
-
-
Save MeFoDy/abafd39c0692396dd64108bf69290791 to your computer and use it in GitHub Desktop.
Advent of Code 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = [], b = []; | |
pairs.split('\n').forEach(line => { | |
const x = line.split(' '); | |
const [left, right] = [+x[0], +x[1]]; | |
a.push(left); | |
b.push(right); | |
}) | |
a.sort((a,b) => a-b); | |
b.sort((a,b) => a-b); | |
let sum = 0; | |
a.forEach((el, i) => { | |
sum += Math.abs(a[i] - b[i]) | |
}) | |
console.log(sum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = [], b = []; | |
pairs.split('\n').forEach(line => { | |
const x = line.split(' '); | |
const [left, right] = [+x[0], +x[1]]; | |
a.push(left); | |
b.push(right); | |
}) | |
let sum = 0; | |
a.forEach((el, i) => { | |
let count = b.reduce((acc, c) => { | |
if (c === el) return acc + 1; | |
return acc; | |
}, 0); | |
sum += count * el; | |
}) | |
console.log(sum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sum = 0; | |
function direction(n1, n2) { | |
if (n1 === n2) return 0; | |
return Math.abs(n1 - n2) / (n1 - n2); | |
} | |
function isSafe(numbers) { | |
let dir = direction(numbers[1], numbers[0]); | |
const l = numbers.length; | |
for (let i = 0; i < l - 1; i++) { | |
if (dir !== direction(numbers[i + 1], numbers[i])) return false; | |
const diff = Math.abs(numbers[i] - numbers[i + 1]); | |
if (!(diff >= 1 && diff <= 3)) return false; | |
} | |
return true; | |
} | |
reports.split('\n').forEach(report => { | |
const numbers = report.split(' ').map(x => +x); | |
if (isSafe(numbers)) sum++; | |
}) | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sum = 0; | |
function direction(n1, n2) { | |
if (n1 === n2) | |
return 0; | |
return Math.abs(n1 - n2) / (n1 - n2); | |
} | |
function isSafe(numbers) { | |
let dir = direction(numbers[1], numbers[0]); | |
const l = numbers.length; | |
for (let i = 0; i < l - 1; i++) { | |
if (dir !== direction(numbers[i + 1], numbers[i])) | |
return false; | |
const diff = Math.abs(numbers[i] - numbers[i + 1]); | |
if (!(diff >= 1 && diff <= 3)) | |
return false; | |
} | |
return true; | |
} | |
function isAlmostSafe(numbers) { | |
const varNumbers = numbers.map( (source, i) => { | |
return numbers.toSpliced(i, 1); | |
} | |
) | |
if (varNumbers.some(n => isSafe(n))) | |
return true; | |
return false; | |
} | |
reports.split('\n').forEach(report => { | |
const numbers = report.split(' ').map(x => +x); | |
if (isSafe(numbers)) | |
sum++ | |
else if (isAlmostSafe(numbers)) | |
sum++ | |
} | |
) | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sum = 0; | |
pairs.split('\n').forEach(report => { | |
const numbers = report.split(',').map(x => +x); | |
sum += numbers[0] * numbers[1]; | |
}) | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pattern = 'XMAS'; | |
let sum = 0; | |
const matrix = []; | |
text.split('\n').forEach((line, i) => { | |
const letters = line.split(''); | |
const lineArray = []; | |
letters.forEach(l => { lineArray.push(l); }); | |
matrix.push(lineArray); | |
}); | |
function get(m, i, j) { | |
if (!m[i]) return ''; | |
if (!m[i][j]) return ''; | |
return m[i][j]; | |
} | |
const directions = [ | |
(i, j) => `${get(matrix, i, j)}${get(matrix, i-1, j-1)}${get(matrix, i-2, j-2)}${get(matrix, i-3, j-3)}`, | |
(i, j) => `${get(matrix, i, j)}${get(matrix, i-1, j)}${get(matrix, i-2, j)}${get(matrix, i-3, j)}`, | |
(i, j) => `${get(matrix, i, j)}${get(matrix, i, j-1)}${get(matrix, i, j-2)}${get(matrix, i, j-3)}`, | |
(i, j) => `${get(matrix, i, j)}${get(matrix, i+1, j-1)}${get(matrix, i+2, j-2)}${get(matrix, i+3, j-3)}`, | |
(i, j) => `${get(matrix, i, j)}${get(matrix, i+1, j+1)}${get(matrix, i+2, j+2)}${get(matrix, i+3, j+3)}`, | |
(i, j) => `${get(matrix, i, j)}${get(matrix, i-1, j+1)}${get(matrix, i-2, j+2)}${get(matrix, i-3, j+3)}`, | |
(i, j) => `${get(matrix, i, j)}${get(matrix, i, j+1)}${get(matrix, i, j+2)}${get(matrix, i, j+3)}`, | |
(i, j) => `${get(matrix, i, j)}${get(matrix, i+1, j)}${get(matrix, i+2, j)}${get(matrix, i+3, j)}`, | |
]; | |
const height = matrix.length; | |
const width = matrix[0].length; | |
for (let i=0; i<height; i++) { | |
for (let j=0; j<width; j++) { | |
sum += directions.filter(d => d(i, j) == pattern).length; | |
} | |
} | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pattern = 'MAS'; | |
let sum = 0; | |
const matrix = []; | |
text.split('\n').forEach((line, i) => { | |
const letters = line.split(''); | |
const lineArray = []; | |
letters.forEach(l => { lineArray.push(l); }); | |
matrix.push(lineArray); | |
}); | |
function get(m, i, j) { | |
if (!m[i]) return ''; | |
if (!m[i][j]) return ''; | |
return m[i][j]; | |
} | |
const directions = [ | |
(i, j) => `${get(matrix, i-1, j-1)}${get(matrix, i, j)}${get(matrix, i+1, j+1)}`, | |
(i, j) => `${get(matrix, i+1, j-1)}${get(matrix, i, j)}${get(matrix, i-1, j+1)}`, | |
(i, j) => `${get(matrix, i-1, j+1)}${get(matrix, i, j)}${get(matrix, i+1, j-1)}`, | |
(i, j) => `${get(matrix, i+1, j+1)}${get(matrix, i, j)}${get(matrix, i-1, j-1)}`, | |
]; | |
const height = matrix.length; | |
const width = matrix[0].length; | |
for (let i=0; i<height; i++) { | |
for (let j=0; j<width; j++) { | |
sum += directions.filter(d => d(i, j) == pattern).length > 1 ? 1 : 0; | |
} | |
} | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rulesNumbers = rules.split('\n').map(rule => { | |
const [left, right] = rule.split('|').map(x => +x); | |
return { | |
left, | |
right | |
} | |
}) | |
const updatesNumbers = updates.split('\n').map(update => { | |
return update.split(',').map(x => +x); | |
}) | |
const isSafeLine = (line) => { | |
const needToCheck = rulesNumbers.filter(rule => line.includes(rule.left) && line.includes(rule.right)); | |
if (needToCheck.length === 0) return true; | |
return needToCheck.every(rule => { | |
const leftPos = line.indexOf(rule.left); | |
const rightPos = line.indexOf(rule.right); | |
return leftPos < rightPos; | |
}); | |
} | |
const sum = updatesNumbers.filter(isSafeLine).reduce((acc, c) => { | |
acc += c[Math.trunc(c.length / 2)]; | |
return acc; | |
}, 0); | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rulesNumbers = rules.split('\n').map(rule => { | |
const [left, right] = rule.split('|').map(x => +x); | |
return { | |
left, | |
right | |
} | |
}) | |
const updatesNumbers = updates.split('\n').map(update => { | |
return update.split(',').map(x => +x); | |
}) | |
const isSafeLine = (line) => { | |
const needToCheck = rulesNumbers.filter(rule => line.includes(rule.left) && line.includes(rule.right)); | |
if (needToCheck.length === 0) return true; | |
return needToCheck.every(rule => { | |
const leftPos = line.indexOf(rule.left); | |
const rightPos = line.indexOf(rule.right); | |
return leftPos < rightPos; | |
}); | |
} | |
const unsafeLines = updatesNumbers.filter(nums => !isSafeLine(nums)); | |
let sum = unsafeLines.reduce((acc, c) => { | |
const sorted = c.toSorted((a, b) => { | |
const needToCheck = rulesNumbers.filter(rule => b === rule.left && a === rule.right); | |
if (needToCheck.length === 0) return 0; | |
return -1; | |
}).reverse(); | |
acc += sorted[Math.trunc(sorted.length / 2)]; | |
return acc; | |
}, 0); | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const map = source.split('\n'); | |
const h = map.length; | |
const w = map[0].length; | |
function nextPosition(dir, y, x) { | |
switch (dir) { | |
case 1: // up | |
return [y - 1, x]; | |
case 2: // right | |
return [y, x + 1]; | |
case 3: // down | |
return [y + 1, x]; | |
case 4: // left | |
return [y, x - 1]; | |
} | |
} | |
function nextDirection(dir) { | |
return dir % 4 + 1; | |
} | |
function getGuardPos() { | |
for (let j=0; j<h; j++) { | |
for (let i=0; i<w; i++) { | |
if (map[j][i] === '^') { | |
return [j, i]; | |
} | |
} | |
} | |
} | |
function getVisitedPositionsCount() { | |
let sum = 0; | |
for (let j=0; j<h; j++) { | |
for (let i=0; i<w; i++) { | |
if (map[j][i] === 'X') { | |
sum++; | |
} | |
} | |
} | |
return sum; | |
} | |
const replaceAt = function(string, index, replacement) { | |
return string.substring(0, index) + replacement + string.substring(index + replacement.length); | |
} | |
let guard = getGuardPos(); | |
let pos = getGuardPos(); | |
let dir = 1; | |
while (true) { | |
const nextPos = nextPosition(dir, guard[0], guard[1]); | |
if (nextPos[0] < 0 || nextPos[0] >= h || nextPos[1] < 0 || nextPos[1] >= w) { | |
map[guard[0]] = replaceAt(map[guard[0]], guard[1], 'X'); | |
break; | |
} | |
if (map[nextPos[0]][nextPos[1]] === '#') { | |
dir = nextDirection(dir); | |
continue; | |
} | |
map[guard[0]] = replaceAt(map[guard[0]], guard[1], 'X'); | |
guard[0] = nextPos[0]; | |
guard[1] = nextPos[1]; | |
} | |
console.log(getVisitedPositionsCount()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const map = source.split('\n'); | |
const h = map.length; | |
const w = map[0].length; | |
function nextPosition(dir, y, x) { | |
switch (dir) { | |
case 1: | |
// up | |
return [y - 1, x]; | |
case 2: | |
// right | |
return [y, x + 1]; | |
case 3: | |
// down | |
return [y + 1, x]; | |
case 4: | |
// left | |
return [y, x - 1]; | |
} | |
} | |
function nextDirection(dir) { | |
return dir % 4 + 1; | |
} | |
function getGuardPos() { | |
for (let j = 0; j < h; j++) { | |
for (let i = 0; i < w; i++) { | |
if (map[j][i] === '^') { | |
return [j, i]; | |
} | |
} | |
} | |
} | |
const replaceAt = function(string, index, replacement) { | |
return string.substring(0, index) + replacement + string.substring(index + replacement.length); | |
} | |
const guardStartPosition = getGuardPos(); | |
const testMap = (y, x) => { | |
if (map[y][x] !== '.') | |
return false; | |
map[y] = replaceAt(map[y], x, '#'); | |
let guard = [guardStartPosition[0], guardStartPosition[1]]; | |
let dir = 1; | |
let isObstacleVisited = false; | |
let visited = new Set(); | |
while (true) { | |
let state = `${guard[0]},${guard[1]},${dir}`; | |
if (!visited.has(state)) { | |
visited.add(state); | |
} else { | |
map[y] = replaceAt(map[y], x, '.'); | |
return true; | |
} | |
const nextPos = nextPosition(dir, guard[0], guard[1]); | |
if (nextPos[0] < 0 || nextPos[0] >= h || nextPos[1] < 0 || nextPos[1] >= w) { | |
map[y] = replaceAt(map[y], x, '.'); | |
return false; | |
} | |
if (map[nextPos[0]][nextPos[1]] === '#') { | |
dir = nextDirection(dir); | |
continue; | |
} | |
guard[0] = nextPos[0]; | |
guard[1] = nextPos[1]; | |
} | |
}; | |
let sum = 0; | |
for (let j = 0; j < h; j++) { | |
for (let i = 0; i < w; i++) { | |
console.log(j, i); | |
if (testMap(j, i)) { | |
sum++; | |
} | |
} | |
} | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lines = source.split('\n'); | |
const transform = (line) => { | |
const parts = line.split(': '); | |
return { | |
result: +parts[0], | |
nums: parts[1].split(' ').map(x => +x), | |
} | |
} | |
const operations = { | |
'+': (a, b) => a + b, | |
'*': (a, b) => a * b, | |
} | |
function isCalibrated(line) { | |
let variants = ['+', '*']; | |
const l = line.nums.length - 1; | |
for (i=1; i<l; i++) { | |
variants = variants.reduce((acc, v) => { | |
acc.push(v + '+'); | |
acc.push(v + '*'); | |
return acc; | |
}, []); | |
} | |
const result = variants.some(variant => { | |
const sum = variant.split('').reduce((acc, c, i) => { | |
return operations[c](acc, line.nums[i+1]); | |
}, line.nums[0]); | |
return sum === line.result; | |
}); | |
return result; | |
} | |
const sum = lines.map(transform).filter(isCalibrated).reduce((acc, c) => acc + c.result, 0); | |
console.log(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lines = source.split('\n'); | |
const transform = (line) => { | |
const parts = line.split(': '); | |
return { | |
result: +parts[0], | |
nums: parts[1].split(' ').map(x => +x), | |
} | |
} | |
const operations = { | |
'+': (a, b) => a + b, | |
'*': (a, b) => a * b, | |
'|': (a, b) => +`${a}${b}`, | |
} | |
function isCalibrated(line) { | |
let variants = ['+', '*', '|']; | |
const l = line.nums.length - 1; | |
for (i=1; i<l; i++) { | |
variants = variants.reduce((acc, v) => { | |
acc.push(v + '+'); | |
acc.push(v + '*'); | |
acc.push(v + '|'); | |
return acc; | |
}, []); | |
} | |
const result = variants.some(variant => { | |
const sum = variant.split('').reduce((acc, c, i) => { | |
return operations[c](acc, line.nums[i+1]); | |
}, line.nums[0]); | |
return sum === line.result; | |
}); | |
return result; | |
} | |
const sum = lines.map(transform).filter(isCalibrated).reduce((acc, c) => acc + c.result, 0); | |
console.log(sum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment