summaryrefslogtreecommitdiff
path: root/1-2.scm
diff options
context:
space:
mode:
Diffstat (limited to '1-2.scm')
-rwxr-xr-x1-2.scm41
1 files changed, 41 insertions, 0 deletions
diff --git a/1-2.scm b/1-2.scm
new file mode 100755
index 0000000..27bc004
--- /dev/null
+++ b/1-2.scm
@@ -0,0 +1,41 @@
+#!/usr/bin/guile -s
+!#
+(use-modules (ice-9 regex)
+ (ice-9 textual-ports)
+ (srfi srfi-1))
+
+(define numbers '(("one" . "1")
+ ("two" . "2")
+ ("three" . "3")
+ ("four" . "4")
+ ("five" . "5")
+ ("six" . "6")
+ ("seven" . "7")
+ ("eight" . "8")
+ ("nine" . "9")))
+
+(define revnumbers (map (lambda (x) (cons (string-reverse (car x)) (cdr x))) numbers))
+
+(define (read-lines port)
+ (letrec ((loop (lambda (l ls)
+ (if (eof-object? l)
+ ls
+ (loop (get-line port) (cons l ls))))))
+ (reverse (loop (get-line port) '()))))
+
+(define (find-digit dict line)
+ (let* ((regex (fold (lambda (x y) (string-append x "|" y)) "[0-9]" (map car dict)))
+ (matches (string-match regex line))
+ (digit (match:substring matches 0))
+ (as-num (string->number digit)))
+ (if as-num digit (cdr (assoc digit dict)))))
+
+(define (fix-line line)
+ (let ((first-digit (find-digit numbers line))
+ (last-digit (find-digit revnumbers (string-reverse line))))
+ (string->number (string-append first-digit last-digit))))
+
+(let* ((port (open-input-file "1.txt"))
+ (lines (read-lines port)))
+ (close-port port)
+ (display (fold + 0 (map fix-line lines))))