alexandria-ocasio-cortez axiom-of-choice area-of-concern american-orthodox-church almost-optimal-coset solutions in the year of our lord 2021, I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0 KiB

#lang racket
(require "lib/common.rkt")
(struct posn (x y) #:transparent)
(struct fold (axis n) #:transparent)
(define (parse input)
(match-define `(,paper ,instrs) (string-split input "\n\n"))
(values (for/set ([p (in-list (string-split paper))])
(match-define (pregexp #px"([0-9]+),([0-9]+)"
(list _
(app string->number x)
(app string->number y)))
(posn x y))
(for/list ([instr (in-list (string-split instrs "\n"))])
(match-define (pregexp #px"fold along (x|y)=([0-9]+)"
(list _
(app string->symbol axis)
(app string->number n)))
(fold axis n))))
(define (fold-along paper instr)
(for/set ([p (in-set paper)])
(match-define (posn x y) p)
(match instr
[(fold 'x n) #:when (> x n) (posn (- (* 2 n) x) y)]
[(fold 'y n) #:when (> y n) (posn x (- (* 2 n) y))]
[_ p])))
(define (day13a paper instrs)
(set-count (fold-along paper (first instrs))))
(define (max-on-axis paper axis)
(for/max ([pt (in-set paper)])
(match axis
['x (posn-x pt)]
['y (posn-y pt)])))
(define (print-paper paper)
(for ([y (in-inclusive-range 0 (max-on-axis paper 'y))])
(apply string
(for/list ([x (in-inclusive-range 0 (max-on-axis paper 'x))])
(cond [(set-member? paper (posn x y)) #\█]
[else #\space]))))))
(define (day13b paper instrs)
(for/fold ([cur-paper paper])
([instr (in-list instrs)])
(fold-along cur-paper instr))))
(module+ main
(call-with-input-file "data/day13.txt"
(λ (prt)
(define-values (paper instrs) (parse (port->string prt)))
(answer 13 1 (day13a paper instrs))
(displayln "answer 13.2:")
(day13b paper instrs))))