2020-12-19 05:50:29 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
|
|
|
|
;; solution for day 19
|
|
|
|
|
2020-12-19 06:24:35 +00:00
|
|
|
;; MEGA CHEATING
|
|
|
|
(define (run-part defs-in lines)
|
|
|
|
;; add the #lang and the top rule
|
|
|
|
(define defs
|
|
|
|
(string-append "#lang brag\ntop: r0\n"
|
|
|
|
;; replace all the rule numbers N with rN otherwise brag has issues
|
|
|
|
(regexp-replace* #px"\\d+" defs-in (lambda (x) (format "r~a" x)))))
|
|
|
|
;; create a temporary file with the parser contents for brag
|
|
|
|
(define file (make-temporary-file))
|
|
|
|
(call-with-output-file file (lambda (out) (write-string defs out)) #:exists 'truncate)
|
|
|
|
;; require it
|
|
|
|
(define parse (dynamic-require file 'parse))
|
|
|
|
;; yeeeeet
|
|
|
|
(for/sum ([line (in-list lines)])
|
|
|
|
(with-handlers ([exn:fail? (lambda (_) 0)]) ;; catch parse error and just add 0
|
|
|
|
(parse line)
|
|
|
|
1)))
|
2020-12-19 05:50:29 +00:00
|
|
|
|
|
|
|
(define (part1 input)
|
|
|
|
(match-define (cons rules lines) input)
|
2020-12-19 06:24:35 +00:00
|
|
|
(run-part rules lines))
|
2020-12-19 05:50:29 +00:00
|
|
|
|
|
|
|
(define (part2 input)
|
|
|
|
(match-define (cons rules lines) input)
|
2020-12-19 06:24:35 +00:00
|
|
|
;; replace the lines for 8 and 11 with their replacements, then run it
|
|
|
|
(run-part
|
|
|
|
(string-join
|
|
|
|
(for/list ([line (in-list (string-split rules "\n"))])
|
|
|
|
(match line
|
|
|
|
[(pregexp #px"^8:") "8: 42 | 42 8"]
|
|
|
|
[(pregexp #px"^11:") "11: 42 31 | 42 11 31"]
|
|
|
|
[x x]))
|
|
|
|
"\n")
|
|
|
|
lines))
|
2020-12-19 05:50:29 +00:00
|
|
|
|
|
|
|
;; parse input file
|
|
|
|
(define (parse fname)
|
2020-12-19 06:24:35 +00:00
|
|
|
(match-define (list rules input) (string-split (file->string fname) "\n\n"))
|
2020-12-19 05:50:29 +00:00
|
|
|
(cons rules (string-split input "\n")))
|
|
|
|
|
|
|
|
(module+ main
|
|
|
|
(define input (parse "inputs/19"))
|
|
|
|
(answer 19 1 (time (part1 input)))
|
|
|
|
(answer 19 2 (time (part2 input)))
|
|
|
|
(displayln "meow"))
|