Browse Source

λ looks cool & format code

Mistivia 1 year ago
parent
commit
bebb8946d2

+ 1 - 1
advent-of-code/2022/01/1.rkt

@@ -4,7 +4,7 @@
 
 
 (define lines
 (define lines
   (call-with-input-file "input"
   (call-with-input-file "input"
-    (lambda (fp)
+    (λ (fp)
       (get-lines fp))))
       (get-lines fp))))
 
 
 (define elves (split-list-by "" lines))
 (define elves (split-list-by "" lines))

+ 1 - 1
advent-of-code/2022/01/2.rkt

@@ -4,7 +4,7 @@
 
 
 (define lines
 (define lines
   (call-with-input-file "input"
   (call-with-input-file "input"
-    (lambda (fp)
+    (λ (fp)
       (get-lines fp))))
       (get-lines fp))))
 
 
 (define elves (split-list-by "" lines))
 (define elves (split-list-by "" lines))

+ 17 - 17
advent-of-code/2022/02/1.rkt

@@ -3,26 +3,26 @@
 (require "../../lib/utils.rkt")
 (require "../../lib/utils.rkt")
 
 
 (define lines
 (define lines
-    (call-with-input-file "input"
-        (lambda (fp) (get-lines fp))))
+  (call-with-input-file "input"
+     (fp) (get-lines fp))))
 
 
 (define games (map string-split lines))
 (define games (map string-split lines))
 
 
 (define (score game)
 (define (score game)
-    (define (base-score mine)
-        (cond ((string=? "X" mine) 1)
-              ((string=? "Y" mine) 2)
-              ((string=? "Z" mine) 3)))
-    (define (win-score mine oppo)
-        (cond ((string=? "X" mine) (cond ((string=? "A" oppo) 3)
-                                         ((string=? "B" oppo) 0)
-                                         ((string=? "C" oppo) 6)))
-              ((string=? "Y" mine) (cond ((string=? "A" oppo) 6)
-                                         ((string=? "B" oppo) 3)
-                                         ((string=? "C" oppo) 0)))
-              ((string=? "Z" mine) (cond ((string=? "A" oppo) 0)
-                                         ((string=? "B" oppo) 6)
-                                         ((string=? "C" oppo) 3)))))
-    (+ (base-score (cadr game)) (win-score (cadr game) (car game))))
+  (define (base-score mine)
+    (cond ((string=? "X" mine) 1)
+          ((string=? "Y" mine) 2)
+          ((string=? "Z" mine) 3)))
+  (define (win-score mine oppo)
+    (cond ((string=? "X" mine) (cond ((string=? "A" oppo) 3)
+                                     ((string=? "B" oppo) 0)
+                                     ((string=? "C" oppo) 6)))
+          ((string=? "Y" mine) (cond ((string=? "A" oppo) 6)
+                                     ((string=? "B" oppo) 3)
+                                     ((string=? "C" oppo) 0)))
+          ((string=? "Z" mine) (cond ((string=? "A" oppo) 0)
+                                     ((string=? "B" oppo) 6)
+                                     ((string=? "C" oppo) 3)))))
+  (+ (base-score (cadr game)) (win-score (cadr game) (car game))))
 
 
 (apply + (map score games))
 (apply + (map score games))

+ 30 - 30
advent-of-code/2022/02/2.rkt

@@ -3,43 +3,43 @@
 (require "../../lib/utils.rkt")
 (require "../../lib/utils.rkt")
 
 
 (define lines
 (define lines
-    (call-with-input-file "input"
-        (lambda (fp) (get-lines fp))))
+  (call-with-input-file "input"
+     (fp) (get-lines fp))))
 
 
 (define games (map string-split lines))
 (define games (map string-split lines))
 
 
 (define (transform-cheat mine oppo)
 (define (transform-cheat mine oppo)
-    (cond ((string=? "X" mine) (cond ((string=? "A" oppo) "Z")
-                                     ((string=? "B" oppo) "X")
-                                     ((string=? "C" oppo) "Y")))
-          ((string=? "Y" mine) (cond ((string=? "A" oppo) "X")
-                                     ((string=? "B" oppo) "Y")
-                                     ((string=? "C" oppo) "Z")))
-          ((string=? "Z" mine) (cond ((string=? "A" oppo) "Y")
-                                     ((string=? "B" oppo) "Z")
-                                     ((string=? "C" oppo) "X")))))
+  (cond ((string=? "X" mine) (cond ((string=? "A" oppo) "Z")
+                                   ((string=? "B" oppo) "X")
+                                   ((string=? "C" oppo) "Y")))
+        ((string=? "Y" mine) (cond ((string=? "A" oppo) "X")
+                                   ((string=? "B" oppo) "Y")
+                                   ((string=? "C" oppo) "Z")))
+        ((string=? "Z" mine) (cond ((string=? "A" oppo) "Y")
+                                   ((string=? "B" oppo) "Z")
+                                   ((string=? "C" oppo) "X")))))
 
 
 (set! games
 (set! games
-    (map
-        (lambda (x)
-            (list (car x) (transform-cheat (cadr x) (car x))))
-        games))
+      (map
+        (x)
+         (list (car x) (transform-cheat (cadr x) (car x))))
+       games))
 
 
 (define (score game)
 (define (score game)
-    (define (base-score mine)
-        (cond ((string=? "X" mine) 1)
-              ((string=? "Y" mine) 2)
-              ((string=? "Z" mine) 3)))
-    (define (win-score mine oppo)
-        (cond ((string=? "X" mine) (cond ((string=? "A" oppo) 3)
-                                         ((string=? "B" oppo) 0)
-                                         ((string=? "C" oppo) 6)))
-              ((string=? "Y" mine) (cond ((string=? "A" oppo) 6)
-                                         ((string=? "B" oppo) 3)
-                                         ((string=? "C" oppo) 0)))
-              ((string=? "Z" mine) (cond ((string=? "A" oppo) 0)
-                                         ((string=? "B" oppo) 6)
-                                         ((string=? "C" oppo) 3)))))
-    (+ (base-score (cadr game)) (win-score (cadr game) (car game))))
+  (define (base-score mine)
+    (cond ((string=? "X" mine) 1)
+          ((string=? "Y" mine) 2)
+          ((string=? "Z" mine) 3)))
+  (define (win-score mine oppo)
+    (cond ((string=? "X" mine) (cond ((string=? "A" oppo) 3)
+                                     ((string=? "B" oppo) 0)
+                                     ((string=? "C" oppo) 6)))
+          ((string=? "Y" mine) (cond ((string=? "A" oppo) 6)
+                                     ((string=? "B" oppo) 3)
+                                     ((string=? "C" oppo) 0)))
+          ((string=? "Z" mine) (cond ((string=? "A" oppo) 0)
+                                     ((string=? "B" oppo) 6)
+                                     ((string=? "C" oppo) 3)))))
+  (+ (base-score (cadr game)) (win-score (cadr game) (car game))))
 
 
 (apply + (map score games))
 (apply + (map score games))

