summary refs log tree commit diff stats
path: root/day4.scm
diff options
context:
space:
mode:
authorbrian <brianmchu42@gmail.com>2024-08-03 13:33:43 -0700
committerbrian <brianmchu42@gmail.com>2024-08-03 13:33:43 -0700
commita88e8da779cd09c398723e4e87e79d945af0994e (patch)
tree0be5a89e050f088ebc166433c4ce251d43062bb4 /day4.scm
downloadAdventOfCode2019-main.tar.gz
initial commit to day 6 main
Diffstat (limited to 'day4.scm')
-rwxr-xr-xday4.scm35
1 files changed, 35 insertions, 0 deletions
diff --git a/day4.scm b/day4.scm
new file mode 100755
index 0000000..29eefb0
--- /dev/null
+++ b/day4.scm
@@ -0,0 +1,35 @@
+#!/usr/bin/env gosh
+(use scheme.set)
+
+(define max-num 746325)
+(define min-num 264360)
+(define num-range (iota (- max-num min-num -1) min-num))
+
+(define (digit-list number) (map digit->integer (string->list (number->string number))))
+
+(define (valid-pw? number)
+  (letrec ((num-list (digit-list number))
+           (sorted-num-list (sort num-list))
+           (num-set (list->set eq-comparator num-list)))
+    (and
+      (> (length num-list) (set-size num-set))
+      (equal? sorted-num-list num-list))))
+
+(define part1
+  (length (filter valid-pw? num-range)))
+
+(define (valid-count? number)
+  (letrec ((num-list (digit-list number))
+           (counts
+            (fold 
+              (lambda (element hashtable) (hash-table-update! hashtable element (cut + 1 <>) 0) hashtable)
+              (make-hash-table eq-comparator)
+              num-list)))
+    (any (lambda (x) (= x 2)) (hash-table-values counts))))
+
+(define part2
+  (length (filter (lambda (x) (and (valid-pw? x) (valid-count? x))) num-range)))
+
+(define (main args)
+  (print part1)
+  (print part2))