aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/01/part2.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'advent-of-code/2023/01/part2.rkt')
-rw-r--r--advent-of-code/2023/01/part2.rkt36
1 files changed, 36 insertions, 0 deletions
diff --git a/advent-of-code/2023/01/part2.rkt b/advent-of-code/2023/01/part2.rkt
new file mode 100644
index 0000000..85602da
--- /dev/null
+++ b/advent-of-code/2023/01/part2.rkt
@@ -0,0 +1,36 @@
+#lang racket
+
+(define fp (open-input-file "input"))
+(define sum 0)
+
+(define (get-lines fp)
+ (let loop ((lines '()))
+ (define l (read-line fp))
+ (if (eof-object? l)
+ (reverse lines)
+ (loop (cons l lines)))))
+
+(define lines (get-lines fp))
+
+(define (replace-digit str)
+ (set! str (string-replace str "one" "o1ne"))
+ (set! str (string-replace str "two" "t2wo"))
+ (set! str (string-replace str "three" "t3hree"))
+ (set! str (string-replace str "four" "f4our"))
+ (set! str (string-replace str "five" "f5ive"))
+ (set! str (string-replace str "six" "s6ix"))
+ (set! str (string-replace str "seven" "s7even"))
+ (set! str (string-replace str "eight" "e8ight"))
+ (set! str (string-replace str "nine" "n9ine"))
+ str)
+
+(define (extract-number rawline)
+ (define line (replace-digit rawline))
+ (define number-list (filter char-numeric? (string->list line)))
+ (set! number-list (map (lambda (c)
+ (string->number (list->string (list c))))
+ number-list))
+ (+ (last number-list) (* 10 (car number-list))))
+
+(display (apply + (map extract-number lines)))
+(newline)