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