aboutsummaryrefslogtreecommitdiff
path: root/racket/aoc2019
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/aoc2019
parent6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff)
downloadgleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz
gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip
sorting by language
Diffstat (limited to 'racket/aoc2019')
-rw-r--r--racket/aoc2019/day-02/day-02.rkt32
-rw-r--r--racket/aoc2019/day-03/day-03.rkt52
-rw-r--r--racket/aoc2019/day-04/day-04.rkt39
-rw-r--r--racket/aoc2019/day-05/day-05.rkt1
4 files changed, 124 insertions, 0 deletions
diff --git a/racket/aoc2019/day-02/day-02.rkt b/racket/aoc2019/day-02/day-02.rkt
new file mode 100644
index 0000000..56019e8
--- /dev/null
+++ b/racket/aoc2019/day-02/day-02.rkt
@@ -0,0 +1,32 @@
+#lang racket
+
+(require advent-of-code
+ threading)
+
+(define initial-opcodes
+ (parameterize ([current-readtable (make-readtable #f #\, #\space #f)])
+ (port->list read (open-aoc-input (find-session) 2019 2 #:cache #true))))
+
+;; part 1
+(define (edit-opcode ocs oc-1 oc-2)
+ (~> ocs (list-set 1 oc-1) (list-set 2 oc-2)))
+
+(define (run-intcode ocs)
+ (for/fold ([ocs ocs] #:result (car ocs))
+ ([pointer (in-range 0 (length initial-opcodes) 4)] #:break (= 99 (list-ref ocs pointer)))
+ (define op
+ (match (list-ref ocs pointer)
+ [1 +]
+ [2 *]))
+ (list-set ocs
+ (list-ref ocs (+ 3 pointer))
+ (op (list-ref ocs (list-ref ocs (+ 1 pointer)))
+ (list-ref ocs (list-ref ocs (+ 2 pointer)))))))
+
+(~> initial-opcodes (edit-opcode 12 2) run-intcode)
+
+;; part 2
+(for*/first ([noun (inclusive-range 0 99)]
+ [verb (inclusive-range 0 99)]
+ #:when (~> initial-opcodes (edit-opcode noun verb) run-intcode (= 19690720)))
+ (+ (* 100 noun) verb))
diff --git a/racket/aoc2019/day-03/day-03.rkt b/racket/aoc2019/day-03/day-03.rkt
new file mode 100644
index 0000000..6da3a07
--- /dev/null
+++ b/racket/aoc2019/day-03/day-03.rkt
@@ -0,0 +1,52 @@
+#lang racket
+
+(require advent-of-code
+ threading
+ fancy-app
+ racket/hash)
+
+(define/match (instruction-parse _str)
+ [((regexp #px"(.)(.+)" (list _ (app string->symbol dir) (app string->number amt)))) (cons dir amt)])
+
+(define (wire-parse str)
+ (~> str (string-split ",") (map instruction-parse _)))
+
+(define wires
+ (~>> (fetch-aoc-input (find-session) 2019 3 #:cache #true) string-split (map wire-parse)))
+
+(define (manhattan-distance-from-origin p)
+ (+ (abs (car p)) (abs (cdr p))))
+
+(define (trace-wire-path wire)
+ (for/fold ([path (hash)] [x 0] [y 0] [len 0] #:result path) ([inst (in-list wire)])
+ (define-values (x* y*)
+ (match inst
+ [(cons 'U dy) (values x (+ y dy))]
+ [(cons 'D dy) (values x (- y dy))]
+ [(cons 'R dx) (values (+ x dx) y)]
+ [(cons 'L dx) (values (- x dx) y)]))
+ (define next-segment
+ (for*/list ([new-x (inclusive-range x x* (if (< x x*) 1 -1))]
+ [new-y (inclusive-range y y* (if (< y y*) 1 -1))])
+ (cons new-x new-y)))
+ (define numbered-segments
+ (for/hash ([segment (in-list next-segment)] [new-len (in-naturals len)])
+ (values segment new-len)))
+ (values (hash-union path numbered-segments #:combine (λ (v0 _) v0)) x* y* (+ len (cdr inst)))))
+
+;; part 1
+(define wire-paths (map trace-wire-path wires))
+
+(define intersections
+ (~>> wire-paths
+ (map (λ~> hash-keys list->set))
+ (apply set-intersect)
+ (set-remove _ '(0 . 0))
+ set->list))
+
+(~>> intersections (map manhattan-distance-from-origin) (apply min))
+
+;; part 2
+(~>> (for/list ([intersection (in-list intersections)])
+ (~>> wire-paths (map (hash-ref _ intersection)) (apply +)))
+ (apply min))
diff --git a/racket/aoc2019/day-04/day-04.rkt b/racket/aoc2019/day-04/day-04.rkt
new file mode 100644
index 0000000..9518779
--- /dev/null
+++ b/racket/aoc2019/day-04/day-04.rkt
@@ -0,0 +1,39 @@
+#lang racket
+
+(define (number->digits n [acc '()])
+ (cond
+ [(< n 10) (cons n acc)]
+ [else (number->digits (quotient n 10) (cons (remainder n 10) acc))]))
+
+(define (always-increasing? xs)
+ (match xs
+ [(list _) #t]
+ [(list* a b _) #:when (<= a b) (always-increasing? (cdr xs))]
+ [_ #f]))
+
+(define (adjacent-pair? xs)
+ (match xs
+ [(list _) #f]
+ [(list* a a _) a]
+ [_ (adjacent-pair? (cdr xs))]))
+
+;; part 1
+(for/sum ([password (inclusive-range 125730 579381)]
+ #:do [(define digits (number->digits password))]
+ #:when ((conjoin always-increasing? adjacent-pair?) digits))
+ 1)
+
+;; part 2
+(define (not-in-adjacent-triplet? xs)
+ (match xs
+ [(list a a c _ _ _) #:when (not (= a c)) #t]
+ [(list b a a c _ _) #:when (not (or (= a b) (= a c))) #t]
+ [(list _ b a a c _) #:when (not (or (= a b) (= a c))) #t]
+ [(list _ _ b a a c) #:when (not (or (= a b) (= a c))) #t]
+ [(list _ _ _ b a a) #:when (not (= a b)) #t]
+ [_ #f]))
+
+(for/sum ([password (inclusive-range 125730 579381)]
+ #:do [(define digits (number->digits password))]
+ #:when ((conjoin always-increasing? adjacent-pair? not-in-adjacent-triplet?) digits))
+ 1) \ No newline at end of file
diff --git a/racket/aoc2019/day-05/day-05.rkt b/racket/aoc2019/day-05/day-05.rkt
new file mode 100644
index 0000000..6f1f7b4
--- /dev/null
+++ b/racket/aoc2019/day-05/day-05.rkt
@@ -0,0 +1 @@
+#lang racket