#lang curly-fn racket (require "scripts/aoc.rkt") ;; solution for day 16 (struct note [name test] #:transparent) (define (valid? notes val) (ormap #{(note-test %) val} notes)) (define (part1 input) (match-define (list notes yours others) input) (for*/sum ([tick (in-list others)] [val (in-list tick)] #:unless (valid? notes val)) val)) (define (part2 input) (match-define (list notes yours others) input) (define valid-tickets (filter #{andmap #{valid? notes %} %} others)) (define (match? idx cand-note) (andmap #{valid? (list cand-note) (list-ref % idx)} valid-tickets)) (define G (undirected-graph (for*/list ([idx (in-range (length yours))] [cand-note (in-list notes)] #:when (match? idx cand-note)) (list idx (note-name cand-note))))) (for/product ([elem (in-list (maximum-bipartite-matching G))] #:when (string-contains? (second elem) "departure")) (list-ref yours (first elem)))) (define (parse fname) (define sn string->number) (match-define (list notes-in yours-in others-in) (string-split (file->string fname) "\n\n")) (list (for/list ([line (in-list (string-split notes-in "\n"))]) (match line [(pregexp #px"^([^:]+): (\\d+)\\-(\\d+) or (\\d+)-(\\d+)$" (list _ name (app sn a) (app sn b) (app sn c) (app sn d))) (note name #{or (<= a % b) (<= c % d)})])) (map sn (string-split (second (string-split yours-in "\n")) ",")) (for/list ([line (in-list (rest (string-split others-in "\n")))]) (map sn (string-split line ","))))) (module+ main (define input (parse "inputs/16")) (answer 16 1 (time (part1 input))) (answer 16 2 (time (part2 input))) (displayln "meow"))