aoc2023/11.rkt

58 lines
1.4 KiB
Racket

#lang curly-fn racket
(require "scripts/aoc.rkt")
;; solution for day 11
(define (expand universe [amount 1])
(define width
(add1 (apply max (set-map universe car))))
(define height
(add1 (apply max (set-map universe cdr))))
(define (has-y? y)
(for/or ([pt (in-set universe)])
(= (cdr pt) y)))
(define (has-x? x)
(for/or ([pt (in-set universe)])
(= (car pt) x)))
(define empty-rows
(for/list ([y (in-range height)]
#:unless (has-y? y))
y))
(define empty-cols
(for/list ([x (in-range width)]
#:unless (has-x? x))
x))
(for/set ([pt (in-set universe)])
(define x-offset (count #{< % (car pt)} empty-cols))
(define y-offset (count #{< % (cdr pt)} empty-rows))
(cons (+ (car pt) (* amount x-offset)) (+ (cdr pt) (* amount y-offset)))))
(define (part1 input [amount 1])
(define expanded (expand input amount))
(/ (for*/sum ([a (in-set expanded)] [b (in-set expanded)])
(+ (abs (- (car a) (car b)))
(abs (- (cdr a) (cdr b)))))
2))
(define (part2 input)
(part1 input (sub1 1000000)))
(define (parse fname)
(for/set ([line (in-list (file->lines fname))] [y (in-naturals)]
#:do []
[char (in-string line)] [x (in-naturals)]
#:when (eq? char #\#))
(cons x y)))
(module+ main
(define input (parse "inputs/11"))
(answer 11 1 (time (part1 input)))
(answer 11 2 (time (part2 input)))
(displayln "meow"))