diff options
author | Aria <me@aria.rip> | 2023-01-02 21:58:56 +0000 |
---|---|---|
committer | Aria <me@aria.rip> | 2023-01-02 21:58:56 +0000 |
commit | 5eb58ad076f2cd435b11b140820da224b60b73d5 (patch) | |
tree | 2a67939595fbf993ff04f69b9cd3f0aa20827d96 /2021/day4 |
initial commit
Diffstat (limited to '2021/day4')
-rw-r--r-- | 2021/day4/04a-alt.rkt | 49 | ||||
-rw-r--r-- | 2021/day4/04b-alt.rkt | 49 | ||||
-rw-r--r-- | 2021/day4/04t.rkt | 9 | ||||
-rw-r--r-- | 2021/day4/parser.rkt | 8 | ||||
-rw-r--r-- | 2021/day4/reader.rkt | 21 |
5 files changed, 136 insertions, 0 deletions
diff --git a/2021/day4/04a-alt.rkt b/2021/day4/04a-alt.rkt new file mode 100644 index 0000000..db03881 --- /dev/null +++ b/2021/day4/04a-alt.rkt @@ -0,0 +1,49 @@ +#lang racket + +(require racket/file) + +(define (parse-row x) + (map string->number (filter (lambda (x) (< 0 (string-length x))) (regexp-split #rx" +" x)))) + +(define (parse-board x) + (map parse-row (string-split x "\n"))) + +(define (get-numbers x) + (define number-line (car (string-split x "\n\n"))) + (map string->number (string-split number-line ","))) + +(define (get-boards x) + (define boards (cdr (string-split x "\n\n"))) + (map parse-board boards)) + +(define (subsetOf xs ys) + (subset? (list->set xs) (list->set ys))) + +(define (transpose xs) + (apply map list xs)) + +(define (check-win board numbers) + (define winningColumns (filter (lambda (x) (subsetOf x numbers)) board)) + (define winningRows (filter (lambda (x) (subsetOf x numbers)) (transpose board))) + (cond [(not (null? winningColumns)) (car winningColumns)] + [(not (null? winningRows)) (car winningRows)] + [else #f])) + +(define input (file->string "./input")) +(define numbers (get-numbers input)) +(define boards (get-boards input)) + +(define winningBoard #f) +(define winningRun #f) +(define wonOnRound 0) +(for* ([rnd (length numbers)] + [board boards] + #:unless winningRun + ) + (set! winningRun (check-win board (take numbers rnd))) + (set! winningBoard board) + (set! wonOnRound rnd)) + +(* + (foldr + 0 (filter (lambda (x) (not (member x (take numbers wonOnRound)))) (flatten winningBoard))) + (list-ref numbers (- wonOnRound 1))) diff --git a/2021/day4/04b-alt.rkt b/2021/day4/04b-alt.rkt new file mode 100644 index 0000000..ab32bc2 --- /dev/null +++ b/2021/day4/04b-alt.rkt @@ -0,0 +1,49 @@ +#lang racket + +(require racket/file) + +(define (parse-row x) + (map string->number (filter (lambda (x) (< 0 (string-length x))) (regexp-split #rx" +" x)))) + +(define (parse-board x) + (map parse-row (string-split x "\n"))) + +(define (get-numbers x) + (define number-line (car (string-split x "\n\n"))) + (map string->number (string-split number-line ","))) + +(define (get-boards x) + (define boards (cdr (string-split x "\n\n"))) + (map parse-board boards)) + +(define (subsetOf xs ys) + (subset? (list->set xs) (list->set ys))) + +(define (transpose xs) + (apply map list xs)) + +(define (check-win board numbers) + (define winningColumns (filter (lambda (x) (subsetOf x numbers)) board)) + (define winningRows (filter (lambda (x) (subsetOf x numbers)) (transpose board))) + (cond [(not (null? winningColumns)) (car winningColumns)] + [(not (null? winningRows)) (car winningRows)] + [else #f])) + +(define input (file->string "./input")) +(define numbers (get-numbers input)) +(define boards (get-boards input)) + +(define ht (make-hash)) +(for* ([rnd (length numbers)] + [board boards] + #:unless (hash-ref ht board #f) + ) + (cond [(check-win board (take numbers rnd)) (hash-set! ht board rnd)])) + +(define maxTurn 0) +(define lastBoard #f) +(hash-for-each ht (lambda (k v) (cond [(> v maxTurn) (set! maxTurn v) (set! lastBoard k)]))) + +(* + (foldr + 0 (filter (lambda (x) (not (member x (take numbers maxTurn)))) (flatten lastBoard))) + (list-ref numbers (- maxTurn 1))) diff --git a/2021/day4/04t.rkt b/2021/day4/04t.rkt new file mode 100644 index 0000000..b3f10ad --- /dev/null +++ b/2021/day4/04t.rkt @@ -0,0 +1,9 @@ +#lang reader "reader.rkt" +7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1 + +22 13 17 11 0 + 8 2 23 4 24 +21 9 14 16 7 + 6 10 3 18 5 +1 12 20 15 19 + diff --git a/2021/day4/parser.rkt b/2021/day4/parser.rkt new file mode 100644 index 0000000..449d886 --- /dev/null +++ b/2021/day4/parser.rkt @@ -0,0 +1,8 @@ +#lang brag +file: numbers "\n" "\n" boards +numbers: number ("," number)* +number: digit+ +digit: "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" +boards: board ("\n" board)* +board: row "\n" row "\n" row "\n" row "\n" row "\n" +row: " "+ number " "+ number " "+ number " "+ number " "+ number diff --git a/2021/day4/reader.rkt b/2021/day4/reader.rkt new file mode 100644 index 0000000..b777f92 --- /dev/null +++ b/2021/day4/reader.rkt @@ -0,0 +1,21 @@ +#lang racket + +(require "parser.rkt") +(require brag/support) + +(define (make-tokenizer port) + (define (next-token) + (define bf-lexer + (lexer + [(char-set "\n1234567890, ") lexeme] + [any-char (next-token)])) + (bf-lexer port)) + next-token) + +(define (read-syntax path port) + (define parse-tree (parse path (make-tokenizer port))) + (define module-datum `(module day4 racket + ,parse-tree)) + (datum->syntax #f module-datum)) + +(provide read-syntax) |