summaryrefslogtreecommitdiff
path: root/password-philosophy
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2024-06-09 20:49:33 -0600
committerbd <bdunahu@operationnull.com>2024-06-09 20:49:33 -0600
commitaec326408dd6ad16347b5661112895e60335893a (patch)
treee6f31a6858256a02fdd7bea60c869c76fd4e1a7e /password-philosophy
parentf3ae22b302a298984df62a4aaba275c15d010072 (diff)
AoC 2020.2 p2
Diffstat (limited to 'password-philosophy')
-rw-r--r--password-philosophy/README.org4
-rw-r--r--password-philosophy/main.scm6
-rw-r--r--password-philosophy/pp-test.scm33
-rw-r--r--password-philosophy/pp.scm30
4 files changed, 57 insertions, 16 deletions
diff --git a/password-philosophy/README.org b/password-philosophy/README.org
index 2ceb22c..15d87ab 100644
--- a/password-philosophy/README.org
+++ b/password-philosophy/README.org
@@ -31,3 +31,7 @@ The password verification method has changed:
#+end_example
Now, the "range" corresponds to two indices (indexed from 1). Of these indices, exactly one needs to be the given CHAR.
+
+*** Method
+
+See implementation. Simply, we add a wrapper to the valid-password? function that refines the inputs before calling valid-password? as usual.
diff --git a/password-philosophy/main.scm b/password-philosophy/main.scm
index fd62ba8..fa142a3 100644
--- a/password-philosophy/main.scm
+++ b/password-philosophy/main.scm
@@ -12,5 +12,9 @@
(loop (string-append result line "\n"))))))
(define (main args)
- (display (pp (stdin-to-str)))
+ (let ((result
+ (if (null? (cdr args))
+ (pp (stdin-to-str) #t)
+ (pp (stdin-to-str) #f))))
+ (display result))
(newline))
diff --git a/password-philosophy/pp-test.scm b/password-philosophy/pp-test.scm
index 7641c4a..f1b9c64 100644
--- a/password-philosophy/pp-test.scm
+++ b/password-philosophy/pp-test.scm
@@ -22,19 +22,19 @@
(count-occurrences "zzf9ct5f" #\f))
(test-assert "Single X"
- (valid-password? "X" #\X '(1 . 1)))
+ (valid-password-p1? "X" #\X '(1 . 1)))
(test-assert "Single X extra"
- (valid-password? "the X marks the spot" #\X '(1 . 1)))
+ (valid-password-p1? "the X marks the spot" #\X '(1 . 1)))
(test-assert "Double X allowed"
- (valid-password? "X lots of bad things use the logo of just an X" #\X '(1 . 2)))
+ (valid-password-p1? "X lots of bad things use the logo of just an X" #\X '(1 . 2)))
(test-assert "Double X forced"
- (valid-password? "X lots of bad things use the logo of just an X" #\X '(2 . 2)))
+ (valid-password-p1? "X lots of bad things use the logo of just an X" #\X '(2 . 2)))
(test-assert "Double X failed"
- (not (valid-password? "lots of bad things use the logo of just an X" #\X '(2 . 2))))
+ (not (valid-password-p1? "lots of bad things use the logo of just an X" #\X '(2 . 2))))
(test-equal "Parse line 1"
'("abcde" #\a (1 . 3))
@@ -48,25 +48,40 @@
0
(pp "1-3 a: bcde
1-3 b: cdefg
-2-9 c: cccccccccc"))
+2-9 c: cccccccccc" #t))
(test-equal "Count valid 1"
1
(pp "1-3 a: abcde
1-3 b: cdefg
-2-9 c: cccccccccc"))
+2-9 c: cccccccccc" #t))
(test-equal "Count valid 2"
2
(pp "1-3 a: abcde
1-3 b: cdefg
-2-9 c: ccccccccc"))
+2-9 c: ccccccccc" #t))
(test-equal "Count valid 3"
3
(pp "1-3 a: abcde
1-3 b: cdbfg
-2-9 c: ccccccccc"))
+2-9 c: ccccccccc" #t))
+
+(test-assert "First char matched"
+ (valid-password-p2? ":)" #\) '(1 . 2)))
+
+(test-assert "Neither chars matched"
+ (not (valid-password-p2? ":(" #\) '(1 . 2))))
+
+(test-assert "Example 1"
+ (valid-password-p2? "abcde" #\a '(1 . 3)))
+
+(test-assert "Example 2"
+ (not (valid-password-p2? "cdefg" #\b '(1 . 3))))
+
+(test-assert "Example 3"
+ (not (valid-password-p2? "cccccccccc" #\a '(2 . 9))))
(test-end "harness")
diff --git a/password-philosophy/pp.scm b/password-philosophy/pp.scm
index 9431ffa..b1d92d7 100644
--- a/password-philosophy/pp.scm
+++ b/password-philosophy/pp.scm
@@ -3,15 +3,20 @@
#:export (pp
parse-line
count-occurrences
- valid-password?))
+ valid-password-p1?
+ valid-password-p2?))
-(define (pp str)
+(define (pp str p1?)
(count (lambda (x) x)
(map (lambda (line)
(let ((parsed (parse-line line)))
- (valid-password? (car parsed)
- (cadr parsed)
- (caddr parsed))))
+ (if p1?
+ (valid-password-p1? (car parsed)
+ (cadr parsed)
+ (caddr parsed))
+ (valid-password-p2? (car parsed)
+ (cadr parsed)
+ (caddr parsed)))))
(string-split
(string-trim-both str char-set:whitespace)
#\newline))))
@@ -29,7 +34,20 @@ into a workable datastructure:
(cons (car range)
(cadr range)))))
-(define (valid-password? passwd char range)
+(define (valid-password-p2? passwd char range)
+ "Wrapper for valid-password-p1?.
+Prepares the input by indexing RANGE
+into PASSWD, setting that as the new
+PASSWD, and setting RANGE to (1 . 1)
+
+Note the original problem specifies
+that strings are NOT zero-indexed!"
+ (valid-password-p1? (string (string-ref passwd (1- (car range)))
+ (string-ref passwd (1- (cdr range))))
+ char
+ '(1 . 1)))
+
+(define (valid-password-p1? passwd char range)
"Given PASSWD, checks that CHAR appears
within RANGE number of times. RANGE is a
cons pair."