Skip to content

Instantly share code, notes, and snippets.

@winny- winny-/day3.rkt Secret
Created Dec 3, 2017

Embed
What would you like to do?
#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
You can’t perform that action at this time.