From d2c78d34b7bb82b3c42aac45906ac2fdb2e3d819 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Thu, 14 Dec 2023 08:39:46 +0200 Subject: [PATCH] refactor --- src/2023/day01.html | 2 +- src/2023/day02.html | 2 +- src/2023/day03.html | 2 +- src/2023/day04.html | 2 +- src/2023/day05.html | 2 +- src/2023/day06.html | 2 +- src/2023/day07.html | 2 +- src/2023/day08.html | 2 +- src/2023/day09.html | 2 +- src/2023/day10.html | 2 +- src/2023/day11.html | 2 +- src/2023/day12.html | 2 +- src/2023/day13.html | 2 +- src/2023/day14.html | 129 ++++++++++++++++++++ src/2023/day14.js | 90 ++++---------- src/2023/events.html | 6 +- src/2023/index.html | 31 ++--- src/2023/leaderboard.html | 199 +++++++++++++++++++++++++++++-- src/2023/solver.html | 2 +- src/static/events-screenshot.png | Bin 76391 -> 76471 bytes src/static/solver-screenshot.png | Bin 62059 -> 62074 bytes 21 files changed, 376 insertions(+), 107 deletions(-) create mode 100644 src/2023/day14.html diff --git a/src/2023/day01.html b/src/2023/day01.html index 811b028a..94585c0e 100644 --- a/src/2023/day01.html +++ b/src/2023/day01.html @@ -22,7 +22,7 @@

Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year= + + + + Day 14 - Advent of Code 2023 + + + + + + +
+
+

Advent of Code

+ +
Shahar Talmi (AoC++) 28*
+
+
+

   $year=2023;

+
+
+
+

--- Day 14: Parabolic Reflector Dish ---

You reach the place where all of the mirrors were pointing: a massive parabolic reflector dish attached to the side of another large mountain.

+

The dish is made up of many small mirrors, but while the mirrors themselves are roughly in the shape of a parabolic reflector dish, each individual mirror seems to be pointing in slightly the wrong direction. If the dish is meant to focus light, all it's doing right now is sending it in a vague direction.

+

This system must be what provides the energy for the lava! If you focus the reflector dish, maybe you can go where it's pointing and use the light to fix the lava production.

+

Upon closer inspection, the individual mirrors each appear to be connected via an elaborate system of ropes and pulleys to a large metal platform below the dish. The platform is covered in large rocks of various shapes. Depending on their position, the weight of the rocks deforms the platform, and the shape of the platform controls which ropes move and ultimately the focus of the dish.

+

In short: if you move the rocks, you can focus the dish. The platform even has a control panel on the side that lets you tilt it in one of four directions! The rounded rocks (O) will roll when the platform is tilted, while the cube-shaped rocks (#) will stay in place. You note the positions of all of the empty spaces (.) and rocks (your puzzle input). For example:

+
O....#....
+O.OO#....#
+.....##...
+OO.#O....O
+.O.....O#.
+O.#..O.#.#
+..O..#O..O
+.......O..
+#....###..
+#OO..#....
+
+

Start by tilting the lever so all of the rocks will slide north as far as they will go:

+
OOOO.#.O..
+OO..#....#
+OO..O##..O
+O..#.OO...
+........#.
+..#....#.#
+..O..#.O.O
+..O.......
+#....###..
+#....#....
+
+

You notice that the support beams along the north side of the platform are damaged; to ensure the platform doesn't collapse, you should calculate the total load on the north support beams.

+

The amount of load caused by a single rounded rock (O) is equal to the number of rows from the rock to the south edge of the platform, including the row the rock is on. (Cube-shaped rocks (#) don't contribute to load.) So, the amount of load caused by each rock in each row is as follows:

+
OOOO.#.O.. 10
+OO..#....#  9
+OO..O##..O  8
+O..#.OO...  7
+........#.  6
+..#....#.#  5
+..O..#.O.O  4
+..O.......  3
+#....###..  2
+#....#....  1
+
+

The total load is the sum of the load caused by all of the rounded rocks. In this example, the total load is 136.

+

Tilt the platform so that the rounded rocks all roll north. Afterward, what is the total load on the north support beams?

+
+

Your puzzle answer was 105784.

--- Part Two ---

The parabolic reflector dish deforms, but not in a way that focuses the beam. To do that, you'll need to move the rocks to the edges of the platform. Fortunately, a button on the side of the control panel labeled "spin cycle" attempts to do just that!

+

Each cycle tilts the platform four times so that the rounded rocks roll north, then west, then south, then east. After each tilt, the rounded rocks roll as far as they can before the platform tilts in the next direction. After one cycle, the platform will have finished rolling the rounded rocks in those four directions in that order.

+

Here's what happens in the example above after each of the first few cycles:

+
After 1 cycle:
+.....#....
+....#...O#
+...OO##...
+.OO#......
+.....OOO#.
+.O#...O#.#
+....O#....
+......OOOO
+#...O###..
+#..OO#....
+
+After 2 cycles:
+.....#....
+....#...O#
+.....##...
+..O#......
+.....OOO#.
+.O#...O#.#
+....O#...O
+.......OOO
+#..OO###..
+#.OOO#...O
+
+After 3 cycles:
+.....#....
+....#...O#
+.....##...
+..O#......
+.....OOO#.
+.O#...O#.#
+....O#...O
+.......OOO
+#...O###.O
+#.OOO#...O
+
+

This process should work if you leave it running long enough, but you're still worried about the north support beams. To make sure they'll survive for a while, you need to calculate the total load on the north support beams after 1000000000 cycles.

+

In the above example, after 1000000000 cycles, the total load on the north support beams is 64.

+

Run the spin cycle for 1000000000 cycles. Afterward, what is the total load on the north support beams?

+
+

Your puzzle answer was 91286.

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/day14.js b/src/2023/day14.js index 0886f9e6..7dddc34d 100644 --- a/src/2023/day14.js +++ b/src/2023/day14.js @@ -1,82 +1,40 @@ -function north(lines) { +function vertical(lines, add = 1) { for (let x = 0; x < lines[0].length; x++) { - let target = 0; - for (let y = 0; y < lines.length; y++) { + let target = add > 0 ? 0 : lines.length - 1; + for (let y = target; y >= 0 && y < lines.length; y += add) { if (lines[y][x] === 'O') { lines[y][x] = '.'; lines[target][x] = 'O'; - target++; - } - if (lines[y][x] === '#') { - target = y + 1; - } - } - } -} - -function west(lines) { - for (let y = 0; y < lines.length; y++) { - let target = 0; - for (let x = 0; x < lines[0].length; x++) { - if (lines[y][x] === 'O') { - lines[y][x] = '.'; - lines[y][target] = 'O'; - target++; - } - if (lines[y][x] === '#') { - target = x + 1; - } - } - } -} - -function south(lines) { - for (let x = 0; x < lines[0].length; x++) { - let target = lines.length - 1; - for (let y = lines.length - 1; y >= 0; y--) { - if (lines[y][x] === 'O') { - lines[y][x] = '.'; - lines[target][x] = 'O'; - target--; - } - if (lines[y][x] === '#') { - target = y - 1; + target += add; } + if (lines[y][x] === '#') target = y + add; } } } -function east(lines) { +function horizontal(lines, add = 1) { for (let y = 0; y < lines.length; y++) { - let target = lines[0].length - 1; - for (let x = lines[0].length - 1; x >= 0; x--) { + let target = add > 0 ? 0 : lines[0].length - 1; + for (let x = target; x >= 0 && x < lines[0].length; x += add) { if (lines[y][x] === 'O') { lines[y][x] = '.'; lines[y][target] = 'O'; - target--; - } - if (lines[y][x] === '#') { - target = x - 1; + target += add; } + if (lines[y][x] === '#') target = x + add; } } } function weight(lines) { - let result = 0; - for (let i = 0; i < lines[0].length; i++) { - for (let j = 0; j < lines.length; j++) { - if (lines[j][i] === 'O') { - result += lines.length - j; - } - } - } - return result; + return lines.reduce((acc, line, y) => { + return acc + line.filter(x => x === 'O').length * (lines.length - y); + }, 0); } export function part1(input) { const lines = input.split('\n').map(line => line.split('')); - north(lines); + vertical(lines, 1); return weight(lines); } @@ -84,19 +42,15 @@ export function part2(input) { const lines = input.split('\n').map(line => line.split('')); const memory = new Map(); const count = 1000000000; - for (let i = 0; i < count; i++) { - north(lines); - west(lines); - south(lines); - east(lines); + for (let i = 1; i <= count; i++) { + const key = lines.map(line => line.join('')).join('\n'); + if (memory.has(key)) i = count - ((count - i) % (i - memory.get(key))); + else memory.set(key, i); - const key = JSON.stringify(lines); - const index = memory.get(key); - if (index !== undefined) { - i += Math.floor((count - i - 1) / (i - index)) * (i - index); - } else { - memory.set(key, i); - } + vertical(lines, 1); + horizontal(lines, 1); + vertical(lines, -1); + horizontal(lines, -1); } return weight(lines); } diff --git a/src/2023/events.html b/src/2023/events.html index eb6bc101..fe15a7d4 100644 --- a/src/2023/events.html +++ b/src/2023/events.html @@ -22,7 +22,7 @@

Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=   $year=<
+

Total stars: 428*

diff --git a/src/2023/index.html b/src/2023/index.html index 4b652c7e..517fd1b7 100644 --- a/src/2023/index.html +++ b/src/2023/index.html @@ -22,7 +22,7 @@

Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=   $year=<
-

-
-
+
     .''....' '..'.  './\*/\ ~                      13 **
 
 .'''' ZZ* .'''.     ''''.... .. .  '               12 **
diff --git a/src/2023/leaderboard.html b/src/2023/leaderboard.html
index 3c2df2cc..e97be7c7 100644
--- a/src/2023/leaderboard.html
+++ b/src/2023/leaderboard.html
@@ -22,7 +22,7 @@ 

Advent of Code

Shahar Talmi (AoC++) 26*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 28*

   $year=   $year=<