aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-other
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-02 00:10:57 -0500
committerHJ <thechairman@thechairman.info>2023-12-02 00:10:57 -0500
commite06c1b7ee377fbb09066bc05472757759a3b6435 (patch)
tree4c7cee6535b7c23f5c8191ac37567b2ea614f975 /aoc2023-other
parenta82877a093f43c11b298f2a59374b53595b40f53 (diff)
parent826d7698c84b42dadf6b1684c93828e3e2f5ee3a (diff)
downloadgleam_aoc-e06c1b7ee377fbb09066bc05472757759a3b6435.tar.gz
gleam_aoc-e06c1b7ee377fbb09066bc05472757759a3b6435.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2023-other')
-rw-r--r--aoc2023-other/day-01/day-01.rkt38
1 files changed, 38 insertions, 0 deletions
diff --git a/aoc2023-other/day-01/day-01.rkt b/aoc2023-other/day-01/day-01.rkt
new file mode 100644
index 0000000..b720f79
--- /dev/null
+++ b/aoc2023-other/day-01/day-01.rkt
@@ -0,0 +1,38 @@
+#lang racket
+
+(require advent-of-code
+ threading)
+
+(define calibration-values (fetch-aoc-input (find-session) 2023 1))
+
+(define (to-number str)
+ (match (string->number str)
+ [#false (hash-ref word-to-digit str)]
+ [n n]))
+
+(define (parse-calibration-value v valid)
+ (for/fold ([acc '()] [value v] #:result (+ (to-number (first acc)) (* 10 (to-number (last acc)))))
+ ([_ (in-naturals)])
+ #:break (equal? value "")
+ (let ([possible-prefix (findf (curry string-prefix? value) valid)])
+ (if possible-prefix
+ (values (cons possible-prefix acc) (substring value 1))
+ (values acc (substring value 1))))))
+
+(define (total-calibration input valid)
+ (~> input
+ (string-trim)
+ (string-split "\n")
+ (map (λ~> (parse-calibration-value valid)) _)
+ (apply + _)))
+
+;; part 1
+
+(define valid-for-part-1 (~> (range 1 10) (map ~a _)))
+(total-calibration calibration-values valid-for-part-1)
+
+;; part 2
+(define word-to-digit
+ (hash "one" 1 "two" 2 "three" 3 "four" 4 "five" 5 "six" 6 "seven" 7 "eight" 8 "nine" 9))
+(define valid-for-part-2 (append valid-for-part-1 (hash-keys word-to-digit)))
+(total-calibration calibration-values valid-for-part-2)