Skip to content

Instantly share code, notes, and snippets.

@winny-

winny-/day5.rkt Secret

Created December 5, 2017 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winny-/f106edc0b73c69e13168569e8f31f2b7 to your computer and use it in GitHub Desktop.
Save winny-/f106edc0b73c69e13168569e8f31f2b7 to your computer and use it in GitHub Desktop.
#lang racket
(module+ test
(require rackunit))
(define (part1-step ls index)
(values (list-update ls index add1)
(+ index (list-ref ls index))))
(module+ test
(test-case "part1-step"
(define tests
'((((0 3 0 1 -3) 0)
((1 3 0 1 -3) 0))
(((2 3 0 1 -3) 1)
((2 4 0 1 -3) 4))
(((2 4 0 1 -3) 4)
((2 4 0 1 -2) 1))
(((2 4 0 1 -2) 1)
((2 5 0 1 -2) 5))))
(for ([t tests])
(check-equal? (call-with-values (thunk (apply part1-step (car t))) list) (cadr t) (~a t)))))
(define (part1 ls)
(let loop ([ls ls] [index 0] [n 0])
(if (not (< index (length ls)))
n
(let-values ([(nls nindex) (part1-step ls index)])
(loop nls nindex (add1 n))))))
(module+ test
(test-case "part1"
(check-equal? (part1 '(0 3 0 1 -3)) 5)))
(module+ main
(define ls (map string->number (port->list read-line)))
(displayln (format "Part 1: ~a" (part1 ls))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment