From 214186f3f8439fb7fe1a93cbcb4c874e242d1646 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 18 Dec 2018 23:09:00 -0800 Subject: [PATCH] d09 p1 (slow) --- 2018/09-test.txt | 1 + 2018/09.rkt | 30 ++++++++++++++++++++++++++++++ 2018/09.txt | 1 + 3 files changed, 32 insertions(+) create mode 100644 2018/09-test.txt create mode 100644 2018/09.rkt create mode 100644 2018/09.txt diff --git a/2018/09-test.txt b/2018/09-test.txt new file mode 100644 index 0000000..f94a71a --- /dev/null +++ b/2018/09-test.txt @@ -0,0 +1 @@ +10 players; last marble is worth 1618 points \ No newline at end of file diff --git a/2018/09.rkt b/2018/09.rkt new file mode 100644 index 0000000..5aeb6e7 --- /dev/null +++ b/2018/09.rkt @@ -0,0 +1,30 @@ +#lang debug br +(require racket/file) + +(define (★) + (match-define (list player-count marble-count) + (map string->number (regexp-match* #px"\\d+" (file->string "09.txt")))) + (define scores (make-hasheqv)) + (let loop ([marble 1] [pos 0] [circle (list 0)]) + (cond + [(> marble marble-count) (argmax cdr (hash->list scores))] + [(zero? (modulo marble 23)) + (define len (length circle)) + (define deletion-pos (modulo (+ (- pos 7) len) len)) + (define-values (head tail) (split-at circle deletion-pos)) + (define player (modulo marble player-count)) + (hash-update! scores player (λ (sc) (+ marble (car tail) sc)) 0) + (loop (add1 marble) deletion-pos (append head (cdr tail)))] + [else + (define next-pos (add1 (modulo (add1 pos) (length circle)))) + (define-values (head tail) (split-at circle next-pos)) + (loop (add1 marble) next-pos (append head (list marble) tail))]))) + +#;(define (★★) + ) +#;(★★) + +#;(module+ test + (require rackunit) + (check-equal? (time (★)) 437654) + (check-equal? (time (★★)) 566)) \ No newline at end of file diff --git a/2018/09.txt b/2018/09.txt new file mode 100644 index 0000000..18f5327 --- /dev/null +++ b/2018/09.txt @@ -0,0 +1 @@ +400 players; last marble is worth 71864 points \ No newline at end of file