diff --git a/src/2023/day03.html b/src/2023/day03.html new file mode 100644 index 00000000..abf23d6a --- /dev/null +++ b/src/2023/day03.html @@ -0,0 +1,83 @@ + + + + + Day 3 - Advent of Code 2023 + + + + + + +
+
+

Advent of Code

+ +
Shahar Talmi (AoC++) 50*
+
+
+

   $year=2023;

+
+
+
+

--- Day 3: Gear Ratios ---

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?

+
+

Your puzzle answer was 528799.

--- Part Two ---

The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.

+

You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.

+

Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.

+

The missing part wasn't the only issue - one of the gears in the engine is wrong. A gear is any * symbol that is adjacent to exactly two part numbers. Its gear ratio is the result of multiplying those two numbers together.

+

This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.

+

Consider the same engine schematic again:

+
467..114..
+...*......
+..35..633.
+......#...
+617*......
+.....+.58.
+..592.....
+......755.
+...$.*....
+.664.598..
+
+

In this schematic, there are two gears. The first is in the top left; it has part numbers 467 and 35, so its gear ratio is 16345. The second gear is in the lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces 467835.

+

What is the sum of all of the gear ratios in your engine schematic?

+
+

Your puzzle answer was 84907174.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + diff --git a/src/2023/day03.js b/src/2023/day03.js index 4278c731..744d2a4d 100644 --- a/src/2023/day03.js +++ b/src/2023/day03.js @@ -1,100 +1,51 @@ -function signed(map, i, j) { - const neighbors = [ - map[i - 1]?.[j - 1], - map[i - 1]?.[j], - map[i - 1]?.[j + 1], - map[i]?.[j - 1], - map[i]?.[j + 1], - map[i + 1]?.[j - 1], - map[i + 1]?.[j], - map[i + 1]?.[j + 1], - ]; - return neighbors.some(n => n !== '.' && (n < '0' || n > '9')); +function signed(map, i, j, check = c => c && c !== '.' && Number.isNaN(+c)) { + return [ + { pos: `${i - 1},${j - 1}`, c: map[i - 1]?.[j - 1] }, + { pos: `${i - 1},${j}`, c: map[i - 1]?.[j] }, + { pos: `${i - 1},${j + 1}`, c: map[i - 1]?.[j + 1] }, + { pos: `${i},${j - 1}`, c: map[i]?.[j - 1] }, + { pos: `${i},${j + 1}`, c: map[i]?.[j + 1] }, + { pos: `${i + 1},${j - 1}`, c: map[i + 1]?.[j - 1] }, + { pos: `${i + 1},${j}`, c: map[i + 1]?.[j] }, + { pos: `${i + 1},${j + 1}`, c: map[i + 1]?.[j + 1] }, + ].find(({ c }) => check(c))?.pos; } -function gear(map, i, j) { - const neighbors = [ - { x: i - 1, y: j - 1, c: map[i - 1]?.[j - 1] }, - { x: i - 1, y: j, c: map[i - 1]?.[j] }, - { x: i - 1, y: j + 1, c: map[i - 1]?.[j + 1] }, - { x: i, y: j - 1, c: map[i]?.[j - 1] }, - { x: i, y: j + 1, c: map[i]?.[j + 1] }, - { x: i + 1, y: j - 1, c: map[i + 1]?.[j - 1] }, - { x: i + 1, y: j, c: map[i + 1]?.[j] }, - { x: i + 1, y: j + 1, c: map[i + 1]?.[j + 1] }, - ]; - const gears = neighbors.filter(n => n.c === '*'); - if (gears.length > 1) { - console.log(gears); //? - } - return gears.length === 0 ? undefined : `${gears[0].x},${gears[0].y}`; -} - -export function part1(input) { +function parse(input, check) { const map = input.split('\n').map(line => line.split('')); - let current = ''; - let sign = false; - let sum = 0; + let signs = {}; for (let i = 0; i < map.length; i++) { - const line = map[i]; - for (let j = 0; j < line.length; j++) { - const char = line[j]; - if (char >= '0' && char <= '9') { - current += char; - if (signed(map, i, j)) { - sign = true; - } + let current = ''; + let pos = undefined; + for (let j = 0; j < map[i].length; j++) { + if (Number.isInteger(+map[i][j])) { + current += map[i][j]; + pos = pos || signed(map, i, j, check); } else { - if (current) { - if (sign) sum += +current; - current = ''; - sign = false; + if (current && pos) { + signs[pos] = signs[pos] ? signs[pos].concat(+current) : [+current]; } + current = ''; + pos = undefined; } } - if (current) { - if (sign) sum += +current; - current = ''; - sign = false; + if (current && pos) { + signs[pos] = signs[pos] ? signs[pos].concat(+current) : [+current]; } } - return sum; + return signs; +} + +export function part1(input) { + const signs = parse(input); + return Object.values(signs) + .flat() + .reduce((a, b) => a + b, 0); } export function part2(input) { - const map = input.split('\n').map(line => line.split('')); - let current = ''; - let pos = undefined; - let gears = {}; - for (let i = 0; i < map.length; i++) { - const line = map[i]; - for (let j = 0; j < line.length; j++) { - const char = line[j]; - if (char >= '0' && char <= '9') { - current += char; - pos = pos || gear(map, i, j); - } else { - if (current) { - if (pos) - gears[pos] = gears[pos] ? gears[pos].concat(current) : [+current]; - current = ''; - pos = undefined; - } - } - } - if (current) { - if (pos) - gears[pos] = gears[pos] ? gears[pos].concat(current) : [+current]; - current = ''; - pos = undefined; - } - } - gears = Object.values(gears).map(gear => { - if (gear.length > 1) { - return gear.reduce((a, b) => a * b, 1); - } else { - return 0; - } - }); - return Object.values(gears).reduce((a, b) => a + b, 0); + const gears = parse(input, c => c === '*'); + return Object.values(gears) + .map(gear => (gear.length > 1 ? gear.reduce((a, b) => a * b, 1) : 0)) + .reduce((a, b) => a + b, 0); } diff --git a/src/2023/events.html b/src/2023/events.html index 423a08ec..63691c12 100644 --- a/src/2023/events.html +++ b/src/2023/events.html @@ -32,7 +32,7 @@

   $year=<
+

Total stars: 406*

diff --git a/src/2023/index.html b/src/2023/index.html index d60bae9f..d35ffa4b 100644 --- a/src/2023/index.html +++ b/src/2023/index.html @@ -31,10 +31,11 @@

   $year=<

 
@@ -53,17 +54,13 @@ 

   $year=< - - - - - - + + + + '.. - - *..' 3 ** ----@ '''..*......''' 2 ** * ! /^\ 1 **

diff --git a/src/2023/leaderboard.html b/src/2023/leaderboard.html index 0468a1e6..35426310 100644 --- a/src/2023/leaderboard.html +++ b/src/2023/leaderboard.html @@ -37,7 +37,7 @@

   $year=<