Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
For example:
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
In this example, the calibration values of these four lines are 12
, 38
, 15
, and 77
. Adding these together produces 142
.
Consider your entire calibration document. What is the sum of all of the calibration values?
const solution = (data: string) => {
return (
data
// Split the data into lines
.split("\n")
// For each line, find first and last digit
.map((line) => {
const digits = line.match(/d/g);
const first = digits?.at(0);
const last = digits?.at(-1);
if (first === undefined || last === undefined) {
return null;
}
const parsedFirst = parseInt(first);
const parsedLast = parseInt(last);
if (isNaN(parsedFirst) || isNaN(parsedLast)) {
return null;
}
return [parsedFirst, parsedLast] as [number, number];
})
// Filter out nulls for invalid lines
.filter((x) => x !== null)
// Casting to non-null array because filter does not change the type
.map((x) => x as NonNullable<typeof x>)
// Map two digits into one number
.map(([first, last]) => parseInt(`${first}${last}`))
// Sum all numbers
.reduce((acc, x) => acc + x, 0)
);
};
You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile of leaves. It's quite cold, but you don't see much snow. An Elf runs over to greet you.
The Elf explains that you've arrived at Snow Island and apologizes for the lack of snow. He'll be happy to explain the situation, but it's a bit of a walk, so you have some time. They don't get many visitors up here; would you like to play a game in the meantime?
As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to figure out information about the number of cubes.
To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random cubes, show them to you, and then put them back in the bag. He'll do this a few times per game.
You play several games and record the information from each game (your puzzle input). Each game is listed with its ID number (like the 11
in Game 11: ...
) followed by a semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 red, 5 green, 4 blue
).
For example, the record of a few games might look like this:
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
In game 1, three sets of cubes are revealed from the bag (and then put back again). The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is only 2 green cubes.
The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?
In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly, game 4 would also have been impossible because the Elf showed you 15 blue cubes at once. If you add up the IDs of the games that would have been possible, you get 8
.
Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those games?
const solution = (data: string) => {
const maxRedCubes = 12;
const maxGreenCubes = 13;
const maxBlueCubes = 14;
return (
data
// Split the data into lines
.split("\n")
// Validate and parse the input
.map((line) => {
const isLineValid =
/Game\s\d+:((\s\d+\s(blue|red|green)(,|;|$)))+/.test(line);
if (!isLineValid) {
return null;
}
const gameId = line.match(/Game\s\d+/)!.at(0)!; // Non null assertions because we did check the validation
const parsedGameId = parseInt(gameId.replace("Game ", ""));
const rounds = line
.replace(/Game\s\d+:\s/, "")
.split(";")
.map((round) => round.trim());
const parsedRounds = rounds.map((round) => {
const redMatch = round.match(/\d+\sred/) ?? ["0 red"];
const greenMatch = round.match(/\d+\sgreen/) ?? ["0 green"];
const blueMatch = round.match(/\d+\sblue/) ?? ["0 blue"];
const red = parseInt(redMatch.at(0)!.replace(" red", "")); // Non null assertions because we did check the validation
const green = parseInt(greenMatch.at(0)!.replace(" green", "")); // Non null assertions because we did check the validation
const blue = parseInt(blueMatch.at(0)!.replace(" blue", "")); // Non null assertions because we did check the validation
return { red, green, blue };
});
return { game: parsedGameId, rounds: parsedRounds };
})
// Filter out invalid lines
.filter((line) => line !== null)
// Casting to the correct type because filter does not change the type
.map((line) => line as NonNullable<typeof line>)
// Filter out games with invalid rounds
.filter((game) => {
return game.rounds.every((round) => {
return (
round.red <= maxRedCubes &&
round.green <= maxGreenCubes &&
round.blue <= maxBlueCubes
);
});
})
// Sum the gameIds numbers
.reduce((acc, game) => {
return acc + game.game;
}, 0)
);
};
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
"Aaah!"
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.
) do not count as a symbol.)
Here is an example engine schematic:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114
(top right) and 58
(middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361
.
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?
const solution = (data: string) => {
type Coordinates = [number, number];
// Possible signs that we are searching for as neighbours
const listOfPossibleSigns = [
"*",
"#",
"$",
"@",
"%",
"&",
"=",
"+",
"-",
"/",
];
// A matrix of all the values
const matrix = data.split("\n").map((line) => line.split(""));
return (
data
// Split the data into lines
.split("\n")
// Parse the input so it includes the values and its coordinates
.map((line, row) => {
return line.split("").reduce((acc, char, column) => {
if (!/\d/.test(char)) {
return [...acc, { value: "", coordinates: [] }];
}
if (acc.length === 0) {
return [
{ value: char, coordinates: [[row, column] as Coordinates] },
];
}
const copy = structuredClone(acc);
const lastElement = copy.at(-1)!; // Asserting because we know that the array is not empty
lastElement.value += char;
lastElement.coordinates.push([row, column] as Coordinates);
return copy;
}, [] as { value: string; coordinates: Coordinates[] }[]);
})
// Flattening the lines
.flat(1)
// Filtering items with empty value
.filter((item) => item.value !== "")
// Adding the signs neighbours to each item
.map((item) => {
return {
...item,
neighbours: item.coordinates
.map(([row, column]) => {
return [
matrix[row - 1]?.[column - 1],
matrix[row - 1]?.[column],
matrix[row - 1]?.[column + 1],
matrix[row]?.[column - 1],
matrix[row]?.[column + 1],
matrix[row + 1]?.[column - 1],
matrix[row + 1]?.[column],
matrix[row + 1]?.[column + 1],
]
.filter((value) => value !== undefined)
.filter((value) => listOfPossibleSigns.includes(value));
})
.flat(1),
};
})
// Filtering items that have no neighbours
.filter((item) => item.neighbours.length > 0)
// Parsing the items values to numbers
.map((item) => parseInt(item.value))
// Summing the values
.reduce((acc, value) => acc + value, 0)
);
};