diff options
Diffstat (limited to 'advent-of-code')
| -rw-r--r-- | advent-of-code/2023/.gitignore | 1 | ||||
| -rw-r--r-- | advent-of-code/2023/09/1.rkt | 27 | ||||
| -rw-r--r-- | advent-of-code/2023/09/2.rkt | 26 |
3 files changed, 54 insertions, 0 deletions
diff --git a/advent-of-code/2023/.gitignore b/advent-of-code/2023/.gitignore new file mode 100644 index 0000000..770eab4 --- /dev/null +++ b/advent-of-code/2023/.gitignore @@ -0,0 +1 @@ +input
\ No newline at end of file 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)) + diff --git a/advent-of-code/2023/09/2.rkt b/advent-of-code/2023/09/2.rkt new file mode 100644 index 0000000..b747b97 --- /dev/null +++ b/advent-of-code/2023/09/2.rkt @@ -0,0 +1,26 @@ +#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) + (- (car lst) (predict (diff lst))))) + +(apply + (map predict data)) |
