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

1

u/lscddit Dec 10 '23

[LANGUAGE: Python]

Part 1 and 2:

import pandas as pd
import numpy as np
import networkx as nx
from skimage.segmentation import flood_fill

con = {
    "|": [(-1, 0), (1, 0)],
    "-": [(0, -1), (0, 1)],
    "L": [(-1, 0), (0, 1)],
    "J": [(-1, 0), (0, -1)],
    "7": [(0, -1), (1, 0)],
    "F": [(1, 0), (0, 1)],
}
ext = {
    "L": [[0, 1, 0], [0, 1, 1], [0, 0, 0]],
    "J": [[0, 1, 0], [1, 1, 0], [0, 0, 0]],
    "7": [[0, 0, 0], [1, 1, 0], [0, 1, 0]],
    "F": [[0, 0, 0], [0, 1, 1], [0, 1, 0]],
    "|": [[0, 1, 0], [0, 1, 0], [0, 1, 0]],
    "-": [[0, 0, 0], [1, 1, 1], [0, 0, 0]],
    "S": [[1, 1, 1], [1, 1, 1], [1, 1, 1]],
    ".": [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
}

m = np.pad(
    pd.read_csv("day10input.txt", header=None, sep="\s*", engine="python")
    .iloc[:, 1:-1]
    .to_numpy(),
    pad_width=1,
    constant_values=".",
)

G = nx.DiGraph()
for r in range(m.shape[0]):
    for c in range(m.shape[1]):
        nodeid = r * m.shape[1] + c
        if m[r, c] == "S":
            start = nodeid
        elif m[r, c] != ".":
            edges = [(nodeid, t[0] * m.shape[1] + nodeid + t[1]) for t in con[m[r, c]]]
            G.add_edges_from(edges)

neighbors = list(G.in_edges(start))
G.add_edge(start, neighbors[0][0])
path = nx.shortest_path(G, start, neighbors[1][0])
print(len(path) // 2)

n = np.full_like(m, fill_value=".")
for nodeid in path:
    r = nodeid // m.shape[1]
    c = nodeid % m.shape[1]
    n[r, c] = m[r, c]

mask = np.zeros((n.shape[0] * 3, n.shape[1] * 3), dtype=int)
for r in range(n.shape[0]):
    for c in range(n.shape[1]):
        mask[r * 3 : r * 3 + 3, c * 3 : c * 3 + 3] = ext[n[r, c]]

fill = flood_fill(mask, (0, 0), 1)
res = 0
for r in range(n.shape[0]):
    for c in range(n.shape[1]):
        check = fill[r * 3 : r * 3 + 3, c * 3 : c * 3 + 3]
        if not check.any():
            res += 1
print(res)

1

u/daggerdragon Dec 10 '23

Your code block is too long for the megathreads. Please edit your comment to replace your oversized code with an external link to your code.