aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/09/1.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'advent-of-code/2023/09/1.rkt')
-rw-r--r--advent-of-code/2023/09/1.rkt27
1 files changed, 27 insertions, 0 deletions
diff --git a/advent-of-code/2023/09/1.rkt b/advent-of-code/2023/09/1.rkt
new file mode 100644
index 0000000..531c5dd
--- /dev/null
+++ b/advent-of-code/2023/09/1.rkt
@@ -0,0 +1,27 @@
+#lang racket
+
+(require "../../lib/utils.rkt")
+
+(define lines
+ (call-with-input-file "input"
+ (λ (fp)
+ (get-lines fp))))
+
+(define (parse-data line)
+ (map string->number (string-split line)))
+
+(define data (map parse-data lines))
+
+(define (diff lst)
+ (let loop ((cur lst) (ret '()))
+ (if (or (null? cur) (null? (cdr cur)))
+ (reverse ret)
+ (loop (cdr cur) (cons (- (cadr cur) (car cur)) ret)))))
+
+(define (predict lst)
+ (if (andmap (λ (x) (= x (car lst))) lst)
+ (car lst)
+ (+ (last lst) (predict (diff lst)))))
+
+(apply + (map predict data))
+