From 0f5e14363bb223fd34699685e74953c2afa8b3ff Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Mon, 14 Dec 2020 21:46:39 -0800 Subject: [PATCH] Day 15. --- .gitignore | 1 + src/15.rkt | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/15.rkt diff --git a/.gitignore b/.gitignore index 072f362..f8acb17 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ **/*.bak **/*.rkt~ **/*.txt~ +**/*.exe **/compiled/ diff --git a/src/15.rkt b/src/15.rkt new file mode 100644 index 0000000..d283602 --- /dev/null +++ b/src/15.rkt @@ -0,0 +1,21 @@ +#lang curly-fn racket + +(require "../lib.rkt") + +(define input '(5 1 9 18 13 8 0)) + +(define (play end) + (define turns + (make-hash (map cons input (range 1 (add1 (length input)))))) + (let loop ([turn (length input)] [curr (last input)]) + (cond + [(>= turn end) curr] + [(hash-has-key? turns curr) + (let ([next (- turn (hash-ref turns curr))]) + (hash-set! turns curr turn) + (loop (add1 turn) next))] + [else + (hash-set! turns curr turn) + (loop (add1 turn) 0)]))) + +(show-solution (play 2020) (play 30000000))