Skip to content

Instantly share code, notes, and snippets.

@winny-

winny-/day3.rkt Secret

Created December 3, 2017 07:07
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-/a8adbe19f0cba0f11e029d3d5a97adeb to your computer and use it in GitHub Desktop.
Save winny-/a8adbe19f0cba0f11e029d3d5a97adeb to your computer and use it in GitHub Desktop.
#lang racket
(module+ test
(require rackunit))
(define (get-ring n)
(define m (ceiling (sqrt n)))
(if (odd? m)
m
(add1 m)))
(module+ test
(test-case "get-ring"
(check-= (get-ring 1) 1 0)
(check-= (get-ring 4) 3 0)
(check-= (get-ring 12) 5 0)
(check-= (get-ring 23) 5 0)
(check-= (get-ring 1024) 33 0)))
(define (get-closest-orthagonal n)
(define ring (get-ring n))
(if (= 1 ring)
1
(argmin (λ (x) (abs (- x n))) (range (+ (quotient ring 2) (sqr (- ring 2)))
(add1 (sqr ring))
(sub1 ring)))))
(define (part1 target)
(+ (abs (- target (get-closest-orthagonal target)))
(quotient (get-ring target) 2)))
(module+ test
(test-case "part1"
(check-= (part1 1) 0 0)
(check-= (part1 12) 3 0)
(check-= (part1 23) 2 0)
(check-= (part1 1024) 31 0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment