aboutsummaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-03 23:54:22 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-03 23:54:22 -0500
commitbeb9bc443b3e206ea7a15be7be5f2be45317d9d7 (patch)
treefaf20bbb480397064a459d720c4c15577b66b3bf /2020
parent9c778129734990871179ca012364ff0637ba3a14 (diff)
downloadgleam_aoc-beb9bc443b3e206ea7a15be7be5f2be45317d9d7.tar.gz
gleam_aoc-beb9bc443b3e206ea7a15be7be5f2be45317d9d7.zip
2020 day 10 complete
Diffstat (limited to '2020')
-rw-r--r--2020/day-10/day-10.rkt37
1 files changed, 37 insertions, 0 deletions
diff --git a/2020/day-10/day-10.rkt b/2020/day-10/day-10.rkt
new file mode 100644
index 0000000..77d9bb7
--- /dev/null
+++ b/2020/day-10/day-10.rkt
@@ -0,0 +1,37 @@
+#lang racket
+
+(require advent-of-code
+ threading
+ algorithms
+ memoize)
+
+;; part 1
+(define adapters
+ (~> (fetch-aoc-input (find-session) 2020 10)
+ (string-split "\n")
+ (map string->number _)
+ (sort <)
+ ((λ (xs) (flatten (list 0 xs (+ 3 (last xs))))))))
+
+(~>> adapters
+ (sliding _ 2)
+ (map (match-lambda
+ [(list b a) (- a b)]))
+ (group-by identity)
+ (map length)
+ (apply *))
+
+;; part 2
+(define subpaths
+ (for*/hash ([adapter (in-list adapters)])
+ (define predecessor-candidates (inclusive-range (+ 1 adapter) (+ 3 adapter)))
+ (values adapter (filter (λ (p) (member p adapters)) predecessor-candidates))))
+
+(define/memo (find-paths from to)
+ (define paths (hash-ref subpaths from 'failed))
+ (match paths
+ ['failed 0]
+ [(list-no-order (== to) _ ...) 1]
+ [ts (for/sum ([t (in-list ts)]) (find-paths t to))]))
+
+(find-paths (first adapters) (last adapters))