Skip to content

Commit

Permalink
feat(day 10): complete day 10 (#11)
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce Thuilot <bryce@thuilot.io>
  • Loading branch information
bthuilot authored Dec 11, 2024
1 parent 4e38239 commit bb6ad28
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Below is an index to every completed day's implementation source code (containin
- [Day 7](private/days/07.rkt) : [Problem](https://adventofcode.com/2024/day/7)
- [Day 8](private/days/08.rkt) : [Problem](https://adventofcode.com/2024/day/8)
- [Day 9](private/days/09.rkt) : [Problem](https://adventofcode.com/2024/day/9)
<!-- - [Day 10](private/days/10.rkt) : [Problem](https://adventofcode.com/2024/day/10) -->
- [Day 10](private/days/10.rkt) : [Problem](https://adventofcode.com/2024/day/10)
<!-- - [Day 11](private/days/11.rkt) : [Problem](https://adventofcode.com/2024/day/11) -->
<!-- - [Day 12](private/days/12.rkt) : [Problem](https://adventofcode.com/2024/day/12) -->
<!-- - [Day 13](private/days/13.rkt) : [Problem](https://adventofcode.com/2024/day/13) -->
Expand Down
50 changes: 50 additions & 0 deletions inputs/10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
14567892107654348943218769016567650154541210421036
03456783298993267654309458122168743243450344323145
12567654456780154327812367433059804012769455410234
03498012349876065016901056544965418765898766708943
12345101212145076545411034545878329658981055899854
09876876705034187632110123656789421047432765988765
67878965896123298901001656743078431236598894012034
50965014387654567650012349856127340012367653213125
41234321298347656543243492347833458903458743404987
30087430178298343650156781016942167812769252985676
21196567069121243761056432679851043212890101679854
33203498451080252852347841589765654301285234521763
14512432347890161943210950432106567610106501430012
01693501036543270856102167645656788943217432567897
32789672321015389987343078938765497654998549879898
45679987410234578101256560129812321067801456734787
03478756500187665432107452121901054328982340125676
12568767891098987013898943030810167017654321010210
21079458910127698123965436945107878988901267124378
30980349821034787654876327876716901210985458095469
45671210136765693454761016329825432345671329186954
12789800345876548763876125419434501654510413277843
03543211238989439012985630308765898746701204567832
14623400141232323101234521678906567239874343236901
25710519850541014143219834567611452108965650145690
76897678769650001054301712106320143210345789036781
87678989678742112363212601235431234321276988325432
90549876349233678478004592347842389123489676710876
21632305256104569589123487656965476016512369856945
52301014107012345670149874565456365017603450747832
65490123458912396501234563432147454328214921632401
86985432167905487654341012563038901039309834521321
97876789001856778761232127678127612398712701100410
89810678012760869890103238999210543125625632234509
76701549013451987217876434785695610034534548765678
05432432174012987301987325654780123435210159854789
12980120985123673458986510783279234987346543123898
43878921976034562567603412892168765679857012010187
34565437852178901070412103601001410012768001921236
45430566543065012181543014580432321003459122876545
50121098767654327892678877698569457654219433468904
23292145678954218983019988087658768894308596567812
14587239010563007654128679112565489765107687656521
05674678323472167659436543203474321087230156785430
96983565401089898748540987654589321098543243896543
87874328992396701037621296562105465407698012565432
78765017687478632128760345673456978312789801478521
29653078596569543019656921087567889213456700329650
12532169430430156010567892193610367804765410418789
03445678321321060123456543012323458912894321001678
5 changes: 1 addition & 4 deletions private/days/09.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ in my head, so a refactor is definitely needed for it to be readable.
(require racket/list)
(require racket/set)
(require "../solution.rkt")
(require "../utils/strings.rkt")

(provide run)

Expand All @@ -50,12 +51,8 @@ in my head, so a refactor is definitely needed for it to be readable.
;; and #f represents free space


(define (char->number c)
(- (char->integer c) 48))

(define (parse-disk-map input)
(map char->number (string->list input)))


;; DiskMap -> Disk
;; parses a disk map into the actual contents of the disk
Expand Down
159 changes: 159 additions & 0 deletions private/days/10.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#|
Day 10
https://adventofcode.com/2024/day/10
I really thought a graph problem was gonna be
hard but I got this one a lot quicker than I realized.
First did each part seperately but eventually
settled on one function that computes the 'Point's
for '9' tiles that can be reached from a given point.
points are repeated if they occur from a different path.
I then fold over the whole map with that function
and each result for a 0 tile I add to a list.
If for each 0 value you compute the amount of unique
points in each list, that is the 'score'.
If for each 0 value you get the amount of points in each
list, thats the 'rating'
|#

#lang racket/base

(require racket/function)
(require racket/string)
(require racket/set)
(require racket/list)
(require "../utils/matrix.rkt")
(require "../utils/strings.rkt")
(require "../utils/point.rkt")
(require "../solution.rkt")

(provide run)

;; TopographicMap = [Matrixof Integer]

;; String -> TopographicMap
;; Parses the topographic map from the
;; given string
(define (parse-topographic-map input)
(define lines (string-split input "\n"))
(map (λ (l) (map char->number (string->list l))) lines))

;; TrailTails = [Listof Point]
;; represents the points for the ends
;; of the trails reached, the same point
;; visited through a different path will
;; contain duplicated values

;; Cache = [Hashof Point -> TrailTails]
;; cache records the computed results of TrailTails

;; TopographicMap Cache Number Point -> (cons Cache TrailTails)
;; returns the TrailTails for a given point with the given height
;; cache is used to store the TrailTails for other points as to not
;; re-compute previously explored
(define (trailtails t-map cache height point)
; fold-next will find the TrailTrails for all the next points
; and combine their output into a single TrailTrails
(define (fold-next cur-height next-point acc)
(define next-height (matrix-point t-map next-point))
(define c (car acc)) ; cache
(define tails (cdr acc))
(cond
[(not (= next-height (add1 cur-height))) (cons c tails)]
[else
(define-values (tm ta) (trailtails t-map c next-height next-point))
(cons tm (append ta tails))]))

(cond
; if were at the top, return the current point
[(= height 9) (values cache (list point))]
; return previous result if possible
[(hash-has-key? cache point) (values cache (hash-ref cache point))]
[else
; get all the endpoints of the next points,
; and combine them
(define c-points (cardinal-points point))
(define next-points (filter ((curry in-bounds?) t-map) c-points))
(define acc (foldl ((curry fold-next) height)
(cons cache '())
next-points))
(define tails (cdr acc))
(define final-cache (hash-set (car acc) point tails))
(values final-cache tails)]))


;; TopographicMap -> [Listof TrailTails]
;; computes the TrailTrails for every point
;; and then returns a list of the TrailsTails
;; for all points that have height 0
(define (calculate-trailhead-tails t-map)
(define (fold point height acc)
(define cache (car acc))
(define all-tails (cdr acc))
(define-values (update-cache tails) (trailtails t-map cache height point))
(cons update-cache
(if (= height 0) (cons tails all-tails) all-tails)))
(cdr (matrix-fold-point fold (cons (hash) '()) t-map)))

;; TrailTrails Number -> Number
(define (sum-score tails total)
(+ (set-count (list->set tails)) total))

;; TrailTrails Number -> Number
(define (sum-ratings tails total)
(+ (length tails) total))

;; TrailsTails -> Number
(define (part1 tails)
(foldl sum-score 0 tails))

;; TrailsTails -> Number
(define (part2 tails)
(foldl sum-ratings 0 tails))

(define (run input)
(define t-map (parse-topographic-map input))
(define tails (calculate-trailhead-tails t-map))
(full-solution (part1 tails) (part2 tails)))

(module+ test
(require rackunit)

(define example-input #<<EOF
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
EOF
)


(define t-map (parse-topographic-map example-input))
(check-equal? t-map
'((8 9 0 1 0 1 2 3)
(7 8 1 2 1 8 7 4)
(8 7 4 3 0 9 6 5)
(9 6 5 4 9 8 7 4)
(4 5 6 7 8 9 0 3)
(3 2 0 1 9 0 1 2)
(0 1 3 2 9 8 0 1)
(1 0 4 5 6 7 3 2)))

(define tails (calculate-trailhead-tails t-map))

(check-equal? (part1 tails)
36)
(check-equal? (part2 tails)
81)

)
3 changes: 1 addition & 2 deletions private/utils/lists.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
[else
(define applied (foldl (fold (car ls)) '() (cdr ls)))
(helper f (cdr ls) (append applied acc))]))
(helper func l '()))

(helper func l '()))
38 changes: 37 additions & 1 deletion private/utils/matrix.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#lang racket/base

(require racket/list)
(require "point.rkt")

(provide
Expand All @@ -11,6 +12,12 @@
in-bounds?
;; Point Number Number (Number)? (Number)? -> Boolean
point-within?

;; [<A> <T> -> <A>] <A> [Matrixof <T>] -> <A>
matrix-fold

;; [Point <A> <T> -> <A>] <A> [Matrixof <T>] -> <A>
matrix-fold-point
)

;; Matrixof<T> = [Listof [Listof T]]
Expand Down Expand Up @@ -49,4 +56,33 @@
(and (>= (point-x p) min-x)
(>= (point-y p) min-y)
(< (point-x p) max-x)
(< (point-y p) max-y)))
(< (point-y p) max-y)))

;; [<A> <T> -> <A>] <A> [Matrixof <T>] -> <A>
;; folds over all values in a matrix.
(define (matrix-fold f acc matrix)
(define (fold-row row acc)
(foldl f acc row))
(define (fold-rows rows acc)
(foldl fold-row rows))
(foldl fold-rows acc matrix))


;; [Point <A> <T> -> <A>] <A> [Matrixof <T>] -> <A>
;; folds over all values in a matrix.
;; Same as matrix-fold but additionally calls
;; with a Point representing the coordinate in the matrix
(define (matrix-fold-point f acc matrix)
(define (fold-row rs p acc)
(define next-point (point (add1 (point-x p)) (point-y p)))
(define (fold i) (f p i acc))
(cond
[(empty? rs) acc]
[else (fold-row (cdr rs) next-point (fold (car rs)))]))
(define (fold-rows m p acc)
(define next-point (point (point-x p) (add1 (point-y p))))
(define (fold i) (fold-row i p acc))
(cond
[(empty? m) acc]
[else (fold-rows (cdr m) next-point (fold (car m)))]))
(fold-rows matrix (point 0 0) acc))
11 changes: 10 additions & 1 deletion private/utils/point.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

;; Point Slope -> Point
translate-slope

;; Point -> [Listof Point]
cardinal-points
)

;; a point represents an x y coordinate
Expand All @@ -45,4 +48,10 @@

(define (negate-slope s)
(slope (* -1 (slope-dx s))
(* -1 (slope-dy s))))
(* -1 (slope-dy s))))

(define (cardinal-points p)
(list (translate p 1 0)
(translate p 0 1)
(translate p -1 0)
(translate p 0 -1)))
14 changes: 14 additions & 0 deletions private/utils/strings.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#lang racket/base

(provide
;; Character -> Number
char->number
)

(require racket/string)

;; Character -> Number
;; Parses a character into
;; a Number
(define (char->number c)
(- (char->integer c) 48))

0 comments on commit bb6ad28

Please sign in to comment.