1
0
Fork 0

Minor refactoring of Days 1 - 7.

This commit is contained in:
Jonathan Chan 2020-12-07 20:49:06 -08:00 committed by Jonathan Chan
parent 452ffe283b
commit 9307d1c01d
5 changed files with 29 additions and 80 deletions

View File

@ -13,17 +13,14 @@
(define input (map parse (problem-input 2))) (define input (map parse (problem-input 2)))
(define part1 (define part1
(length (filter (λ (entry) (count (match-lambda [(list min max char pass)
(match-let* ([(list min max char pass) entry] (<= min (count (λ~> (char=? char)) pass) max)])
[occurrences (length (filter ( char=? char) pass))]) input))
(<= min occurrences max)))
input)))
(define part2 (define part2
(length (filter (λ (entry) (count (match-lambda [(list min max char pass)
(match-let* ([(list min max char pass) entry])
(xor (char=? (list-ref pass (sub1 min)) char) (xor (char=? (list-ref pass (sub1 min)) char)
(char=? (list-ref pass (sub1 max)) char)))) (char=? (list-ref pass (sub1 max)) char))])
input))) input))
(show-solution part1 part2) (show-solution part1 part2)

View File

@ -1,44 +1,16 @@
#lang racket #lang curly-fn racket
(require "../lib.rkt") (require "../lib.rkt")
(define test (define input (problem-input 3))
(lists->vectors
(map string->list
'("..##......."
"#...#...#.."
".#....#..#."
"..#.#...#.#"
".#...##..#."
"..#.##....."
".#.#.#....#"
".#........#"
"#.##...#..."
"#...##....#"
".#..#...#.#"))))
(define input (lists->vectors (map string->list (problem-input 3))))
(define (trees grid right down) (define (trees grid right down)
(define width (vector-length (vector-ref grid 0))) (define width (string-length (list-ref grid 0)))
(for/sum ([i (range 0 (vector-length grid) down)]) (define height (length grid))
(if (char=? #\# (count #{char=? #\#
(vector-ref (vector-ref grid i) (string-ref (list-ref grid %)
(% (* (/ i down) right) width))) (modulo (* (/ % down) right) width))}
1 0))) (range 0 height down)))
(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 part1 (define part1
(trees input 3 1)) (trees input 3 1))
@ -50,5 +22,4 @@
(trees input 7 1) (trees input 7 1)
(trees input 1 2))) (trees input 1 2)))
(test-input test)
(show-solution part1 part2) (show-solution part1 part2)

View File

@ -4,21 +4,6 @@
(define input (problem-input-grouped 4)) (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 (struct passport
(byr iyr eyr hgt hcl ecl pid cid) (byr iyr eyr hgt hcl ecl pid cid)
#:transparent) #:transparent)
@ -45,16 +30,12 @@ iyr:2011 ecl:brn hgt:59in")
[(regexp #px"^(\\d{2})in$" (list _ hgt)) (<= 59 (string->number hgt) 76)] [(regexp #px"^(\\d{2})in$" (list _ hgt)) (<= 59 (string->number hgt) 76)]
[_ #f])) [_ #f]))
(define (passports in) (define-values (part1 part2)
(let* ([passport-entries (map (compose (let* ([passport-entries (map (λ~>> (string-split _ #px"[ \n]")
( map (compose (map (match-lambda [(regexp #rx"(.+):(.+)" (list _ field value))
( map (list string->symbol identity)) (list (string->symbol field) value)])))
(∂r string-split ":"))) input)]
(∂r string-split " ") [passports (map ( foldl passport-set default-passport) passport-entries)]
(∂r string-replace "\n" " "))
in)]
[passports (map ( foldl passport-set default-passport)
passport-entries)]
[valid-raw? (struct/c passport string? string? string? string? string? string? string? any/c)] [valid-raw? (struct/c passport string? string? string? string? string? string? string? any/c)]
[valid? (struct/c passport [valid? (struct/c passport
(and/c string? ( (integer-in 1920 2002) string->number)) (and/c string? ( (integer-in 1920 2002) string->number))
@ -68,7 +49,4 @@ iyr:2011 ecl:brn hgt:59in")
(values (count valid-raw? passports) (values (count valid-raw? passports)
(count valid? passports)))) (count valid? passports))))
(define-values (part1 part2)
(passports input))
(show-solution part1 part2) (show-solution part1 part2)

View File

@ -4,13 +4,16 @@
"../lib.rkt") "../lib.rkt")
(define (string->seat str) (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 input (map string->seat (problem-input 5)))
(define-values (part1 part2) (define-values (part1 part2)
(let* ([minimum ($ min input)] (let* ([minimum (apply min input)]
[maximum ($ max input)] [maximum (apply max input)]
[seats (list->set (range minimum (add1 maximum)))] [seats (list->set (range minimum (add1 maximum)))]
[filled (list->set input)]) [filled (list->set input)])
(values maximum (set-first (set-subtract seats filled))))) (values maximum (set-first (set-subtract seats filled)))))

View File

@ -22,10 +22,10 @@
(define (count-bags graph source) (define (count-bags graph source)
(let ([neighbours (get-neighbours graph source)]) (let ([neighbours (get-neighbours graph source)])
(apply + 1 (map (λ (neighbour) (add1 (sum (map (λ (neighbour)
(let ([weight (edge-weight graph source neighbour)]) (let ([weight (edge-weight graph source neighbour)])
(* weight (count-bags graph neighbour)))) (* weight (count-bags graph neighbour))))
neighbours)))) neighbours)))))
(define-values (part1 part2) (define-values (part1 part2)
(let*-values ([(contained contains) (strings->graphs input)] (let*-values ([(contained contains) (strings->graphs input)]