From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2021/day4/04a-alt.rkt | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2021/day4/04a-alt.rkt (limited to '2021/day4/04a-alt.rkt') 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))) -- cgit v1.2.3