day 16: cleanup lol

This commit is contained in:
xenia 2020-12-16 00:53:10 -05:00
parent 8674bc7d56
commit e2468a6e71
1 changed files with 35 additions and 55 deletions

84
16.rkt
View File

@ -4,78 +4,58 @@
;; solution for day 16 ;; solution for day 16
(struct note [name rule0 rule1] #:transparent) (struct note [name test] #:transparent)
(define (invalid? notes val) (define (valid? notes val)
(not (ormap #{(note-test %) val} notes))
(for/or ([n (in-list notes)])
(match-define (note name (cons a b) (cons c d)) n)
(or (and (>= val a) (<= val b)) (and (>= val c) (<= val d))))))
(define (part1 input) (define (part1 input)
(match-define (list notes yours others) input) (match-define (list notes yours others) input)
(for/sum ([tick (in-list others)]) (for*/sum ([tick (in-list others)] [val (in-list tick)] #:unless (valid? notes val))
(for/sum ([val (in-list tick)] #:when (invalid? notes val)) val))) val))
(define (part2 input) (define (part2 input)
(match-define (list notes yours others) input) (match-define (list notes yours others) input)
(define (ticket-valid? tick) (define valid-tickets (filter #{andmap #{valid? notes %} %} others))
(not (for/or ([val (in-list tick)]) (invalid? notes val))))
(define valid-tickets (filter ticket-valid? others))
(define mapping (make-hash)) (define mapping
(for/vector ([idx (in-range (length yours))])
(for ([idx (in-range (length yours))]) (define matching-notes
(define decided-note (mutable-set)) (for/mutable-set ([cand-note (in-list notes)]
(for ([cand-note (in-list notes)]) #:when (andmap #{valid? (list cand-note) (list-ref % idx)} valid-tickets))
(define shonks (note-name cand-note)))
(for/and ([tick (in-list valid-tickets)]) (when (set-empty? matching-notes) (error "couldn't decide..."))
(not (invalid? (list cand-note) (list-ref tick idx))))) matching-notes))
(when shonks
(set-add! decided-note (note-name cand-note))))
(when (set-empty? decided-note) (error "couldn't decide..."))
(hash-set! mapping idx decided-note))
; (displayln mapping)
(let loop () (let loop ()
(for ([(k v) (in-hash mapping)]) (define has-conflicts #f)
(when (= 1 (set-count v)) (for ([v (in-vector mapping)] [k (in-naturals)])
(cond
[(= 1 (set-count v))
(define fst (set-first v)) (define fst (set-first v))
(for ([k2 (in-list (hash-keys mapping))] #:unless (= k2 k)) (for ([v2 (in-vector mapping)] [k2 (in-naturals)] #:unless (= k2 k))
(set-remove! (hash-ref mapping k2) fst)))) (set-remove! v2 fst))]
(when (for/or ([(k v) (in-hash mapping)]) [else (set! has-conflicts #t)]))
(> (set-count v) 1)) (when has-conflicts
(displayln "looping")
(loop))) (loop)))
(displayln mapping)
(for/product ([val (in-list yours)] [idx (in-naturals)]) (for/product ([val (in-list yours)] [name-set (in-vector mapping)])
(define n (set-first (hash-ref mapping idx))) (if (string-contains? (set-first name-set) "departure")
(displayln n) val
(if (string-contains? n "departure")
(begin (displayln val) val)
1))) 1)))
;; parse input file ;; parse input file
(define (parse fname) (define (parse fname)
(match-define (list notes yours others) (string-split (file->string fname) "\n\n"))
(define sn string->number) (define sn string->number)
(define pnotes (for/list ([line (in-list (string-split notes "\n"))]) (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 (match line
[(pregexp #px"^([^:]+): (\\d+)\\-(\\d+) or (\\d+)-(\\d+)$" [(pregexp #px"^([^:]+): (\\d+)\\-(\\d+) or (\\d+)-(\\d+)$"
(list _ name a b c d)) (list _ name (app sn a) (app sn b) (app sn c) (app sn d)))
(note name (cons (sn a) (sn b)) (cons (sn c) (sn d)))]))) (note name #{or (<= a % b) (<= c % d)})]))
(define pyours (map sn (string-split (second (string-split yours "\n")) ","))) (map sn (string-split (second (string-split yours-in "\n")) ","))
(define pothers (for/list ([line (in-list (rest (string-split others "\n")))]) (for/list ([line (in-list (rest (string-split others-in "\n")))])
(map sn (string-split line ",")))) (map sn (string-split line ",")))))
(list pnotes pyours pothers))
(module+ test
(require rackunit)
;; tests here
(displayln "no tests :("))
(module+ main (module+ main
(define input (parse "inputs/16")) (define input (parse "inputs/16"))