Day s i x

This commit is contained in:
xenia 2019-12-06 00:23:54 -05:00
parent e088178b3a
commit 8b043023bb
1 changed files with 47 additions and 0 deletions

47
6.rkt Normal file
View File

@ -0,0 +1,47 @@
#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)