summaryrefslogtreecommitdiff
path: root/password-philosophy/pp.scm
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2024-06-09 20:28:10 -0600
committerbd <bdunahu@operationnull.com>2024-06-09 20:28:10 -0600
commitf3ae22b302a298984df62a4aaba275c15d010072 (patch)
tree099398ef5ca7b1f462d54ba78b02e0999bdf2078 /password-philosophy/pp.scm
parent2c85332e1e402653ed44ea1a5248cb7d11259389 (diff)
AoC 2020.2 p1
Diffstat (limited to 'password-philosophy/pp.scm')
-rw-r--r--password-philosophy/pp.scm43
1 files changed, 43 insertions, 0 deletions
diff --git a/password-philosophy/pp.scm b/password-philosophy/pp.scm
new file mode 100644
index 0000000..9431ffa
--- /dev/null
+++ b/password-philosophy/pp.scm
@@ -0,0 +1,43 @@
+(define-module (pp)
+ #:use-module (srfi srfi-1)
+ #:export (pp
+ parse-line
+ count-occurrences
+ valid-password?))
+
+(define (pp str)
+ (count (lambda (x) x)
+ (map (lambda (line)
+ (let ((parsed (parse-line line)))
+ (valid-password? (car parsed)
+ (cadr parsed)
+ (caddr parsed))))
+ (string-split
+ (string-trim-both str char-set:whitespace)
+ #\newline))))
+
+(define (parse-line str)
+ "Parses a line of the puzzle format
+into a workable datastructure:
+(passwd (char (min max)))"
+ (let* ((fields (string-split str char-set:whitespace))
+ (range (map string->number (string-split (car fields) #\-)))
+ (char (string-ref (cadr fields) 0))
+ (passwd (caddr fields)))
+ (list passwd
+ char
+ (cons (car range)
+ (cadr range)))))
+
+(define (valid-password? passwd char range)
+ "Given PASSWD, checks that CHAR appears
+within RANGE number of times. RANGE is a
+cons pair."
+ (let ((occurrences (count-occurrences passwd char)))
+ (and (>= occurrences (car range))
+ (>= (cdr range) occurrences))))
+
+(define (count-occurrences str char)
+ "Returns the number of times CHR
+appears in STR."
+ (string-length (string-filter char str)))