aoc2020/16.rkt

48 lines
1.7 KiB
Racket
Raw Normal View History

2020-12-16 05:31:34 +00:00
#lang curly-fn racket
(require "scripts/aoc.rkt")
;; solution for day 16
2020-12-16 05:53:10 +00:00
(struct note [name test] #:transparent)
2020-12-16 05:31:34 +00:00
2020-12-16 05:53:10 +00:00
(define (valid? notes val)
(ormap #{(note-test %) val} notes))
2020-12-16 05:31:34 +00:00
(define (part1 input)
(match-define (list notes yours others) input)
2020-12-16 05:53:10 +00:00
(for*/sum ([tick (in-list others)] [val (in-list tick)] #:unless (valid? notes val))
val))
2020-12-16 05:31:34 +00:00
(define (part2 input)
(match-define (list notes yours others) input)
2020-12-16 05:53:10 +00:00
(define valid-tickets (filter #{andmap #{valid? notes %} %} others))
(define (match? idx cand-note)
(andmap #{valid? (list cand-note) (list-ref % idx)} valid-tickets))
2020-12-16 05:31:34 +00:00
(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))))
2020-12-16 05:31:34 +00:00
(define (parse fname)
(define sn string->number)
2020-12-16 05:53:10 +00:00
(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 ",")))))
2020-12-16 05:31:34 +00:00
(module+ main
(define input (parse "inputs/16"))
(answer 16 1 (time (part1 input)))
(answer 16 2 (time (part2 input)))
(displayln "meow"))