-
-
Save winny-/a8adbe19f0cba0f11e029d3d5a97adeb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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