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/2018/03.rkt

40 lines
1.2 KiB
Racket

6 years ago
#lang debug br
(define fp (open-input-file "03.txt"))
(struct pt (x y) #:transparent)
(struct rect (ul lr) #:transparent)
(define (parse-claim ln)
(match-define (list left top width height)
(map string->number (cdr (regexp-match #px"(\\d+),(\\d+): (\\d+)x(\\d+)" ln))))
(rect (pt left top) (pt (+ left width) (+ top height))))
(define claims
(map parse-claim (port->lines fp)))
(define coverage (make-hash))
(for ([(claim idx) (in-indexed claims)])
(define cidx (add1 idx))
(match-define (rect (pt left top) (pt right bottom)) claim)
(for* ([x (in-range left right)]
[y (in-range top bottom)])
(hash-update! coverage (pt x y) (λ (cidxs) (cons cidx cidxs)) empty)))
(define ()
(for/sum ([cidxs (in-list (hash-values coverage))]
#:when (<= 2 (length cidxs)))
1))
(define (★★)
(define cidxss (hash-values coverage))
(for/first ([cidx (in-range 1 (add1 (length claims)))]
#:when (for/and ([cidxs (in-list cidxss)]
#:when (memv cidx cidxs))
(= (length cidxs) 1)))
cidx))
6 years ago
(module+ test
6 years ago
(require rackunit)
(check-equal? (time ()) 110827)
(check-equal? (time (★★)) 116))