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/2017/d10/main.rkt

43 lines
1.8 KiB
Racket

7 years ago
#lang br/quicklang
7 years ago
(require "../helper.rkt" racket/sequence)
7 years ago
(provide read-syntax (rename-out [#%mb #%module-begin]))
(define (read-syntax path port)
(define lines (port->lines port))
(strip-context #`(module mod "main.rkt"
#,@(for/list ([datum (in-port read (open-input-string (car lines)))])
7 years ago
datum)
7 years ago
#,@(cdr lines))))
7 years ago
(define-macro (#%mb STARS RANGE-IN STR)
7 years ago
#`(#%module-begin
7 years ago
((if (eq? 'STARS ') one-star two-star) RANGE-IN STR)))
7 years ago
7 years ago
(define (one-star range-in str)
(define lens (with-input-from-string (string-replace str "," " ")
(λ () (for/list ([len (in-port)])
len))))
(define nums (reverse-segments range-in lens))
(* (first nums) (second nums)))
7 years ago
7 years ago
(define (two-star range-in str)
7 years ago
(define ascii-chars (map char->integer (string->list str)))
7 years ago
(define nums (reverse-segments range-in (append ascii-chars '(17 31 73 47 23)) #:reps 64))
(define dense-hash (for/list ([vals (in-slice 16 nums)])
(apply bitwise-xor vals)))
7 years ago
(string-append* (for/list ([num (in-list dense-hash)])
7 years ago
(~r num #:base 16 #:min-width 2 #:pad-string "0"))))
7 years ago
7 years ago
(define (reverse-segments range-in lens #:reps [reps 1])
(define vec (list->vector (range range-in)))
7 years ago
(for*/fold ([current-position 0]
[skip-size 0])
7 years ago
([rep (in-range reps)]
7 years ago
[len (in-list lens)])
(define posns (for/list ([i (in-range len)])
7 years ago
(modulo (+ current-position i) range-in)))
7 years ago
(for ([val (in-list (map (curry vector-ref vec) posns))]
[posn (in-list (reverse posns))])
7 years ago
(vector-set! vec posn val))
(values (+ current-position len skip-size) (add1 skip-size)))
(vector->list vec))