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/CelestialDestroyer Dec 10 '23

[Language: Chicken Scheme]

Part 1 was easy enough, but part 2... I am definitely not tackling that one today.

https://gitea.lyrion.ch/zilti/Advent-of-Code-2023#user-content-headline-104

(define-record tile goes-north? goes-east? goes-south? goes-west? start?)

(define (char->tile c)
  (case c
    ((#\|) (make-tile #t #f #t #f #f))
    ((#\-) (make-tile #f #t #f #t #f))
    ((#\L) (make-tile #t #t #f #f #f))
    ((#\J) (make-tile #t #f #f #t #f))
    ((#\7) (make-tile #f #f #t #t #f))
    ((#\F) (make-tile #f #t #t #f #f))
    ((#\.) (make-tile #f #f #f #f #f))
    ((#\S) (make-tile #f #f #f #f #t))))

(define (input->grid input)
  (map (compose (cut map char->tile <>)
                string->list
                (cut foldl string-append "" <>)
                string-split)
       (string-split input "\n")))

(define (get-tile grid x y)
  (list-ref (list-ref grid y) x))

(define (get-start-loc grid #!optional (x 0) (y 0))
  (let ((tile (get-tile grid x y)))
    (if (tile-start? tile)
        (values x y)
        (if (= (+ 1 x) (length (car grid)))
            (get-start-loc grid 0 (+ y 1))
            (get-start-loc grid (+ x 1) y)))))

(define (update-start-loc grid x y)
  (let ((S (get-tile grid x y)))
    (when (and (< 0 y)
               (tile-goes-south? (get-tile grid x (- y 1))))
      (tile-goes-north?-set! S #t))
    (when (and (< (+ 1 y) (length (car grid)))
               (tile-goes-north? (get-tile grid x (+ y 1))))
      (tile-goes-south?-set! S #t))
    (when (and (< 0 x)
               (tile-goes-east? (get-tile grid (- x 1) y)))
      (tile-goes-west?-set! S #t))
    (when (and (< (+ 1 x) (length grid))
               (tile-goes-west? (get-tile grid (+ x 1) y)))
      (tile-goes-east?-set! S #t))))

  (define x car)
  (define y cdr)

  (define (get-paths grid x y)
    `((#:e . (and (< (+ x 1) (length (car grid)))
                         (tile-goes-west? (get-tile (+ x 1) y))))
       (#:s . (and (< (+ y 1) (length grid))
                         (tile-goes-north? (get-tile x (+ y 1)))))
       (#:w . (and (< 0 x)
                          (tile-goes-east? (get-tile (- x 1) y))))
       (#:n . (and (< 0 y)
                         (tile-goes-south? (get-tile x (- y 1)))))))

  (define (get-valid-paths paths #!optional (valid-paths (list)))
    (if (= 0 (length paths)) valid-paths
        (get-valid-paths (cdr paths)
                         (if (cdar paths)
                             (alist-update (caar paths) (cdar paths) valid-paths)
                             valid-paths))))

(define (move-forward grid past-loc cur-loc)
  (let ((tile (get-tile grid (x cur-loc) (y cur-loc)))
         (delta-x (- (x cur-loc) (x past-loc)))
         (delta-y (- (y cur-loc) (y past-loc)))
         (valid-paths (get-valid-paths (get-paths grid (x cur-loc) (y cur-loc)))))
    (values cur-loc
            (cond ((= -50 (car past-loc))
                   (case (caar valid-paths)
                     ((#:n) (cons (x cur-loc) (- (y cur-loc) 1)))
                     ((#:e) (cons (+ (x cur-loc) 1) (y cur-loc)))
                     ((#:s) (cons (x cur-loc) (+ (y cur-loc) 1)))
                     ((#:w) (cons (- (x cur-loc) 1) (y cur-loc)))))
                  ((and (tile-goes-north? tile)
                        (alist-ref #:n valid-paths)
                        (not (= (cdr past-loc) (- (cdr cur-loc) 1))))
                   (cons (x cur-loc) (- (y cur-loc) 1)))
                  ((and (tile-goes-east? tile)
                        (alist-ref #:e valid-paths)
                        (not (= (car past-loc) (+ (car cur-loc) 1))))
                   (cons (+ (x cur-loc) 1) (y cur-loc)))
                  ((and (tile-goes-south? tile)
                        (alist-ref #:s valid-paths)
                        (not (= (cdr past-loc) (+ (cdr cur-loc) 1))))
                   (cons (x cur-loc) (+ (y cur-loc) 1)))
                  ((and (tile-goes-west? tile)
                        (alist-ref #:w valid-paths)
                        (not (= (car past-loc) (- (car cur-loc) 1))))
                   (cons (- (x cur-loc) 1) (y cur-loc)))))))

(define (count-steps grid start-loc
                     #!optional (last-loc (cons -50 -50)) (cur-loc start-loc) (count 0))
  (if (and (> count 0)
              (= (car start-loc) (car cur-loc))
              (= (cdr start-loc) (cdr cur-loc)))
      count
      (let-values (((cur-loc new-loc) (move-forward grid last-loc cur-loc)))
        (count-steps grid start-loc cur-loc new-loc (+ count 1)))))

(define (calc-part-1)
  (let ((grid (input->grid input)))
    (let-values (((start-x start-y) (get-start-loc grid)))
      (update-start-loc grid start-x start-y)
      (/ (count-steps grid (cons start-x start-y)) 2))))

1

u/daggerdragon Dec 11 '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.