Skip to content

Instantly share code, notes, and snippets.

@Xiphe
Last active December 8, 2023 22:50
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 Xiphe/51e18796cfb30d2564fcfb8693b639ac to your computer and use it in GitHub Desktop.
Save Xiphe/51e18796cfb30d2564fcfb8693b639ac to your computer and use it in GitHub Desktop.
Advent Of Code 2023 in JS without keywords and reassignments
`1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet`
.split("\n")
.reduce(
(sum, line) =>
sum +
parseInt(
(line + line)
.match(/(\d).*(\d)/)
.slice(1, 3)
.join(""),
),
0,
);
`two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen`
.split("\n")
.reduce(
(sum, line) =>
sum +
parseInt(
(line + line)
.replace(/(one)/g, `$11$1`)
.replace(/(two)/g, `$12$1`)
.replace(/(three)/g, `$13$1`)
.replace(/(four)/g, `$14$1`)
.replace(/(five)/g, `$15$1`)
.replace(/(six)/g, `$16$1`)
.replace(/(seven)/g, `$17$1`)
.replace(/(eight)/g, `$18$1`)
.replace(/(nine)/g, `$19$1`)
.match(/(\d).*(\d)/)
.slice(1, 3)
.join(""),
),
0,
);
`Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`
.split("\n")
.reduce(
(state, games) => ({
...state,
sum:
state.sum +
games
.match(/Game (\d+): (.+)/)
.slice(1, 3)
.reduce(
(id, match, i) =>
i === 0
? parseInt(match)
: match.split(";").find((round) =>
round.split(",").find((cubes) =>
cubes
.match(/(\d+) (\w+)/)
.slice(1, 3)
.reduce(
(amount, match, i) =>
i === 0
? parseInt(match)
: state.constraint[match] < amount,
0
)
)
)
? 0
: id,
0
),
}),
{ sum: 0, constraint: { red: 12, green: 13, blue: 14 } }
).sum;
`Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`
.split("\n")
.reduce(
(sum, game) =>
sum +
Object.values(
game
.split(":")[1]
.split(";")
.reduce(
(mins, round) =>
round.split(",").reduce(
(mins, cubes) => ({
...mins,
...cubes
.match(/(\d+) (\w+)/)
.slice(1, 3)
.reduce(
(amount, match, i) =>
i === 0
? parseInt(match)
: { [match]: Math.max(mins[match], amount) },
0
),
}),
mins
),
{ red: 0, blue: 0, green: 0 }
)
).reduce((power, min) => power * min, 1),
0
);
`467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..`
.split("\n")
.reduce(
(sum, line, i, lines) =>
Array.from(line.matchAll(/\d+/g)).reduce(
(sum, match) =>
sum +
parseInt(
(lines
.slice(Math.max(0, i - 1), i + 2)
.find((line) =>
line
.slice(
Math.max(0, match.index - 1),
match.index + match[0].length + 1
)
.match(/[^\d.]/)
) &&
match[0]) ||
"0"
),
sum
),
0
);
`467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..`
.split("\n")
.reduce(
(potentialGears, line, i, lines) =>
[
Array.from(line.matchAll(/\d+/g)).reduce(
(potentialGears, match) => ({
...potentialGears,
...[
lines
.slice(Math.max(0, i - 1), i + 2)
.map((line, i) => [
line
.slice(
Math.max(0, match.index - 1),
match.index + match[0].length + 1
)
.match(/[^\d.]/) || [],
i,
])
.find((t) => t[0][0] === "*"),
]
.map((t) =>
t
? `${t[1] + Math.max(0, i - 1)}:${
t[0].index + Math.max(0, match.index - 1)
}`
: 0
)
.map((key) =>
!key
? {}
: {
[key]: [
...(potentialGears[key] || []),
parseInt(match[0]),
],
}
)[0],
}),
potentialGears
),
].map((potentialGears) =>
i < lines.length - 1
? potentialGears
: Object.values(potentialGears).reduce(
(sum, partNumbers) =>
sum +
(partNumbers.length === 2
? partNumbers.reduce((p, n) => p * n, 1)
: 0),
0
)
)[0],
{}
);
`Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11`
.split("\n")
.reduce(
(points, card) =>
points +
card
.split(":")[1]
.split("|")
.map((row) => row.match(/\d+/g).map((n) => parseInt(n)))
.reduce((winningNumbers, numbers, i) =>
i === 0
? numbers
: numbers.reduce(
(sum, number) =>
winningNumbers.includes(number)
? sum === 0
? 1
: sum * 2
: sum,
0
)
),
0
);
`Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11`
.split("\n")
.reduce(
([copies], card, cardNumber) => [
{
...copies,
[cardNumber]: (copies[cardNumber] || 0) + 1,
...card
.split(":")[1]
.split("|")
.map((row) => row.match(/\d+/g).map((n) => parseInt(n)))
.reduce(
(winningNumbers, numbers, i) =>
i === 0
? numbers
: numbers
.filter((number) => winningNumbers.includes(number))
.reduce(
(adds, _, j) => ({
...adds,
[cardNumber + j + 1]:
(copies[cardNumber + j + 1] || 0) +
(copies[cardNumber] || 0) +
1,
}),
{}
),
[]
),
},
],
[{}]
)
.flatMap((c) => Object.values(c))
.reduce((total, cardCopies) => total + cardCopies, 0);
`seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4`
.split("\n\n")
.reduce(
(state, input, i) =>
i === 0
? input.match(/\d+/g).map(Number)
: [
input
.split("\n")
.slice(1)
.map((row) => row.match(/\d+/g).map(Number)),
].reduce(
(state, ranges) =>
state.map(
(id) =>
id +
(
ranges.find(
([_, source, length]) =>
id >= source && id < source + length
) || [0, 0, 0]
)
.slice(0, 2)
.reduce((a, b) => a - b)
),
state
),
[]
)
.reduce((a, b) => Math.min(a, b), Infinity);
`seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4`
.split("\n\n")
.reduce(
(state, input, i) =>
i === 0
? input
.match(/\d+/g)
.map(Number)
.reduce(
(state, n, i, ranges) =>
i % 2 === 0
? state
: [
...state,
...Array.from({ length: n }, (_, j) => ranges[i - 1] + j),
],
[]
)
: [
input
.split("\n")
.slice(1)
.map((row) => row.match(/\d+/g).map(Number)),
].reduce(
(state, ranges) =>
state.map(
(id) =>
id +
(
ranges.find(
([_, source, length]) =>
id >= source && id < source + length
) || [0, 0, 0]
)
.slice(0, 2)
.reduce((a, b) => a - b)
),
state
),
[]
)
.reduce((a, b) => Math.min(a, b), Infinity);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment