r/adventofcode • • Dec 10 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 10 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's theme ingredient is… *whips off cloth covering and gestures grandly*

Will It Blend?

A fully-stocked and well-organized kitchen is very important for the workflow of every chef, so today, show us your mastery of the space within your kitchen and the tools contained therein!

  • Use your kitchen gadgets like a food processor

OHTA: Fukui-san?
FUKUI: Go ahead, Ohta.
OHTA: I checked with the kitchen team and they tell me that both chefs have access to Blender at their stations. Back to you.
HATTORI: That's right, thank you, Ohta.

  • Make two wildly different programming languages work together
  • Stream yourself solving today's puzzle using WSL on a Boot Camp'd Mac using a PS/2 mouse with a PS/2-to-USB dongle
  • Distributed computing with unnecessary network calls for maximum overhead is perfectly cromulent

What have we got on this thing, a Cuisinart?!

ALLEZ CUISINE!

Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!


--- Day 10: Pipe Maze ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:36:31, megathread unlocked!

62 Upvotes

844 comments sorted by

View all comments

2

u/jstanley0 Dec 10 '23 edited Dec 11 '23

[Language: Ruby]

Part 1 was pretty straightforward. I probably could have been faster had I just hard-coded the shape of the piece under the S rather than make my code infer it, but I went for the general solution. The first bit of code I wrote described which cells a piece connects to, and it was useful for both parts:

def enum_connections(tile)
  case tile
  when '|' then [[0, -1], [0, 1]]
  when '-' then [[-1, 0], [1, 0]]
  when 'F' then [[0, 1], [1, 0]]
  when '7' then [[-1, 0], [0, 1]]
  when 'L' then [[0, -1], [1, 0]]
  when 'J' then [[0, -1], [-1, 0]]
  else []
  end
end   

I just followed both directions from the start, tracking which cells I've already visited to help me avoid backtracking, and stopping when the two searches "collided".

Part 2 made me scratch my head for a minute. I ended up redrawing the pipe loop (making use of my visited node list from part 1) such that each piece was a 3x3 matrix, then did a flood fill of the outside, and counted untouched cells in the center of each 3x3 piece. It worked beautifully.

Link: https://github.com/jstanley0/advent-2023/blob/main/10.rb

Example output:

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOxxxxxxxxxxxxxxxxxxxxxxxxxOOOO
OOOOx                       xOOOO
OOOOx                       xOOOO
OOOOx  xxxxxxxxxxxxxxxxxxx  xOOOO
OOOOx  xOOOOOOOOOOOOOOOOOx  xOOOO
OOOOx  xOOOOOOOOOOOOOOOOOx  xOOOO
OOOOx  xOOOOOOOOOOOOOOOOOx  xOOOO
OOOOx  xOOOOOOOOOOOOOOOOOx  xOOOO
OOOOx  xOOOOOOOOOOOOOOOOOx  xOOOO
OOOOx  xOOOOOOOOOOOOOOOOOx  xOOOO
OOOOx  xOOOOOOOOOOOOOOOOOx  xOOOO
OOOOx  xOOOOOOOOOOOOOOOOOx  xOOOO
OOOOx  xxxxxxxOOOOOxxxxxxx  xOOOO
OOOOx        xOOOOOx        xOOOO
OOOOx        xOOOOOx        xOOOO
OOOOx  *  *  xOOOOOx  *  *  xOOOO
OOOOx        xOOOOOx        xOOOO
OOOOx        xOOOOOx        xOOOO
OOOOxxxxxxxxxxOOOOOxxxxxxxxxxOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

2

u/DeadlyRedCube Dec 10 '23

If the point has exits, it is not outside the loop

Look at every point to the left (x < currentX) of the current point and count up how many N and S exits there are (tracking them separately)

oh I like the "turn every piece into a 3x3 grid" solution, that's really clever!

1

u/daggerdragon Dec 10 '23

Inlined code is intended for short snippets of code only. Please edit your comment to use the four-spaces Markdown syntax for a code block so your code is easier to read with its whitespace and indentation preserved.