diff --git a/src/2023/day01.html b/src/2023/day01.html index d4a82807..ddd8cfe6 100644 --- a/src/2023/day01.html +++ b/src/2023/day01.html @@ -22,7 +22,7 @@

Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

   $year=Advent of Code

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

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

Advent of Code

+ +
Shahar Talmi (AoC++) 46*
+
+
+

   $year=2023;

+
+
+
+

--- Day 23: A Long Walk ---

The Elves resume water filtering operations! Clean water starts flowing over the edge of Island Island.

+

They offer to help you go over the edge of Island Island, too! Just hold on tight to one end of this impossibly long rope and they'll lower you down a safe distance from the massive waterfall you just created.

+

As you finally reach Snow Island, you see that the water isn't really reaching the ground: it's being absorbed by the air itself. It looks like you'll finally have a little downtime while the moisture builds up to snow-producing levels. Snow Island is pretty scenic, even without any snow; why not take a walk?

+

There's a map of nearby hiking trails (your puzzle input) that indicates paths (.), forest (#), and steep slopes (^, >, v, and <).

+

For example:

+
#.#####################
+#.......#########...###
+#######.#########.#.###
+###.....#.>.>.###.#.###
+###v#####.#v#.###.#.###
+###.>...#.#.#.....#...#
+###v###.#.#.#########.#
+###...#.#.#.......#...#
+#####.#.#.#######.#.###
+#.....#.#.#.......#...#
+#.#####.#.#.#########v#
+#.#...#...#...###...>.#
+#.#.#v#######v###.###v#
+#...#.>.#...>.>.#.###.#
+#####v#.#.###v#.#.###.#
+#.....#...#...#.#.#...#
+#.#########.###.#.#.###
+#...###...#...#...#.###
+###.###.#.###v#####v###
+#...#...#.#.>.>.#.>.###
+#.###.###.#.###.#.#v###
+#.....###...###...#...#
+#####################.#
+
+

You're currently on the single path tile in the top row; your goal is to reach the single path tile in the bottom row. Because of all the mist from the waterfall, the slopes are probably quite icy; if you step onto a slope tile, your next step must be downhill (in the direction the arrow is pointing). To make sure you have the most scenic hike possible, never step onto the same tile twice. What is the longest hike you can take?

+

In the example above, the longest hike you can take is marked with O, and your starting position is marked S:

+
#S#####################
+#OOOOOOO#########...###
+#######O#########.#.###
+###OOOOO#OOO>.###.#.###
+###O#####O#O#.###.#.###
+###OOOOO#O#O#.....#...#
+###v###O#O#O#########.#
+###...#O#O#OOOOOOO#...#
+#####.#O#O#######O#.###
+#.....#O#O#OOOOOOO#...#
+#.#####O#O#O#########v#
+#.#...#OOO#OOO###OOOOO#
+#.#.#v#######O###O###O#
+#...#.>.#...>OOO#O###O#
+#####v#.#.###v#O#O###O#
+#.....#...#...#O#O#OOO#
+#.#########.###O#O#O###
+#...###...#...#OOO#O###
+###.###.#.###v#####O###
+#...#...#.#.>.>.#.>O###
+#.###.###.#.###.#.#O###
+#.....###...###...#OOO#
+#####################O#
+
+

This hike contains 94 steps. (The other possible hikes you could have taken were 90, 86, 82, 82, and 74 steps long.)

+

Find the longest hike you can take through the hiking trails listed on your map. How many steps long is the longest hike?

+
+

Your puzzle answer was 2086.

--- Part Two ---

As you reach the trailhead, you realize that the ground isn't as slippery as you expected; you'll have no problem climbing up the steep slopes.

+

Now, treat all slopes as if they were normal paths (.). You still want to make sure you have the most scenic hike possible, so continue to ensure that you never step onto the same tile twice. What is the longest hike you can take?

+

In the example above, this increases the longest hike to 154 steps:

+
#S#####################
+#OOOOOOO#########OOO###
+#######O#########O#O###
+###OOOOO#.>OOO###O#O###
+###O#####.#O#O###O#O###
+###O>...#.#O#OOOOO#OOO#
+###O###.#.#O#########O#
+###OOO#.#.#OOOOOOO#OOO#
+#####O#.#.#######O#O###
+#OOOOO#.#.#OOOOOOO#OOO#
+#O#####.#.#O#########O#
+#O#OOO#...#OOO###...>O#
+#O#O#O#######O###.###O#
+#OOO#O>.#...>O>.#.###O#
+#####O#.#.###O#.#.###O#
+#OOOOO#...#OOO#.#.#OOO#
+#O#########O###.#.#O###
+#OOO###OOO#OOO#...#O###
+###O###O#O###O#####O###
+#OOO#OOO#O#OOO>.#.>O###
+#O###O###O#O###.#.#O###
+#OOOOO###OOO###...#OOO#
+#####################O#
+
+

