From 0e5d9eca75997bc32d4edcdd2e9693ed31d1416f Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 2 Dec 2019 06:03:52 -0800 Subject: [PATCH] 2019 d01 & d02 --- 2019/01.rkt | 20 +++++++++++ 2019/01.rktd | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2019/02.rkt | 36 +++++++++++++++++++ 2019/02.rktd | 1 + 4 files changed, 157 insertions(+) create mode 100644 2019/01.rkt create mode 100644 2019/01.rktd create mode 100644 2019/02.rkt create mode 100644 2019/02.rktd diff --git a/2019/01.rkt b/2019/01.rkt new file mode 100644 index 0000000..b776246 --- /dev/null +++ b/2019/01.rkt @@ -0,0 +1,20 @@ +#lang br +(require racket/file rackunit) + +(define (fuel-required mass) (- (floor (/ mass 3)) 2)) + +(define masses + (for/list ([ln (in-port read (open-input-file "01.rktd"))]) + ln)) + +;; 1 +(check-eq? (apply + (map fuel-required masses)) 3167282) + +(define (recursive-fuel mass [acc 0]) + (match (fuel-required mass) + [(? positive? fuel) + (recursive-fuel fuel (+ acc fuel))] + [else acc])) + +;; 2 +(check-eq? (apply + (map recursive-fuel masses)) 4748063) \ No newline at end of file diff --git a/2019/01.rktd b/2019/01.rktd new file mode 100644 index 0000000..6e9d1fc --- /dev/null +++ b/2019/01.rktd @@ -0,0 +1,100 @@ +143754 +83242 +124730 +62796 +128187 +68925 +60687 +68800 +112450 +70696 +94653 +62124 +82251 +91514 +79895 +82973 +71678 +141671 +88243 +109553 +135097 +78026 +100048 +52113 +109934 +92274 +62821 +138384 +90112 +114684 +137383 +71727 +143236 +79842 +101187 +71202 +131156 +128805 +105102 +71319 +88615 +62024 +126027 +55321 +91226 +75020 +136689 +70265 +97850 +96536 +135311 +64962 +87137 +50402 +70604 +56879 +60016 +98231 +136635 +64590 +143522 +112152 +142511 +95350 +83483 +123681 +123792 +99044 +139282 +96610 +116844 +50416 +110682 +55137 +69795 +100411 +110119 +141558 +90780 +108063 +102247 +85487 +107174 +79009 +131908 +95164 +120588 +62031 +51070 +63773 +128565 +96458 +91388 +54345 +52840 +130519 +51357 +146851 +68455 +102463 \ No newline at end of file diff --git a/2019/02.rkt b/2019/02.rkt new file mode 100644 index 0000000..a0a0dc6 --- /dev/null +++ b/2019/02.rkt @@ -0,0 +1,36 @@ +#lang br +(require racket/file rackunit) + +(define (string->regs str) + (list->vector (map string->number (string-split str ",")))) + +(define (solve regs) + (define (deref ptr) (vector-ref regs ptr)) + (let loop ([ptr 0]) + (match (vector-ref regs ptr) + [(and (or 1 2) opcode) + (vector-set! regs (deref (+ ptr 3)) + ((match opcode [1 +][_ *]) + (deref (deref (+ ptr 1))) + (deref (deref (+ ptr 2))))) + (loop (+ ptr 4))] + [99 regs]))) + +(define test "1,1,1,4,99,5,6,0,99") +(check-equal? (solve (string->regs test)) '#(30 1 1 4 2 5 6 0 99)) + +(define (nv-result noun verb) + (define regs (string->regs (file->string "02.rktd"))) + (vector-set*! regs 1 noun 2 verb) + (vector-ref (solve regs) 0)) + +;; 1 +(check-eq? (nv-result 12 2) 6730673) + +;; 2 +(check-eq? + (for*/first ([noun (in-range 91)] + [verb (in-range 91)] + #:when (eq? (nv-result noun verb) 19690720)) + (+ (* 100 noun) verb)) + 3749) \ No newline at end of file diff --git a/2019/02.rktd b/2019/02.rktd new file mode 100644 index 0000000..af1c639 --- /dev/null +++ b/2019/02.rktd @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,9,1,19,1,19,6,23,2,6,23,27,2,27,9,31,1,5,31,35,1,35,10,39,2,39,9,43,1,5,43,47,2,47,10,51,1,51,6,55,1,5,55,59,2,6,59,63,2,63,6,67,1,5,67,71,1,71,9,75,2,75,10,79,1,79,5,83,1,10,83,87,1,5,87,91,2,13,91,95,1,95,10,99,2,99,13,103,1,103,5,107,1,107,13,111,2,111,9,115,1,6,115,119,2,119,6,123,1,123,6,127,1,127,9,131,1,6,131,135,1,135,2,139,1,139,10,0,99,2,0,14,0 \ No newline at end of file