diff --git a/src/02.rkt b/src/02.rkt index 9a39475..7da143a 100644 --- a/src/02.rkt +++ b/src/02.rkt @@ -13,17 +13,14 @@ (define input (map parse (problem-input 2))) (define part1 - (length (filter (λ (entry) - (match-let* ([(list min max char pass) entry] - [occurrences (length (filter (∂ char=? char) pass))]) - (<= min occurrences max))) - input))) + (count (match-lambda [(list min max char pass) + (<= min (count (λ~> (char=? char)) pass) max)]) + input)) (define part2 - (length (filter (λ (entry) - (match-let* ([(list min max char pass) entry]) - (xor (char=? (list-ref pass (sub1 min)) char) - (char=? (list-ref pass (sub1 max)) char)))) - input))) + (count (match-lambda [(list min max char pass) + (xor (char=? (list-ref pass (sub1 min)) char) + (char=? (list-ref pass (sub1 max)) char))]) + input)) (show-solution part1 part2) diff --git a/src/03.rkt b/src/03.rkt index 84c4001..112e14d 100644 --- a/src/03.rkt +++ b/src/03.rkt @@ -1,44 +1,16 @@ -#lang racket +#lang curly-fn racket (require "../lib.rkt") -(define test - (lists->vectors - (map string->list - '("..##......." - "#...#...#.." - ".#....#..#." - "..#.#...#.#" - ".#...##..#." - "..#.##....." - ".#.#.#....#" - ".#........#" - "#.##...#..." - "#...##....#" - ".#..#...#.#")))) - -(define input (lists->vectors (map string->list (problem-input 3)))) +(define input (problem-input 3)) (define (trees grid right down) - (define width (vector-length (vector-ref grid 0))) - (for/sum ([i (range 0 (vector-length grid) down)]) - (if (char=? #\# - (vector-ref (vector-ref grid i) - (% (* (/ i down) right) width))) - 1 0))) - -(define (test-input grid) - (define (print-test right down) - (let ([trees (trees grid right down)]) - (printf "Part 2 test (~a right, ~a down): ~a\n" - right down trees) - trees)) - (printf "Part 2 test: ~a\n" - (* (print-test 1 1) - (print-test 3 1) - (print-test 5 1) - (print-test 7 1) - (print-test 1 2)))) + (define width (string-length (list-ref grid 0))) + (define height (length grid)) + (count #{char=? #\# + (string-ref (list-ref grid %) + (modulo (* (/ % down) right) width))} + (range 0 height down))) (define part1 (trees input 3 1)) @@ -50,5 +22,4 @@ (trees input 7 1) (trees input 1 2))) -(test-input test) (show-solution part1 part2) diff --git a/src/04.rkt b/src/04.rkt index 860cf4f..d34976f 100644 --- a/src/04.rkt +++ b/src/04.rkt @@ -4,21 +4,6 @@ (define input (problem-input-grouped 4)) -(define test - "ecl:gry pid:860033327 eyr:2020 hcl:#fffffd -byr:1937 iyr:2017 cid:147 hgt:183cm - -iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 -hcl:#cfa07d byr:1929 - -hcl:#ae17e1 iyr:2013 -eyr:2024 -ecl:brn pid:760753108 byr:1931 -hgt:179cm - -hcl:#cfa07d eyr:2025 pid:166559648 -iyr:2011 ecl:brn hgt:59in") - (struct passport (byr iyr eyr hgt hcl ecl pid cid) #:transparent) @@ -45,16 +30,12 @@ iyr:2011 ecl:brn hgt:59in") [(regexp #px"^(\\d{2})in$" (list _ hgt)) (<= 59 (string->number hgt) 76)] [_ #f])) -(define (passports in) - (let* ([passport-entries (map (compose - (∂ map (compose - (∂ map ∂ (list string->symbol identity)) - (∂r string-split ":"))) - (∂r string-split " ") - (∂r string-replace "\n" " ")) - in)] - [passports (map (∂ foldl passport-set default-passport) - passport-entries)] +(define-values (part1 part2) + (let* ([passport-entries (map (λ~>> (string-split _ #px"[ \n]") + (map (match-lambda [(regexp #rx"(.+):(.+)" (list _ field value)) + (list (string->symbol field) value)]))) + input)] + [passports (map (∂ foldl passport-set default-passport) passport-entries)] [valid-raw? (struct/c passport string? string? string? string? string? string? string? any/c)] [valid? (struct/c passport (and/c string? (∘ (integer-in 1920 2002) string->number)) @@ -68,7 +49,4 @@ iyr:2011 ecl:brn hgt:59in") (values (count valid-raw? passports) (count valid? passports)))) -(define-values (part1 part2) - (passports input)) - (show-solution part1 part2) diff --git a/src/05.rkt b/src/05.rkt index db6d82e..123a91d 100644 --- a/src/05.rkt +++ b/src/05.rkt @@ -4,13 +4,16 @@ "../lib.rkt") (define (string->seat str) - (string->number (string-append "#b" (regexp-replaces str '([#rx"F" "0"] [#rx"B" "1"] [#rx"L" "0"] [#rx"R" "1"]))))) + (~>> str + (regexp-replaces _ '([#rx"F" "0"] [#rx"B" "1"] [#rx"L" "0"] [#rx"R" "1"])) + (string-append "#b") + string->number)) (define input (map string->seat (problem-input 5))) (define-values (part1 part2) - (let* ([minimum ($ min input)] - [maximum ($ max input)] + (let* ([minimum (apply min input)] + [maximum (apply max input)] [seats (list->set (range minimum (add1 maximum)))] [filled (list->set input)]) (values maximum (set-first (set-subtract seats filled))))) diff --git a/src/07.rkt b/src/07.rkt index 5bd5703..a97eda5 100644 --- a/src/07.rkt +++ b/src/07.rkt @@ -22,10 +22,10 @@ (define (count-bags graph source) (let ([neighbours (get-neighbours graph source)]) - (apply + 1 (map (λ (neighbour) + (add1 (sum (map (λ (neighbour) (let ([weight (edge-weight graph source neighbour)]) (* weight (count-bags graph neighbour)))) - neighbours)))) + neighbours))))) (define-values (part1 part2) (let*-values ([(contained contains) (strings->graphs input)]