aboutsummaryrefslogtreecommitdiff
path: root/aoc2020/day-02/day-02.rkt
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
committerJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
commit8ab65dc2da1742eb86ec636c50c7018385b68167 (patch)
treec4fd556aca9b867cfa1f2f174128c30857353884 /aoc2020/day-02/day-02.rkt
parentfafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff)
downloadgleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz
gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip
prep for 2023, renaming for consistency
Diffstat (limited to 'aoc2020/day-02/day-02.rkt')
-rw-r--r--aoc2020/day-02/day-02.rkt33
1 files changed, 33 insertions, 0 deletions
diff --git a/aoc2020/day-02/day-02.rkt b/aoc2020/day-02/day-02.rkt
new file mode 100644
index 0000000..9e22a1a
--- /dev/null
+++ b/aoc2020/day-02/day-02.rkt
@@ -0,0 +1,33 @@
+#lang racket
+(require "../../jj-aoc.rkt"
+ threading)
+
+(struct policy (least most char pwd) #:transparent)
+
+(define (make-policy least-str most-str char-str pwd)
+ (policy (string->number least-str) (string->number most-str) (string-ref char-str 0) pwd))
+
+(define policies
+ (for/list ([l (in-lines (open-day 02 2020))])
+ (~>> l (regexp-match #px"(\\d+)-(\\d+) (\\w): (.*)") rest (apply make-policy))))
+
+;; part 1
+(define (valid-policy? p)
+ (~>> p
+ policy-pwd
+ string->list
+ (count (curry char=? (policy-char p)))
+ ((λ (n) (and (>= n (policy-least p)) (<= n (policy-most p)))))))
+
+(for/sum ([p (in-list policies)] #:when (valid-policy? p)) 1)
+
+;; part 2
+(define (valid-revised-policy? p)
+ (~>> p
+ policy-pwd
+ string->list
+ ((λ (lst) (list (list-ref lst (sub1 (policy-most p))) (list-ref lst (sub1 (policy-least p))))))
+ (count (curry char=? (policy-char p)))
+ (= 1)))
+
+(for/sum ([p (in-list policies)] #:when (valid-revised-policy? p)) 1)