You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
aoc-racket/2016/day19/lang.rkt

34 lines
1.0 KiB
Racket

8 years ago
#lang br/quicklang ;; http://adventofcode.com/2016/day/19
8 years ago
(provide read-syntax
(rename-out [mb #%module-begin]))
(define (read-syntax path port)
(strip-bindings
#`(module mod "lang.rkt"
#,(string-trim (port->string port)))))
(define-macro (mb NUM-STR)
#'(#%module-begin
8 years ago
#;(displayln (solve (string->number NUM-STR) #f))
8 years ago
(displayln (solve (string->number NUM-STR) #t))))
(define (idx-after vec x)
(or
(for/first ([idx (in-range (modulo (add1 x) (vector-length vec)) (vector-length vec))]
#:when (vector-ref vec idx))
idx)
8 years ago
(idx-after vec -1)))
8 years ago
(define (solve num [circle? #f])
(define elves (make-vector num #t))
(let loop ([taker 0][elves-left num])
(cond
[(= elves-left 2) (add1 taker)]
[else
(define giver (for/fold ([elf taker])
8 years ago
([i (in-range (if circle? (floor (/ elves-left 2)) 1))])
8 years ago
(idx-after elves elf)))
(vector-set! elves giver #f)
(loop (idx-after elves taker) (sub1 elves-left))])))