#lang curly-fn racket (require "scripts/aoc.rkt") ;; solution for day 19 ;; 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))) (define (part1 input) (match-define (cons rules lines) input) (run-part rules lines)) (define (part2 input) (match-define (cons rules lines) input) ;; 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)) ;; parse input file (define (parse fname) (match-define (list rules input) (string-split (file->string fname) "\n\n")) (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"))