Day 6; some refactoring to use new problem-input-grouped and $.
This commit is contained in:
parent
18f13a4da3
commit
9ae585ebed
File diff suppressed because it is too large
Load Diff
7
lib.rkt
7
lib.rkt
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
(provide problem-input
|
(provide problem-input
|
||||||
problem-input-all
|
problem-input-all
|
||||||
|
problem-input-grouped
|
||||||
show-solution
|
show-solution
|
||||||
|
|
||||||
make-vector-grid
|
make-vector-grid
|
||||||
|
@ -95,6 +96,12 @@
|
||||||
[path (string-append "../input/" filename ".txt")])
|
[path (string-append "../input/" filename ".txt")])
|
||||||
(read-file path)))
|
(read-file path)))
|
||||||
|
|
||||||
|
;; problem-input-grouped : number? -> (listof string?)
|
||||||
|
;; Return contents of input file input/xx.txt as a list of strings,
|
||||||
|
;; where each string is a group of lines separated by newlines.
|
||||||
|
(define (problem-input-grouped n)
|
||||||
|
(string-split (problem-input-all n) "\n\n"))
|
||||||
|
|
||||||
;; show-solution : a -> b -> void
|
;; show-solution : a -> b -> void
|
||||||
;; Print part1 and part2 on separate lines.
|
;; Print part1 and part2 on separate lines.
|
||||||
(define (show-solution part1 part2)
|
(define (show-solution part1 part2)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
(require "../lib.rkt")
|
(require "../lib.rkt")
|
||||||
|
|
||||||
(define input (problem-input-all 4))
|
(define input (problem-input-grouped 4))
|
||||||
|
|
||||||
(define test
|
(define test
|
||||||
"ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
|
"ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
|
||||||
|
@ -52,7 +52,7 @@ iyr:2011 ecl:brn hgt:59in")
|
||||||
(∂r string-split ":")))
|
(∂r string-split ":")))
|
||||||
(∂r string-split " ")
|
(∂r string-split " ")
|
||||||
(∂r string-replace "\n" " "))
|
(∂r string-replace "\n" " "))
|
||||||
(string-split in "\n\n"))]
|
in)]
|
||||||
[passports (map (∂ foldl passport-set default-passport)
|
[passports (map (∂ foldl passport-set default-passport)
|
||||||
passport-entries)]
|
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)]
|
||||||
|
|
|
@ -9,8 +9,8 @@
|
||||||
(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 (apply min input)]
|
(let* ([minimum ($ min input)]
|
||||||
[maximum (apply max input)]
|
[maximum ($ 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)))))
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
#lang racket
|
||||||
|
|
||||||
|
(require racket/set
|
||||||
|
"../lib.rkt")
|
||||||
|
|
||||||
|
(define (parse group)
|
||||||
|
(map (∘ list->set string->list) (string-split group "\n")))
|
||||||
|
|
||||||
|
(define input (map parse (problem-input-grouped 6)))
|
||||||
|
|
||||||
|
(define-values (part1 part2)
|
||||||
|
(let* ([any-counts (map (∘ set-count ($ set-union)) input)]
|
||||||
|
[all-counts (map (∘ set-count ($ set-intersect)) input)])
|
||||||
|
(values (sum any-counts) (sum all-counts))))
|
||||||
|
|
||||||
|
(show-solution part1 part2)
|
Loading…
Reference in New Issue