+ 1 - 1
advent-of-code/2023/01/1.rkt

@@ -8,7 +8,7 @@
 
 
 (define (extract-number line)
 (define (extract-number line)
   (define number-list (filter char-numeric? (string->list line)))
   (define number-list (filter char-numeric? (string->list line)))
-  (set! number-list (map (lambda (c)
+  (set! number-list (map (λ (c)
                            (string->number (list->string (list c))))
                            (string->number (list->string (list c))))
                          number-list))
                          number-list))
   (+ (last number-list) (* 10 (car number-list))))
   (+ (last number-list) (* 10 (car number-list))))

+ 1 - 1
advent-of-code/2023/01/2.rkt

@@ -21,7 +21,7 @@
 (define (extract-number rawline)
 (define (extract-number rawline)
   (define line (replace-digit rawline))
   (define line (replace-digit rawline))
   (define number-list (filter char-numeric? (string->list line)))
   (define number-list (filter char-numeric? (string->list line)))
-  (set! number-list (map (lambda (c)
+  (set! number-list (map (λ (c)
                            (string->number (list->string (list c))))
                            (string->number (list->string (list c))))
                          number-list))
                          number-list))
   (+ (last number-list) (* 10 (car number-list))))
   (+ (last number-list) (* 10 (car number-list))))

+ 22 - 22
advent-of-code/2023/02/1.rkt

@@ -18,35 +18,35 @@
   (define b 0)
   (define b 0)
   (define balls (string-split str ","))
   (define balls (string-split str ","))
   (set! balls
   (set! balls
-    (map (lambda (s)
-           (define pair (map string-trim (string-split s " ")))
-           (list (string->number (car pair)) (cadr pair)))
-         balls))
+        (map (λ (s)
+               (define pair (map string-trim (string-split s " ")))
+               (list (string->number (car pair)) (cadr pair)))
+             balls))
   (let loop ((balls balls))
   (let loop ((balls balls))
     (if (null? balls)
     (if (null? balls)
-      '()
-      (let ()
-        (define ball (car balls))
-        (define number (car ball))
-        (define color (cadr ball))
-        (cond ((string=? "red" color) (set! r number))
-              ((string=? "green" color) (set! g number))
-              ((string=? "blue" color) (set! b number)))
-        (loop (cdr balls)))))
+        '()
+        (let ()
+          (define ball (car balls))
+          (define number (car ball))
+          (define color (cadr ball))
+          (cond ((string=? "red" color) (set! r number))
+                ((string=? "green" color) (set! g number))
+                ((string=? "blue" color) (set! b number)))
+          (loop (cdr balls)))))
   (list r g b))
   (list r g b))
 
 
 (define (possible? game)
 (define (possible? game)
   (if (null? game)
   (if (null? game)
-    #t
-    (let ()
-      (define head (car game))
-      (if (or (> (car head) 12)    ; r
-              (> (cadr head) 13)   ; g
-              (> (caddr head) 14)) ; b
-        #f
-        (possible? (cdr game))))))
+      #t
+      (let ()
+        (define head (car game))
+        (if (or (> (car head) 12)    ; r
+                (> (cadr head) 13)   ; g
+                (> (caddr head) 14)) ; b
+            #f
+            (possible? (cdr game))))))
 
 
 (apply + (map cadr
 (apply + (map cadr
-              (filter (lambda (game)
+              (filter (λ (game)
                         (possible? (car game)))
                         (possible? (car game)))
                       (enumerate (map extract-game lines)))))
                       (enumerate (map extract-game lines)))))

+ 13 - 13
advent-of-code/2023/02/2.rkt

