Skip to content

Instantly share code, notes, and snippets.

@trustedtomato
Last active December 4, 2022 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trustedtomato/2d1290e6b9b30ea00646e7c3f340a717 to your computer and use it in GitHub Desktop.
Save trustedtomato/2d1290e6b9b30ea00646e7c3f340a717 to your computer and use it in GitHub Desktop.
Advent of code 2022

These are my solutions to Advent of Code 2022. They are not meant to be readable, these are just the quickest-to-write solutions I found with plain ol' Javascript.

// part 1
`1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
`.split('\n\n').map(bagStr => bagStr.split('\n').map(Number).reduce((a, b) => a + b, 0)).reduce((a, b) => Math.max(a, b))
// part 2
`1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
`.split('\n\n').map(bagStr => bagStr.split('\n').map(Number).reduce((a, b) => a + b, 0)).sort((a, b) => b-a).slice(0, 3).reduce((a, b) => a + b, 0)
// part 1
`A Y
B X
C Z`.split('\n').map(line => {
const els = line.split(' ')
const player2 = (els[0] === 'A' ? 1 : els[0] === 'B' ? 2 : els[0] === 'C' ? 3 : 0)
const player1 = (els[1] === 'X' ? 1 : els[1] === 'Y' ? 2 : els[1] === 'Z' ? 3 : 0)
return player1 + (player1 - player2 + 4) % 3 * 3
}).reduce((a, b) => a + b)
// part 2
`A Y
B X
C Z`.split('\n').map(line => {
const els = line.split(' ')
const player2 = (els[0] === 'A' ? 1 : els[0] === 'B' ? 2 : els[0] === 'C' ? 3 : 0)
const player1 = (els[1] === 'X' ? player2 + 1 : els[1] === 'Y' ? player2 + 2 : els[1] === 'Z' ? player2 : 0) % 3 + 1
return player1 + (player1 - player2 + 4) % 3 * 3
}).reduce((a,b) => a+b)
return player1 + (player1 - player2 + 4) % 3 * 3
})
// part 1
`vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw`.split('\n').flatMap(rucksack => {
const comp1 = rucksack.slice(0, rucksack.length / 2)
const comp2 = rucksack.slice(rucksack.length / 2)
return [...new Set([...comp1].filter(x => comp2.includes(x)))]
}).map(char => {
const code = char.charCodeAt(0)
if (char.toLowerCase() === char) {
return code - 97 + 1
} else {
return code - 65 + 27
}
}).reduce((a, b) => a + b)
// part 2
`vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw`.split(/(.*\n.*\n.*\n?)/).filter(x => x).map(groupsOfThree => groupsOfThree.trim().split('\n')).flatMap(([r1, r2, r3]) => {
return [...new Set([...r1].filter(x => r2.includes(x)).filter(x => r3.includes(x)))]
}).map(char => {
const code = char.charCodeAt(0)
if (char.toLowerCase() === char) {
return code - 97 + 1
} else {
return code - 65 + 27
}
})
// part 1
`2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8`.split('\n').filter(line => {
const elves = line.split(',').map(elf => elf.split('-').map(Number))
return (elves[0][0] <= elves[1][0] && elves[0][1] >= elves[1][1] || elves[1][0] <= elves[0][0] && elves[1][1] >= elves[0][1])
})
// part 2
`2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8`.split('\n').filter(line => {
const elves = line.split(',').map(elf => elf.split('-').map(Number))
return (elves[0][0] <= elves[1][0] && elves[0][1] >= elves[1][0] || elves[1][0] <= elves[0][0] && elves[1][1] >= elves[0][0])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment