Browse Source

add final answer validators, regex part 4 for no reason

canon
Hazel Levine 8 months ago
parent
commit
c194463fcc
Signed by: hazel GPG Key ID: 1884029A28789A62
  1. 2
      README.org
  2. 11
      day1.rkt
  3. 10
      day2.rkt
  4. 11
      day3.rkt
  5. 38
      day4.rkt

2
README.org

@ -156,3 +156,5 @@ After that, the following contracts were used for each field, along with passing
+ =cid=: =any/c=
Again, run =count= with this validator on the parsed input data.
** Revisions
I wanted to see if I could do the Part 2 validator with almost nothing but regex, so I did that.

11
day1.rkt

@ -43,3 +43,14 @@
(define lines (map string->number (port->lines prt)))
(answer 1 1 (day1a lines))
(answer 1 2 (day1b lines)))))
(module+ test
(require rackunit)
(call-with-input-file "data/day1.txt"
(lambda (prt)
(define lines (map string->number (port->lines prt)))
(check-equal? (day1a lines) 542619
"final answer part 1")
(check-equal? (day1b lines) 32858450
"final answer part 2"))))

10
day2.rkt

@ -61,4 +61,12 @@
(check-false (password-valid?/b example2)
"1-3 b: cdefg, part 2 validity")
(check-false (password-valid?/b example3)
"2-9 c: ccccccccc, part 2 validity"))
"2-9 c: ccccccccc, part 2 validity")
(call-with-input-file "data/day2.txt"
(lambda (prt)
(define lines (port->lines prt))
(check-equal? (solution lines password-valid?/a) 572
"final answer part 1")
(check-equal? (solution lines password-valid?/b) 306
"final answer part 2"))))

11
day3.rkt

@ -30,3 +30,14 @@
(define input (list->vector (port->lines prt)))
(answer 3 1 (day3a input))
(answer 3 2 (day3b input)))))
(module+ test
(require rackunit)
(call-with-input-file "data/day3.txt"
(lambda (prt)
(define input (list->vector (port->lines prt)))
(check-equal? (day3a input) 237
"final answer part 1")
(check-equal? (day3b input) 2106818610
"final answer part 2"))))

38
day4.rkt

@ -31,26 +31,30 @@
string? string? string? string?
string? string? string? any/c))
(define (string-length/c num) (compose1 (=/c num) string-length))
(define ((string-integer-in low high) str)
(<= low (string->number str) high))
(define birth-year/c (curry regexp-match? #px"^(19[2-9][0-9]|200[0-2])$"))
(define issue-year/c (curry regexp-match? #px"^(201[0-9]|2020)$"))
(define expiration-year/c (curry regexp-match? #px"^(202[0-9]|2030)"))
(define (height/c str)
(match str
[(pregexp #px"([0-9]+)cm" (list _ hgt)) ((string-integer-in 150 193) hgt)]
[(pregexp #px"([0-9]+)in" (list _ hgt)) ((string-integer-in 59 76) hgt)]
[(pregexp #px"^([0-9]+)(cm|in)$" (list _ hgt unt))
(match unt
["cm" (<= 150 (string->number hgt) 193)]
["in" (<= 59 (string->number hgt) 76)])]
[_ #f]))
(define color/c (curry regexp-match #px"#[0-9a-f]{6}"))
(define hair-color/c (curry regexp-match? #px"#[0-9a-f]{6}"))
(define eye-color/c (curry regexp-match? #px"^(amb|blu|brn|gry|grn|hzl|oth)$"))
(define passport-id/c (curry regexp-match? #px"^\\d{9}$"))
(define valid-passport?/b
(and/c valid-passport?/a
(struct/c passport
(and/c (string-length/c 4) (string-integer-in 1920 2002))
(and/c (string-length/c 4) (string-integer-in 2010 2020))
(and/c (string-length/c 4) (string-integer-in 2020 2030))
birth-year/c
issue-year/c
expiration-year/c
height/c
color/c
(or/c "amb" "blu" "brn" "gry" "grn" "hzl" "oth")
(and/c (string-length/c 9) (string-integer-in 0 999999999))
hair-color/c
eye-color/c
passport-id/c
any/c)))
(define (solution valid? in)
@ -76,4 +80,12 @@
"example 1, parse")
(check-pred valid-passport?/a example1
"example 1, valid"))
"example 1, valid")
(call-with-input-file "data/day4.txt"
(lambda (prt)
(define input (string-split (port->string prt) "\n\n"))
(check-equal? (solution valid-passport?/a input) 190
"final answer part 1")
(check-equal? (solution valid-passport?/b input) 121
"final answer part 2"))))
Loading…
Cancel
Save