47 lines
1.1 KiB
Racket
47 lines
1.1 KiB
Racket
|
#lang racket
|
||
|
|
||
|
(define (split-lines s)
|
||
|
(string-split (string-trim s) "\n"))
|
||
|
|
||
|
(define (parse-orbit s)
|
||
|
(string-split s ")"))
|
||
|
|
||
|
(define COM "COM")
|
||
|
|
||
|
(define input (map parse-orbit (split-lines (file->string "input.6.txt"))))
|
||
|
|
||
|
(define (create-uom input)
|
||
|
(make-hash
|
||
|
(for/list ([grp (group-by first input)])
|
||
|
(cons (first (first grp)) (map second grp)))))
|
||
|
|
||
|
(define uom (create-uom input))
|
||
|
|
||
|
(define (walk-count uom key [depth 0])
|
||
|
(let ([sats (hash-ref uom key '())])
|
||
|
(+ depth
|
||
|
(for/sum ([sat sats])
|
||
|
(walk-count uom sat (add1 depth))))))
|
||
|
|
||
|
;; Part 1
|
||
|
(walk-count uom COM)
|
||
|
|
||
|
(define (get-path uom search [curr COM] [acc '()])
|
||
|
(let ([sats (hash-ref uom curr '())]
|
||
|
[new-curr (append acc (list curr))])
|
||
|
(if (member search sats)
|
||
|
new-curr
|
||
|
(ormap (lambda (sat) (get-path uom search sat new-curr))
|
||
|
sats))))
|
||
|
|
||
|
(define path-YOU (get-path uom "YOU"))
|
||
|
(define path-SAN (get-path uom "SAN"))
|
||
|
|
||
|
(define (get-transfers you san)
|
||
|
(cond
|
||
|
[(equal? (first you) (first san))
|
||
|
(get-transfers (rest you) (rest san))]
|
||
|
[else (+ (length you) (length san))]))
|
||
|
|
||
|
;; Part 2
|
||
|
(get-transfers path-YOU path-SAN)
|