day 4: make it fancey

This commit is contained in:
xenia 2022-12-04 00:24:29 -05:00
parent bd376b7ff7
commit 35112ef68d
1 changed files with 12 additions and 18 deletions

30
4.rkt
View File

@ -1,32 +1,26 @@
#lang curly-fn racket #lang curly-fn racket
(require "scripts/aoc.rkt") (require "scripts/aoc.rkt"
(prefix-in is: data/integer-set))
;; solution for day 4 ;; solution for day 4
(define (part1 input) (define (part1 input)
(for/sum ([line (in-list input)]) (for*/sum ([line (in-list input)]
(match-define (list (cons a b) (cons c d)) line) [u (in-value (apply is:union line))]
(if (or (and (>= a c) (<= b d)) #:when (member u line))
(and (>= c a) (<= d b))) 1))
1 0)))
(define (part2 input) (define (part2 input)
(for/sum ([line (in-list input)]) (for*/sum ([line (in-list input)]
(match-define (list (cons a b) (cons c d)) line) [i (in-value (apply is:intersect line))]
(if (or (and (>= a c) (<= a d)) #:unless (zero? (is:count i)))
(and (>= c a) (<= c b)) 1))
(and (>= b c) (<= b d))
(and (>= d a) (<= d b)))
1 0)))
(define (parse fname) (define (parse fname)
(for/list ([line (in-list (file->lines fname))]) (for/list ([line (in-list (file->lines fname))])
(match-define (list l r) (string-split line ",")) (map #{~>> (string-split % "-") (map string->number) (apply is:make-range)}
(match-define (list a b) (string-split l "-")) (string-split line ","))))
(match-define (list c d) (string-split r "-"))
(list (cons (string->number a) (string->number b))
(cons (string->number c) (string->number d)))))
(module+ main (module+ main
(define input (parse "inputs/4")) (define input (parse "inputs/4"))