@@ -18,21 +18,21 @@
   (define b 0)
   (define b 0)
   (define balls (string-split str ","))
   (define balls (string-split str ","))
   (set! balls
   (set! balls
-    (map (lambda (s)
-           (define pair (map string-trim (string-split s " ")))
-           (list (string->number (car pair)) (cadr pair)))
-         balls))
+        (map (λ (s)
+               (define pair (map string-trim (string-split s " ")))
+               (list (string->number (car pair)) (cadr pair)))
+             balls))
   (let loop ((balls balls))
   (let loop ((balls balls))
     (if (null? balls)
     (if (null? balls)
-      '()
-      (let ()
-        (define ball (car balls))
-        (define number (car ball))
-        (define color (cadr ball))
-        (cond ((string=? "red" color) (set! r number))
-              ((string=? "green" color) (set! g number))
-              ((string=? "blue" color) (set! b number)))
-        (loop (cdr balls)))))
+        '()
+        (let ()
+          (define ball (car balls))
+          (define number (car ball))
+          (define color (cadr ball))
+          (cond ((string=? "red" color) (set! r number))
+                ((string=? "green" color) (set! g number))
+                ((string=? "blue" color) (set! b number)))
+          (loop (cdr balls)))))
   (list r g b))
   (list r g b))
 
 
 (define (power game)
 (define (power game)

+ 37 - 37
advent-of-code/2023/03/1.rkt

@@ -5,7 +5,7 @@
 
 
 (define (read-input)
 (define (read-input)
   (call-with-input-file "input"
   (call-with-input-file "input"
-    (lambda (fp) 
+    (λ (fp)
       (list->vector (get-lines fp)))))
       (list->vector (get-lines fp)))))
 
 
 (define schema (read-input))
 (define schema (read-input))
@@ -22,29 +22,29 @@
   (define nums '())
   (define nums '())
   (let loop ((i 0))
   (let loop ((i 0))
     (if (>= i height)
     (if (>= i height)
-      (void)
-      (let ()
-        (let loop ((j 0))
+        (void)
+        (let ()
+          (let loop ((j 0))
             (define curline (vector-ref schema i))
             (define curline (vector-ref schema i))
             (if (>= j width)
             (if (>= j width)
-            (void)
-            (let ()
-                (define next 1)
-                (define (find-next)
-                  (if (or (>= (+ j next) 140)
-                          (not (char-numeric? (char-at i (+ j next)))))
-                    (void)
-                    (let ()
-                      (set! next (+ 1 next))
-                      (find-next))))
-                (if (char-numeric? (char-at i j))
-                  (let ()
-                    (find-next)
-                    (define value (string->number (substring curline j (+ j next))))
-                    (set! nums (cons (make-num i j value next) nums)))
-                  (void))
-                (loop (+ j next)))))
-        (loop (+ 1 i)))))
+                (void)
+                (let ()
+                  (define next 1)
+                  (define (find-next)
+                    (if (or (>= (+ j next) 140)
+                            (not (char-numeric? (char-at i (+ j next)))))
+                        (void)
+                        (let ()
+                          (set! next (+ 1 next))
+                          (find-next))))
+                  (if (char-numeric? (char-at i j))
+                      (let ()
+                        (find-next)
+                        (define value (string->number (substring curline j (+ j next))))
+                        (set! nums (cons (make-num i j value next) nums)))
+                      (void))
+                  (loop (+ j next)))))
+          (loop (+ 1 i)))))
   (reverse nums))
   (reverse nums))
 (define nums (scan-nums))
 (define nums (scan-nums))
 
 
@@ -55,30 +55,30 @@
 (define (collect-adjacent num)
 (define (collect-adjacent num)
   (define left
   (define left
     (if (= 0 (num 'col))
     (if (= 0 (num 'col))
-      '()
-      (list (char-at (num 'line) (- (num 'col) 1)))))
+        '()
+        (list (char-at (num 'line) (- (num 'col) 1)))))
   (define right
   (define right
     (if (= width (+ (num 'col) (num 'length)))
     (if (= width (+ (num 'col) (num 'length)))
-      '()
-      (list (char-at (num 'line) (+ (num 'col) (num 'length))))))
+        '()
+        (list (char-at (num 'line) (+ (num 'col) (num 'length))))))
   (define up
   (define up
     (if (= 0 (num 'line))
     (if (= 0 (num 'line))
-      '()
-      (string->list
-        (substring (vector-ref schema (- (num 'line) 1))
-                   (max 0 (- (num 'col) 1))
-                   (min width (+ (num 'col) (num 'length) 1))))))
+        '()
+        (string->list
+         (substring (vector-ref schema (- (num 'line) 1))
+                    (max 0 (- (num 'col) 1))
+                    (min width (+ (num 'col) (num 'length) 1))))))
   (define down
   (define down
     (if (= (- height 1) (num 'line))
     (if (= (- height 1) (num 'line))
-      '()
-      (string->list
-        (substring (vector-ref schema (+ (num 'line) 1))
-                   (max 0 (- (num 'col) 1))
-                   (min width (+ (num 'col) (num 'length) 1))))))
+        '()
+        (string->list
+         (substring (vector-ref schema (+ (num 'line) 1))
+                    (max 0 (- (num 'col) 1))
+                    (min width (+ (num 'col) (num 'length) 1))))))
   (append left right up down))
   (append left right up down))
 
 
 (define (is-part-num? num)
 (define (is-part-num? num)
   (findf is-symbol? (collect-adjacent num)))
   (findf is-symbol? (collect-adjacent num)))
 
 
-(apply + (map (lambda (x) (x 'value))
+(apply + (map (λ (x) (x 'value))
               (filter is-part-num? nums)))
               (filter is-part-num? nums)))

+ 39 - 43
advent-of-code/2023/03/2.rkt

@@ -5,7 +5,7 @@
 
 
 (define (read-input)
 (define (read-input)
   (call-with-input-file "input"
   (call-with-input-file "input"
-    (lambda (fp) 
+    (λ (fp)
       (list->vector (get-lines fp)))))
       (list->vector (get-lines fp)))))
 
 
 (define schema (read-input))
 (define schema (read-input))
@@ -22,60 +22,56 @@
   (define nums '())
   (define nums '())
   (let loop ((i 0))
   (let loop ((i 0))
     (if (>= i height)
     (if (>= i height)
-      (void)
-      (let ()
-        (let loop ((j 0))
+        (void)
+        (let ()
+          (let loop ((j 0))
             (define curline (vector-ref schema i))
             (define curline (vector-ref schema i))
             (if (>= j width)
             (if (>= j width)
-            (void)
-            (let ()
-                (define next 1)
-                (define (find-next)
-                  (if (or (>= (+ j next) 140)
-                          (not (char-numeric? (char-at i (+ j next)))))
-                    (void)
-                    (let ()
-                      (set! next (+ 1 next))
-                      (find-next))))
-                (if (char-numeric? (char-at i j))
-                  (let ()
-                    (find-next)
-                    (define value (string->number (substring curline j (+ j next))))
-                    (set! nums (cons (make-num i j value next) nums)))
-                  (void))
-                (loop (+ j next)))))
-        (loop (+ 1 i)))))
+                (void)
+                (let ()
+                  (define next 1)
+                  (define (find-next)
+                    (if (or (>= (+ j next) 140)
+                            (not (char-numeric? (char-at i (+ j next)))))
+                        (void)
+                        (let ()
+                          (set! next (+ 1 next))
+                          (find-next))))
+                  (if (char-numeric? (char-at i j))
+                      (let ()
+                        (find-next)
+                        (define value (string->number (substring curline j (+ j next))))
+                        (set! nums (cons (make-num i j value next) nums)))
+                      (void))
+                  (loop (+ j next)))))
+          (loop (+ 1 i)))))
   (reverse nums))
   (reverse nums))
 (define nums (scan-nums))
 (define nums (scan-nums))
 
 
-(define (is-symbol? c)
-  (and (not (char-numeric? c))
-       (not (char=? #\. c))))
-
 (define (collect-adjacent-positions num)
 (define (collect-adjacent-positions num)
   (define (position-range line start end)
   (define (position-range line start end)
     (define delta (- end start))
     (define delta (- end start))
     (map list (repeat delta line) (range start end)))
     (map list (repeat delta line) (range start end)))
   (define left
   (define left
     (if (= 0 (num 'col))
     (if (= 0 (num 'col))
-      '()
-      (list (list (num 'line) (- (num 'col) 1)))))
+        '()
+        (list (list (num 'line) (- (num 'col) 1)))))
   (define right
   (define right
     (if (= width (+ (num 'col) (num 'length)))
     (if (= width (+ (num 'col) (num 'length)))
-      '()
-      (list (list (num 'line) (+ (num 'col) (num 'length))))))
+        '()
+        (list (list (num 'line) (+ (num 'col) (num 'length))))))
   (define up
   (define up
     (if (= 0 (num 'line))
     (if (= 0 (num 'line))
-      '()
-      (position-range (- (num 'line) 1)
-                      (max 0 (- (num 'col) 1))
-                      (min width (+ (num 'col) (num 'length) 1)))))
+        '()
+        (position-range (- (num 'line) 1)
+                        (max 0 (- (num 'col) 1))
+                        (min width (+ (num 'col) (num 'length) 1)))))
   (define down
   (define down
     (if (= (- height 1) (num 'line))
     (if (= (- height 1) (num 'line))
-      '()
-      (position-range (+ (num 'line) 1)
-                      (max 0 (- (num 'col) 1))
-                      (min width (+ (num 'col) (num 'length) 1)))))
+        '()
+        (position-range (+ (num 'line) 1)
+                        (max 0 (- (num 'col) 1))
+                        (min width (+ (num 'col) (num 'length) 1)))))
   (append left right up down))
   (append left right up down))
 
 
 (define asterisks (make-hash))
 (define asterisks (make-hash))
@@ -84,11 +80,11 @@
   (define adjs (collect-adjacent-positions num))
   (define adjs (collect-adjacent-positions num))
   (define (mark coord)
   (define (mark coord)
     (if (not (char=? #\* (char-at (car coord) (cadr coord))))
     (if (not (char=? #\* (char-at (car coord) (cadr coord))))
-      (void)
-      (let ()
-        (when (not (hash-has-key? asterisks coord))
-          (hash-set! asterisks coord '()))
-        (hash-set! asterisks coord (cons (num 'value) (hash-ref asterisks coord))))))
+        (void)
+        (let ()
+          (when (not (hash-has-key? asterisks coord))
+            (hash-set! asterisks coord '()))
+          (hash-set! asterisks coord (cons (num 'value) (hash-ref asterisks coord))))))
   (for-each mark adjs))
   (for-each mark adjs))
 
 
 (for-each mark-adj-asterisk nums)
 (for-each mark-adj-asterisk nums)

+ 3 - 3
advent-of-code/2023/04/1.rkt

@@ -21,9 +21,9 @@
   (make-card win-nums nums))
   (make-card win-nums nums))
 
 
 (define (point card)
 (define (point card)
-  (define wins (length (filter (lambda (x) (member x (card 'win-nums))) (card 'nums))))
+  (define wins (length (filter (λ (x) (member x (card 'win-nums))) (card 'nums))))
   (if (= wins 0)
   (if (= wins 0)
-    0
-    (expt 2 (- wins 1))))
+      0
+      (expt 2 (- wins 1))))
 
 
 (apply + (map point (map parse-card lines)))
 (apply + (map point (map parse-card lines)))

+ 13 - 13
advent-of-code/2023/04/2.rkt

@@ -25,23 +25,23 @@
 (define card-count (make-vector (length cards) 1))
 (define card-count (make-vector (length cards) 1))
 
 
 (define (win-count card)
 (define (win-count card)
-    (length (filter (lambda (x) (member x (card 'win-nums))) (card 'nums))))
+  (length (filter (λ (x) (member x (card 'win-nums))) (card 'nums))))
 
 
 (define card-vec (list->vector cards))
 (define card-vec (list->vector cards))
 
 
 (let loop ((i 0))
 (let loop ((i 0))
   (if (>= i (vector-length card-count))
   (if (>= i (vector-length card-count))
-    (void)
-    (let ()
-      (define win-cnt (win-count (vector-ref card-vec i)))
-      (let loop ((j (+ i 1)))
-        (if (or (>= j (vector-length card-count))
-                (>= j (+ 1 i win-cnt)))
-            (void)
-            (let ()
-              (vector-set! card-count j (+ (vector-ref card-count i)
-                                           (vector-ref card-count j)))
-              (loop (+ 1 j)))))
-      (loop (+ 1 i)))))
+      (void)
+      (let ()
+        (define win-cnt (win-count (vector-ref card-vec i)))
+        (let loop ((j (+ i 1)))
+          (if (or (>= j (vector-length card-count))
+                  (>= j (+ 1 i win-cnt)))
+              (void)
+              (let ()
+                (vector-set! card-count j (+ (vector-ref card-count i)
+                                             (vector-ref card-count j)))
+                (loop (+ 1 j)))))
+        (loop (+ 1 i)))))
 
 
 (apply + (vector->list card-count))
 (apply + (vector->list card-count))

+ 16 - 16
advent-of-code/2023/05/1.rkt

@@ -2,11 +2,11 @@
 
 
 (define port (open-input-file "input"))
 (define port (open-input-file "input"))
 
 
-(define seed 
+(define seed
   (let ()
   (let ()
-    (define nums-str 
+    (define nums-str
       (string-trim
       (string-trim
-          (list-ref (string-split (read-line port) ":") 1)))
+       (list-ref (string-split (read-line port) ":") 1)))
     (map string->number (string-split nums-str " "))))
     (map string->number (string-split nums-str " "))))
 
 
 (begin (read-line port) (void))
 (begin (read-line port) (void))
@@ -19,9 +19,9 @@
   (define (loop ret)
   (define (loop ret)
     (define line (string-trim (read-line-convert-eof port)))
     (define line (string-trim (read-line-convert-eof port)))
     (if (= 0 (string-length line))
     (if (= 0 (string-length line))
-      (reverse ret)
-      (loop (cons (map string->number (string-split line " "))
-                  ret))))
+        (reverse ret)
+        (loop (cons (map string->number (string-split line " "))
+                    ret))))
   (read-line port)
   (read-line port)
   (loop '()))
   (loop '()))
 
 
@@ -38,16 +38,16 @@
   (define (mapper x)
   (define (mapper x)
     (define (loop the-map)
     (define (loop the-map)
       (if (null? the-map)
       (if (null? the-map)
-        x
-        (let ()
-          (define cur-map (car the-map))
-          (define target (car cur-map))
-          (define start (cadr cur-map))
-          (define len (caddr cur-map))
-          (if (and (>= x start)
-                   (<= x (+ start len)))
-            (+ target (- x start))
-            (loop (cdr the-map))))))
+          x
+          (let ()
+            (define cur-map (car the-map))
+            (define target (car cur-map))
+            (define start (cadr cur-map))
+            (define len (caddr cur-map))
+            (if (and (>= x start)
+                     (<= x (+ start len)))
+                (+ target (- x start))
+                (loop (cdr the-map))))))
     (loop the-map))
     (loop the-map))
   mapper)
   mapper)
 
 

+ 43 - 43
advent-of-code/2023/05/2.rkt

@@ -2,24 +2,24 @@
 
 
 (define port (open-input-file "input"))
 (define port (open-input-file "input"))
 
 
-(define seed 
+(define seed
   (let ()
   (let ()
-    (define nums-str 
+    (define nums-str
       (string-trim
       (string-trim
-          (list-ref (string-split (read-line port) ":") 1)))
+       (list-ref (string-split (read-line port) ":") 1)))
     (define (pairing l)
     (define (pairing l)
       (define (loop ret l)
       (define (loop ret l)
         (if (null? l)
         (if (null? l)
-          ret
-          (loop
-            (cons (cons 
-                    (car l) 
+            ret
+            (loop
+             (cons (cons
+                    (car l)
                     (+ (car l) (cadr l)))
                     (+ (car l) (cadr l)))
-                  ret)
-            (cddr l))))
+                   ret)
+             (cddr l))))
       (loop '() l))
       (loop '() l))
-    (reverse 
-      (pairing (map string->number (string-split nums-str " "))))))
+    (reverse
+     (pairing (map string->number (string-split nums-str " "))))))
 
 
 (begin (read-line port) (void))
 (begin (read-line port) (void))
 
 
@@ -31,9 +31,9 @@
   (define (loop ret)
   (define (loop ret)
     (define line (string-trim (read-line-convert-eof port)))
     (define line (string-trim (read-line-convert-eof port)))
     (if (= 0 (string-length line))
     (if (= 0 (string-length line))
-      (sort (reverse ret) (lambda (x y) (< (cadr x) (cadr y))))
-      (loop (cons (map string->number (string-split line " "))
-                  ret))))
+        (sort (reverse ret) (λ (x y) (< (cadr x) (cadr y))))
+        (loop (cons (map string->number (string-split line " "))
+                    ret))))
   (read-line port)
   (read-line port)
   (loop '()))
   (loop '()))
 
 
@@ -53,40 +53,40 @@
     (define start (car r))
     (define start (car r))
     (define end (cdr r))
     (define end (cdr r))
     (if (null? mlist)
     (if (null? mlist)
-      (cons r result)
-      (let ()
-        (define cur-map (car mlist))
-        (define map-target (car cur-map))
-        (define map-start (cadr cur-map))
-        (define map-end (+ map-start (caddr cur-map)))
-        (define offset (- map-target map-start))
-        (define (pair-offset p) (cons (+ offset (car p)) (+ offset (cdr p))))
-        (cond ((<= end start)
-                    result)
-              ((>= start map-end)
-                    (loop result (cdr mlist) r))
-              ((>= start map-start)
-                    (loop
-                      (cons (pair-offset (cons start (min end map-end)))
-                            result)
-                      (cdr mlist)
-                      (cons (min end map-end) end)))
-              ((<= end map-start)
-                    (cons r result))
-              ((< start map-start)
-                    (loop
-                      (cons (cons start map-start) result)
-                      mlist
-                      (cons map-start end)))
-              (else (error "unhandled cond in range-map"))))))
+        (cons r result)
+        (let ()
+          (define cur-map (car mlist))
+          (define map-target (car cur-map))
+          (define map-start (cadr cur-map))
+          (define map-end (+ map-start (caddr cur-map)))
+          (define offset (- map-target map-start))
+          (define (pair-offset p) (cons (+ offset (car p)) (+ offset (cdr p))))
+          (cond ((<= end start)
+                 result)
+                ((>= start map-end)
+                 (loop result (cdr mlist) r))
+                ((>= start map-start)
+                 (loop
+                  (cons (pair-offset (cons start (min end map-end)))
+                        result)
+                  (cdr mlist)
+                  (cons (min end map-end) end)))
+                ((<= end map-start)
+                 (cons r result))
+                ((< start map-start)
+                 (loop
+                  (cons (cons start map-start) result)
+                  mlist
+                  (cons map-start end)))
+                (else (error "unhandled cond in range-map"))))))
   (reverse (loop '() mlist r)))
   (reverse (loop '() mlist r)))
 
 
 (define (gen-range-mapper the-map)
 (define (gen-range-mapper the-map)
   (define (mapper range-list)
   (define (mapper range-list)
     (apply append
     (apply append
-        (map 
-            (lambda (range)
-              (range-map the-map range)) 
+           (map
+            (λ (range)
+              (range-map the-map range))
             range-list)))
             range-list)))
   mapper)
   mapper)
 
 

+ 16 - 16
advent-of-code/2023/06/1.rkt

@@ -3,11 +3,11 @@
 (define in (open-input-file "input"))
 (define in (open-input-file "input"))
 
 
 (define (read-num-list)
 (define (read-num-list)
-   (define line (read-line in))
-   (map
-       string->number
-       (string-split
-           (cadr (string-split (string-trim line) ":")))))
+  (define line (read-line in))
+  (map
+   string->number
+   (string-split
+    (cadr (string-split (string-trim line) ":")))))
 
 
 (define times (read-num-list))
 (define times (read-num-list))
 (define distances (read-num-list))
 (define distances (read-num-list))
@@ -15,18 +15,18 @@
 (define games (map cons times distances))
 (define games (map cons times distances))
 
 
 (define (calc time hold-time)
 (define (calc time hold-time)
-    (* hold-time (- time hold-time)))
+  (* hold-time (- time hold-time)))
 
 
 (define (solve time distance)
 (define (solve time distance)
-    (define (loop counter hold-time)
-        (if (> hold-time time)
-            counter
-            (if (> (calc time hold-time) distance)
-                (loop (+ 1 counter) (+ 1 hold-time))
-                (loop counter (+ 1 hold-time)))))
-    (loop 0 0))
+  (define (loop counter hold-time)
+    (if (> hold-time time)
+        counter
+        (if (> (calc time hold-time) distance)
+            (loop (+ 1 counter) (+ 1 hold-time))
+            (loop counter (+ 1 hold-time)))))
+  (loop 0 0))
 
 
-(display (apply * (map 
-                     (lambda (x) (solve (car x) (cdr x)))
-                     games)))
+(display (apply * (map
+                    (x) (solve (car x) (cdr x)))
+                   games)))
 (newline)
 (newline)

+ 14 - 14
advent-of-code/2023/06/2.rkt

@@ -3,27 +3,27 @@
 (define in (open-input-file "input"))
 (define in (open-input-file "input"))
 
 
 (define (read-num)
 (define (read-num)
-   (define line (read-line in))
-   (string->number
-       (apply
-            string-append 
-            (string-split
-                (cadr (string-split (string-trim line) ":"))))))
+  (define line (read-line in))
+  (string->number
+   (apply
+    string-append
+    (string-split
+     (cadr (string-split (string-trim line) ":"))))))
 
 
 (define time (read-num))
 (define time (read-num))
 (define distance (read-num))
 (define distance (read-num))
 
 
 (define (calc time hold-time)
 (define (calc time hold-time)
-    (* hold-time (- time hold-time)))
+  (* hold-time (- time hold-time)))
 
 
 (define (solve time distance)
 (define (solve time distance)
-    (define (loop counter hold-time)
-        (if (> hold-time time)
-            counter
-            (if (> (calc time hold-time) distance)
-                (loop (+ 1 counter) (+ 1 hold-time))
-                (loop counter (+ 1 hold-time)))))
-    (loop 0 0))
+  (define (loop counter hold-time)
+    (if (> hold-time time)
+        counter
+        (if (> (calc time hold-time) distance)
+            (loop (+ 1 counter) (+ 1 hold-time))
+            (loop counter (+ 1 hold-time)))))
+  (loop 0 0))
 
 
 (display (solve time distance))
 (display (solve time distance))
 (newline)
 (newline)

+ 31 - 31
advent-of-code/2023/07/1.rkt

@@ -1,18 +1,18 @@
 #lang racket
 #lang racket
 
 
-(define input 
+(define input
   (with-input-from-file "input"
   (with-input-from-file "input"
-    (lambda ()
+    (λ ()
       (let loop ((cards '()))
       (let loop ((cards '()))
         (define line (read-line))
         (define line (read-line))
         (if (or (eof-object? line)
         (if (or (eof-object? line)
-              (= 0 (string-length line)))
-          (reverse cards)
-          (let ()
-            (define splited-line (string-split line))
-            (define hand (car splited-line))
-            (define bid (string->number (cadr splited-line)))
-            (loop (cons (list hand bid) cards))))))))
+                (= 0 (string-length line)))
+            (reverse cards)
+            (let ()
+              (define splited-line (string-split line))
+              (define hand (car splited-line))
+              (define bid (string->number (cadr splited-line)))
+              (loop (cons (list hand bid) cards))))))))
 
 
 (define (card-number char)
 (define (card-number char)
   (cond ((eq? char #\A) 12)
   (cond ((eq? char #\A) 12)
@@ -27,51 +27,51 @@
   (define vec (make-vector 13 0))
   (define vec (make-vector 13 0))
   (let loop ((i 0))
   (let loop ((i 0))
     (if (>= i 5)
     (if (>= i 5)
-      (vector->list (vector-sort vec >))
-      (let ()
-        (define index (card-number (string-ref hand i)))
-        (vector-set! vec index (+ 1 (vector-ref vec index)))
-        (loop (+ i 1))))))
+        (vector->list (vector-sort vec >))
+        (let ()
+          (define index (card-number (string-ref hand i)))
+          (vector-set! vec index (+ 1 (vector-ref vec index)))
+          (loop (+ i 1))))))
 
 
 (define (hand-type<? type1 type2)
 (define (hand-type<? type1 type2)
   (if (or (null? type1)
   (if (or (null? type1)
           (null? type2))
           (null? type2))
-    #f
-    (if (= (car type1) (car type2))
-      (hand-type<? (cdr type1) (cdr type2))
-      (< (car type1) (car type2)))))
+      #f
+      (if (= (car type1) (car type2))
+          (hand-type<? (cdr type1) (cdr type2))
+          (< (car type1) (car type2)))))
 
 
 (define (hand-type=? type1 type2)
 (define (hand-type=? type1 type2)
   (if (null? type1)
   (if (null? type1)
-    #t
-    (if (= (car type1) (car type2))
-      (hand-type=? (cdr type1) (cdr type2))
-      #f)))
+      #t
+      (if (= (car type1) (car type2))
+          (hand-type=? (cdr type1) (cdr type2))
+          #f)))
 
 
 (define (raw-hand<? hand1 hand2)
 (define (raw-hand<? hand1 hand2)
-    (define h1 (map card-number (string->list hand1)))
-    (define h2 (map card-number (string->list hand2)))
-    (hand-type<? h1 h2))
+  (define h1 (map card-number (string->list hand1)))
+  (define h2 (map card-number (string->list hand2)))
+  (hand-type<? h1 h2))
 
 
 (define (hand<? hand1 hand2)
 (define (hand<? hand1 hand2)
   (define type1 (hand-type hand1))
   (define type1 (hand-type hand1))
   (define type2 (hand-type hand2))
   (define type2 (hand-type hand2))
   (if (hand-type=? type1 type2)
   (if (hand-type=? type1 type2)
-    (raw-hand<? hand1 hand2)
-    (hand-type<? type1 type2)))
+      (raw-hand<? hand1 hand2)
+      (hand-type<? type1 type2)))
 
 
 (define sorted-cards
 (define sorted-cards
-  (sort input (lambda (a b)
+  (sort input (λ (a b)
                 (hand<? (car a) (car b)))))
                 (hand<? (car a) (car b)))))
 
 
 (define (calc-points card)
 (define (calc-points card)
-    (* (cadar card) (cadr card)))
+  (* (cadar card) (cadr card)))
 
 
 (define (enumerate lst)
 (define (enumerate lst)
   (let loop ((i 1) (ret '()) (remain lst))
   (let loop ((i 1) (ret '()) (remain lst))
     (if (null? remain)
     (if (null? remain)
-      (reverse ret)
-      (loop (+ 1 i) (cons (list (car remain) i) ret) (cdr remain)))))
+        (reverse ret)
+        (loop (+ 1 i) (cons (list (car remain) i) ret) (cdr remain)))))
 
 
 (define result
 (define result
   (apply + (map calc-points (enumerate sorted-cards))))
   (apply + (map calc-points (enumerate sorted-cards))))

+ 31 - 31
advent-of-code/2023/07/2.rkt

@@ -1,18 +1,18 @@
 #lang racket
 #lang racket
 
 
-(define input 
+(define input
   (with-input-from-file "input"
   (with-input-from-file "input"
-    (lambda ()
+    (λ ()
       (let loop ((cards '()))
       (let loop ((cards '()))
         (define line (read-line))
         (define line (read-line))
         (if (or (eof-object? line)
         (if (or (eof-object? line)
-              (= 0 (string-length line)))
-          (reverse cards)
-          (let ()
-            (define splited-line (string-split line))
-            (define hand (car splited-line))
-            (define bid (string->number (cadr splited-line)))
-            (loop (cons (list hand bid) cards))))))))
+                (= 0 (string-length line)))
+            (reverse cards)
+            (let ()
+              (define splited-line (string-split line))
+              (define hand (car splited-line))
+              (define bid (string->number (cadr splited-line)))
+              (loop (cons (list hand bid) cards))))))))
 
 
 (define (card-number char)
 (define (card-number char)
   (cond ((eq? char #\A) 12)
   (cond ((eq? char #\A) 12)
@@ -34,51 +34,51 @@
   (define vec (make-vector 13 0))
   (define vec (make-vector 13 0))
   (let loop ((i 0))
   (let loop ((i 0))
     (if (>= i 5)
     (if (>= i 5)
-      (vector->list (joker-transform vec))
-      (let ()
-        (define index (card-number (string-ref hand i)))
-        (vector-set! vec index (+ 1 (vector-ref vec index)))
-        (loop (+ i 1))))))
+        (vector->list (joker-transform vec))
+        (let ()
+          (define index (card-number (string-ref hand i)))
+          (vector-set! vec index (+ 1 (vector-ref vec index)))
+          (loop (+ i 1))))))
 
 
 (define (hand-type<? type1 type2)
 (define (hand-type<? type1 type2)
   (if (or (null? type1)
   (if (or (null? type1)
           (null? type2))
           (null? type2))
-    #f
-    (if (= (car type1) (car type2))
-      (hand-type<? (cdr type1) (cdr type2))
-      (< (car type1) (car type2)))))
+      #f
+      (if (= (car type1) (car type2))
+          (hand-type<? (cdr type1) (cdr type2))
+          (< (car type1) (car type2)))))
 
 
 (define (hand-type=? type1 type2)
 (define (hand-type=? type1 type2)
   (if (null? type1)
   (if (null? type1)
-    #t
-    (if (= (car type1) (car type2))
-      (hand-type=? (cdr type1) (cdr type2))
-      #f)))
+      #t
+      (if (= (car type1) (car type2))
+          (hand-type=? (cdr type1) (cdr type2))
+          #f)))
 
 
 (define (raw-hand<? hand1 hand2)
 (define (raw-hand<? hand1 hand2)
-    (define h1 (map card-number (string->list hand1)))
-    (define h2 (map card-number (string->list hand2)))
-    (hand-type<? h1 h2))
+  (define h1 (map card-number (string->list hand1)))
+  (define h2 (map card-number (string->list hand2)))
+  (hand-type<? h1 h2))
 
 
 (define (hand<? hand1 hand2)
 (define (hand<? hand1 hand2)
   (define type1 (hand-type hand1))
   (define type1 (hand-type hand1))
   (define type2 (hand-type hand2))
   (define type2 (hand-type hand2))
   (if (hand-type=? type1 type2)
   (if (hand-type=? type1 type2)
-    (raw-hand<? hand1 hand2)
-    (hand-type<? type1 type2)))
+      (raw-hand<? hand1 hand2)
+      (hand-type<? type1 type2)))
 
 
 (define sorted-cards
 (define sorted-cards
-  (sort input (lambda (a b)
+  (sort input (λ (a b)
                 (hand<? (car a) (car b)))))
                 (hand<? (car a) (car b)))))
 
 
 (define (calc-points card)
 (define (calc-points card)
-    (* (cadar card) (cadr card)))
+  (* (cadar card) (cadr card)))
 
 
 (define (enumerate lst)
 (define (enumerate lst)
   (let loop ((i 1) (ret '()) (remain lst))
   (let loop ((i 1) (ret '()) (remain lst))
     (if (null? remain)
     (if (null? remain)
-      (reverse ret)
-      (loop (+ 1 i) (cons (list (car remain) i) ret) (cdr remain)))))
+        (reverse ret)
+        (loop (+ 1 i) (cons (list (car remain) i) ret) (cdr remain)))))
 
 
 (define result
 (define result
   (apply + (map calc-points (enumerate sorted-cards))))
   (apply + (map calc-points (enumerate sorted-cards))))

+ 11 - 11
advent-of-code/lib/obj.rkt

@@ -5,13 +5,13 @@
          obj-show)
          obj-show)
 
 
 (define (alist->obj alist)
 (define (alist->obj alist)
-  (lambda key
+  (λ key
     (if (null? key)
     (if (null? key)
-      alist
-      (cadr (assoc (car key) alist)))))
+        alist
+        (cadr (assoc (car key) alist)))))
 
 
 (define (obj-maker . fields)
 (define (obj-maker . fields)
-  (lambda inits
+  (λ inits
     (define alist (map list fields inits))
     (define alist (map list fields inits))
     (alist->obj alist)))
     (alist->obj alist)))
 
 
@@ -20,13 +20,13 @@
   (define new-alist
   (define new-alist
     (let loop ((new-list '()) (cur alist) (is-set #f))
     (let loop ((new-list '()) (cur alist) (is-set #f))
       (if (null? cur)
       (if (null? cur)
-        (if is-set
-          new-list
-          (cons (list key value) new-list))
-        (let ()
-          (if (eq? key (caar cur))
-            (loop (cons (list key value) new-list) (cdr cur) #t)
-            (loop (cons (car cur) new-list) (cdr cur) is-set))))))
+          (if is-set
+              new-list
+              (cons (list key value) new-list))
+          (let ()
+            (if (eq? key (caar cur))
+                (loop (cons (list key value) new-list) (cdr cur) #t)
+                (loop (cons (car cur) new-list) (cdr cur) is-set))))))
   (alist->obj new-alist))
   (alist->obj new-alist))
 
 
 (define (obj-show x) (x))
 (define (obj-show x) (x))

+ 10 - 10
advent-of-code/lib/utils.rkt

@@ -8,21 +8,21 @@
 (define (repeat n e)
 (define (repeat n e)
   (let loop ((i 0) (ret '()))
   (let loop ((i 0) (ret '()))
     (if (>= i n)
     (if (>= i n)
-      ret
-      (loop (+ 1 i) (cons e ret)))))
+        ret
+        (loop (+ 1 i) (cons e ret)))))
 
 
 (define (get-lines fp)
 (define (get-lines fp)
   (let loop ((lines '()))
   (let loop ((lines '()))
     (define l (read-line fp))
     (define l (read-line fp))
     (if (eof-object? l)
     (if (eof-object? l)
-      (reverse lines)
-      (loop (cons l lines)))))
+        (reverse lines)
+        (loop (cons l lines)))))
 
 
 (define (enumerate lst)
 (define (enumerate lst)
   (let loop ((i 1) (ret '()) (remain lst))
   (let loop ((i 1) (ret '()) (remain lst))
     (if (null? remain)
     (if (null? remain)
-      (reverse ret)
-      (loop (+ 1 i) (cons (list (car remain) i) ret) (cdr remain)))))
+        (reverse ret)
+        (loop (+ 1 i) (cons (list (car remain) i) ret) (cdr remain)))))
 
 
 (define (split-list-by e lst . eq)
 (define (split-list-by e lst . eq)
   (define cmp (if (null? eq) equal? eq))
   (define cmp (if (null? eq) equal? eq))
@@ -30,7 +30,7 @@
              (cur '())
              (cur '())
              (lst lst))
              (lst lst))
     (if (null? lst)
     (if (null? lst)
-      (reverse (cons (reverse cur) ret))
-      (if (cmp e (car lst))
-        (loop (cons (reverse cur) ret) '() (cdr lst))
-        (loop ret (cons (car lst) cur) (cdr lst))))))
+        (reverse (cons (reverse cur) ret))
+        (if (cmp e (car lst))
+            (loop (cons (reverse cur) ret) '() (cdr lst))
+            (loop ret (cons (car lst) cur) (cdr lst))))))