-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bryce Thuilot <bryce@thuilot.io>
- Loading branch information
Showing
8 changed files
with
273 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |