aboutsummaryrefslogtreecommitdiff
path: root/racket/aoc2021/day-05/day-05.rkt
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
committerH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
commit8777ff071f7bb37631baa7b6717ad29961e50911 (patch)
tree6d59c4ed58e454b960339c3d1151f0a879e8d7cb /racket/aoc2021/day-05/day-05.rkt
parent6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff)
downloadgleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz
gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip
sorting by language
Diffstat (limited to 'racket/aoc2021/day-05/day-05.rkt')
-rw-r--r--racket/aoc2021/day-05/day-05.rkt57
1 files changed, 57 insertions, 0 deletions
diff --git a/racket/aoc2021/day-05/day-05.rkt b/racket/aoc2021/day-05/day-05.rkt
new file mode 100644
index 0000000..e568490
--- /dev/null
+++ b/racket/aoc2021/day-05/day-05.rkt
@@ -0,0 +1,57 @@
+#lang racket
+(require advent-of-code
+ threading)
+
+(define data
+ (for/list ([l (in-lines (open-aoc-input (find-session) 2021 5 #:cache (string->path "./cache")))])
+ (~> l (string-replace " -> " ",") (string-split ",") (map string->number _))))
+
+(define (trace-line! x y vec)
+ (define linear-coord (+ y (* 1000 x)))
+ (vector-set! vec linear-coord (+ 1 (vector-ref vec linear-coord))))
+
+(define/match (orthogonal? coord-pair)
+ [((or (list n _ n _) (list _ n _ n))) #t]
+ [(_) #f])
+
+(define-values (orthogonal-lines diagonal-lines) (partition orthogonal? data))
+
+(define (dir a b)
+ (if (< a b) 1 -1))
+
+(define (trace-orthogonal! coord-pairs tracer result)
+ (for ([coord-pair (in-list coord-pairs)])
+ (match coord-pair
+ [(list x y1 x y2)
+ (for ([y (inclusive-range y1 y2 (dir y1 y2))])
+ (tracer x y result))]
+ [(list x1 y x2 y)
+ (for ([x (inclusive-range x1 x2 (dir x1 x2))])
+ (tracer x y result))])))
+
+(define (trace-diagonal! coord-pairs tracer result)
+ (for ([coord-pair (in-list coord-pairs)])
+ (match-define (list x1 y1 x2 y2) coord-pair)
+ (for ([x (inclusive-range x1 x2 (dir x1 x2))] [y (inclusive-range y1 y2 (dir y1 y2))])
+ (tracer x y result))))
+
+;; part 1
+(define sea-floor (make-vector 1000000))
+(trace-orthogonal! orthogonal-lines trace-line! sea-floor)
+(vector-count (curry <= 2) sea-floor)
+
+;; part 2
+;; since the orthogonal lines have already been traced,
+;; all I need to do is add the diagonal ones to the existing vector
+(trace-diagonal! diagonal-lines trace-line! sea-floor)
+(vector-count (curry <= 2) sea-floor)
+
+;; alternate sparse representation
+(define (trace-line-sparse! x y dict)
+ (hash-update! dict (list x y) (curry + 1) 0))
+
+(define sea-floor-dict (make-hash))
+(trace-orthogonal! orthogonal-lines trace-line-sparse! sea-floor-dict)
+(count (curry <= 2) (hash-values sea-floor-dict))
+(trace-diagonal! diagonal-lines trace-line-sparse! sea-floor-dict)
+(count (curry <= 2) (hash-values sea-floor-dict))