Find the longest hike you can take through the surprisingly dry hiking trails listed on your map. How many steps long is the longest hike?

+
+

Your puzzle answer was 6526.

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/day23.js b/src/2023/day23.js index 3720c082..18eeb201 100644 --- a/src/2023/day23.js +++ b/src/2023/day23.js @@ -3,47 +3,23 @@ function addTeleports(map) { for (let y = 0; y < map.length; y++) { for (let x = 0; x < map.length; x++) { if (map[y][x] === '#') continue; - const neighbors = [ - { x: x + 1, y }, - { x: x - 1, y }, - { x, y: y + 1 }, - { x, y: y - 1 }, - ].filter(({ x, y }) => map[y]?.[x] && map[y][x] !== '#'); + const neighbors = getNeighbors(map, { x, y }, []); if (neighbors.length === 2) continue; neighbors.forEach(n => { let prev = { x, y }; const trail = [prev]; while (n) { + const neighbors = getNeighbors(map, n, [`${prev.x},${prev.y}`]); trail.push(n); - const neighbors = [ - { x: n.x + 1, y: n.y }, - { x: n.x - 1, y: n.y }, - { x: n.x, y: n.y + 1 }, - { x: n.x, y: n.y - 1 }, - ].filter( - ({ x, y }) => - map[y]?.[x] && - map[y][x] !== '#' && - (prev.x !== x || prev.y !== y), - ); - if (neighbors.length === 1 && map[n.y][n.x] === '.') { - prev = n; - n = neighbors[0]; - } else { - n = null; - } + prev = n; + if (neighbors.length === 1) n = neighbors[0]; + else n = null; } - if (trail.length > 4) { - teleports[`${trail.at(1).x},${trail.at(1).y}`] = { - dest: trail.at(-1), - steps: trail.length - 2, - visited: trail.at(-2), - }; - teleports[`${trail.at(-2).x},${trail.at(-2).y}`] = { - dest: trail.at(0), - steps: trail.length - 2, - visited: trail.at(1), - }; + if (trail.length > 2) { + const key1 = `${trail.at(1).x},${trail.at(1).y}`; + const key2 = `${trail.at(-2).x},${trail.at(-2).y}`; + teleports[key1] = { dest: trail.at(-1), steps: trail.length - 2 }; + teleports[key2] = { dest: trail.at(0), steps: trail.length - 2 }; } }); } @@ -51,79 +27,55 @@ function addTeleports(map) { return teleports; } +function getNeighbors(map, { x, y }, visited) { + let neighbors = [ + { x: x + 1, y }, + { x: x - 1, y }, + { x, y: y + 1 }, + { x, y: y - 1 }, + ]; + if (map[y][x] === '>') neighbors = [neighbors[0]]; + if (map[y][x] === '<') neighbors = [neighbors[1]]; + if (map[y][x] === 'v') neighbors = [neighbors[2]]; + if (map[y][x] === '^') neighbors = [neighbors[3]]; + return neighbors.filter( + n => + map[n.y]?.[n.x] && + map[n.y][n.x] !== '#' && + !visited.includes(`${n.x},${n.y}`), + ); +} + +function solve(map, teleports, start, end) { + let best = 0; + const path = []; + const stack = [start]; + while (stack.length > 0) { + const next = stack.pop(); + while (path.length > next.len) path.pop(); + path.push(`${next.x},${next.y}`); + if (next.x === end.x && next.y === end.y) { + best = Math.max(best, next.steps); + } else if (teleports[`${next.x},${next.y}`]) { + const { dest, steps } = teleports[`${next.x},${next.y}`]; + if (!path.includes(`${dest.x},${dest.y}`)) { + stack.push({ ...dest, steps: next.steps + steps, len: path.length }); + } + } else { + getNeighbors(map, next, path).forEach(n => + stack.push({ ...n, steps: next.steps + 1, len: path.length }), + ); + } + } + return best; +} + export function part1(input) { const map = input.split('\n').map(line => line.split('')); const teleports = addTeleports(map); - let start = { x: 1, y: 0, steps: 0, visited: new Set() }; + let start = { x: 1, y: 0, steps: 0, len: 0 }; let end = { x: map.length - 2, y: map.length - 1 }; - let queue = [start]; - const result = new Set(); - while (queue.length > 0) { - const next = queue.shift(); - next.visited.add(`${next.x},${next.y}`); - if (next.x === end.x && next.y === end.y) { - if (Math.max(...result) < next.steps) console.log(next.steps); - result.add(next.steps); - continue; - } - let neighbors = [ - { - x: next.x + 1, - y: next.y, - steps: next.steps + 1, - visited: new Set(next.visited), - }, - { - x: next.x - 1, - y: next.y, - steps: next.steps + 1, - visited: new Set(next.visited), - }, - { - x: next.x, - y: next.y + 1, - steps: next.steps + 1, - visited: new Set(next.visited), - }, - { - x: next.x, - y: next.y - 1, - steps: next.steps + 1, - visited: new Set(next.visited), - }, - ]; - if (map[next.y][next.x] === '>') { - neighbors = neighbors.filter(n => n.x > next.x); - } else if (map[next.y][next.x] === '<') { - neighbors = neighbors.filter(n => n.x < next.x); - } else if (map[next.y][next.x] === '^') { - neighbors = neighbors.filter(n => n.y < next.y); - } else if (map[next.y][next.x] === 'v') { - neighbors = neighbors.filter(n => n.y > next.y); - } - neighbors = neighbors.filter( - n => - map[n.y]?.[n.x] && - map[n.y][n.x] !== '#' && - !next.visited.has(`${n.x},${n.y}`), - ); - neighbors = neighbors.map(n => { - const teleport = teleports[`${n.x},${n.y}`]; - if (teleport && !n.visited.has(`${teleport.dest.x},${teleport.dest.y}`)) { - n.visited.add(`${n.x},${n.y}`); - n.visited.add(`${teleport.visited.x},${teleport.visited.y}`); - return { - ...teleport.dest, - steps: n.steps + teleport.steps, - visited: n.visited, - }; - } - return n; - }); - queue.push(...neighbors); - queue.sort((a, b) => a.x + a.y - b.x - b.y + b.steps - a.steps); - } - return Math.max(...result); + return solve(map, teleports, start, end); } export function part2(input) { diff --git a/src/2023/day23.spec.js b/src/2023/day23.spec.js index aeeb15a6..2377897d 100644 --- a/src/2023/day23.spec.js +++ b/src/2023/day23.spec.js @@ -75,9 +75,8 @@ describe('day23 2023', () => { ).toEqual(154); }); - // need to optimize - // it('should work for part 2 input', () => { - // expect(part2(input)).toEqual(6526); - // }); + it('should work for part 2 input', () => { + expect(part2(input)).toEqual(6526); + }); }); }); diff --git a/src/2023/events.html b/src/2023/events.html index cfe21416..a680ffa7 100644 --- a/src/2023/events.html +++ b/src/2023/events.html @@ -22,7 +22,7 @@

Advent of Code

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

   $year=   $year=<
+

Total stars: 446*

diff --git a/src/2023/index.html b/src/2023/index.html index e62ebaee..d463fbc7 100644 --- a/src/2023/index.html +++ b/src/2023/index.html @@ -22,7 +22,7 @@

Advent of Code

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

   $year=   $year=<
                     ...'''''''''...                    
                   .'' ~/\* ~~~~  /\ ''.            14 **
                 .' /\/\ ~~~~~~~~ * /~\ '.          15 **
            ...  :~~~~~~~~~ /\ ~~~~/ * \ :          16 **
-    .''....' '..'.||||||||||||||||||||~~~~/\*/\ ~  /\   /\ .'          13 **
+    .''....' '..'.||||||||||||||||||||~~~~/\*/\ ~  /\   /\ .'          13 **
     '.ZZ~   ~ * ~~~~.   /\  /\   /\ ..'            17 **
 .'''' ZZ* .'''.[]~~~''''.........'''               12 **
 '.... ~   '...'  *[]....'                          18 **
     .'*  ~   [^^^] '.                              11 **
-    '..''''.*.''''.,.`.'""..".'' ''...                       10 **
+    '..''''.*.''''.'``',`.,``.'' ''...                       10 **
           .'''~ ~ ~ . * ### ''.                    19 **
         .' ~  ,* ~ '", ~ ##### '.                   9 **
         : ~ '(~), ~ * ~ ~ ~ ### :                   8 **
         '. ~ " ' ~ ~ ~ * ~~~## .'                  20 **
-          '.. ~ ~ * ~ ####~~....................~~'.'''''''''...        7 **
+          '.. ~ ~ * ~ ####~~....................~~'.'''''''''...        7 **
              '''.........'''' ~ .'*. ~  ..  ''.     6 **
                         .' ~ .. '...' ~'* '.~  '.  21 **
                         ~~ .~~~'. ~     '. *'.~ :   5 **
-                 ...''''||||||||||||||||||||~~~'*~~.'  .''.~  '..' .'  22 **
-              .''         '..  ~..'*   '. ~ ..'     4 **
-
-
-
               '..    -     -   *..'                 3 **
     ----@        '''..*......'''                    2 **
   * ! /^\                                           1 **
diff --git a/src/2023/leaderboard.html b/src/2023/leaderboard.html
index 06cda8a9..857219a0 100644
--- a/src/2023/leaderboard.html
+++ b/src/2023/leaderboard.html
@@ -22,7 +22,7 @@ 

Advent of Code

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

   $year=   $year=<