2020-12-21 05:47:51 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
|
|
|
|
;; solution for day 21
|
|
|
|
|
2020-12-21 06:33:43 +00:00
|
|
|
(struct line [is as] #:transparent)
|
|
|
|
(struct data [lines all-is all-as tainted] #:transparent)
|
|
|
|
|
2020-12-21 05:47:51 +00:00
|
|
|
(define (part1 input)
|
2020-12-21 06:33:43 +00:00
|
|
|
(match-define (data lines all-is all-as tainted) input)
|
2020-12-21 06:55:22 +00:00
|
|
|
(define all-tainted (apply set-union (hash-values tainted)))
|
|
|
|
|
|
|
|
(for*/sum ([l (in-list lines)] [i (in-set (line-is l))] #:unless (set-member? all-tainted i))
|
2020-12-21 06:33:43 +00:00
|
|
|
1))
|
2020-12-21 05:47:51 +00:00
|
|
|
|
|
|
|
(define (part2 input)
|
2020-12-21 06:33:43 +00:00
|
|
|
(match-define (data lines all-is all-as tainted) input)
|
2020-12-21 06:55:22 +00:00
|
|
|
(define G (undirected-graph
|
|
|
|
(for*/list ([(k v*) (in-hash tainted)] [v (in-set v*)])
|
|
|
|
(list k v))))
|
2020-12-21 05:47:51 +00:00
|
|
|
(define matching (maximum-bipartite-matching G))
|
|
|
|
|
2020-12-21 06:33:43 +00:00
|
|
|
(string-join
|
2020-12-21 06:55:22 +00:00
|
|
|
(map second (sort matching string<? #:key first))
|
2020-12-21 06:33:43 +00:00
|
|
|
","))
|
2020-12-21 05:47:51 +00:00
|
|
|
|
|
|
|
;; parse input file
|
|
|
|
(define (parse fname)
|
2020-12-21 06:33:43 +00:00
|
|
|
(define-values [lines all-is all-as]
|
|
|
|
(for/fold ([lines '()] [all-is (set)] [all-as (set)]) ([l (in-list (file->lines fname))])
|
|
|
|
(match-define (list _ p1 p2) (regexp-match #px"^([^\\(]+) \\(contains ([^\\)]+)\\)+" l))
|
|
|
|
(define is (list->set (string-split p1 " ")))
|
|
|
|
(define as (list->set (string-split p2 ", ")))
|
|
|
|
(values (cons (line is as) lines) (set-union all-is is) (set-union all-as as))))
|
|
|
|
|
|
|
|
(define tainted
|
|
|
|
(for/hash ([a (in-set all-as)])
|
|
|
|
(values a (for/fold ([necessary all-is]) ([l (in-list lines)]
|
|
|
|
#:when (set-member? (line-as l) a))
|
|
|
|
(set-intersect (line-is l) necessary)))))
|
|
|
|
(data lines all-is all-as tainted))
|
2020-12-21 05:47:51 +00:00
|
|
|
|
|
|
|
(module+ main
|
|
|
|
(define input (parse "inputs/21"))
|
|
|
|
(answer 21 1 (time (part1 input)))
|
|
|
|
(answer 21 2 (time (part2 input)))
|
|
|
|
(displayln "meow"))
|