diff options
Diffstat (limited to 'advent-of-code')
| -rw-r--r-- | advent-of-code/2022/01/1.rkt | 2 | ||||
| -rw-r--r-- | advent-of-code/2022/01/2.rkt | 2 | ||||
| -rw-r--r-- | advent-of-code/2022/02/1.rkt | 34 | ||||
| -rw-r--r-- | advent-of-code/2022/02/2.rkt | 60 | ||||
| -rw-r--r-- | advent-of-code/2023/01/1.rkt | 2 | ||||
| -rw-r--r-- | advent-of-code/2023/01/2.rkt | 2 | ||||
| -rw-r--r-- | advent-of-code/2023/02/1.rkt | 44 | ||||
| -rw-r--r-- | advent-of-code/2023/02/2.rkt | 26 | ||||
| -rw-r--r-- | advent-of-code/2023/03/1.rkt | 74 | ||||
| -rw-r--r-- | advent-of-code/2023/03/2.rkt | 82 | ||||
| -rw-r--r-- | advent-of-code/2023/04/1.rkt | 6 | ||||
| -rw-r--r-- | advent-of-code/2023/04/2.rkt | 26 | ||||
| -rwxr-xr-x | advent-of-code/2023/05/1.rkt | 32 | ||||
| -rwxr-xr-x | advent-of-code/2023/05/2.rkt | 86 | ||||
| -rw-r--r-- | advent-of-code/2023/06/1.rkt | 32 | ||||
| -rw-r--r-- | advent-of-code/2023/06/2.rkt | 28 | ||||
| -rw-r--r-- | advent-of-code/2023/07/1.rkt | 62 | ||||
| -rw-r--r-- | advent-of-code/2023/07/2.rkt | 62 | ||||
| -rw-r--r-- | advent-of-code/lib/obj.rkt | 22 | ||||
| -rw-r--r-- | advent-of-code/lib/utils.rkt | 20 |
20 files changed, 350 insertions, 354 deletions
diff --git a/advent-of-code/2022/01/1.rkt b/advent-of-code/2022/01/1.rkt index e7c335e..e8d1f23 100644 --- a/advent-of-code/2022/01/1.rkt +++ b/advent-of-code/2022/01/1.rkt @@ -4,7 +4,7 @@ (define lines (call-with-input-file "input" - (lambda (fp) + (λ (fp) (get-lines fp)))) (define elves (split-list-by "" lines)) diff --git a/advent-of-code/2022/01/2.rkt b/advent-of-code/2022/01/2.rkt index 1644885..e7b70ab 100644 --- a/advent-of-code/2022/01/2.rkt +++ b/advent-of-code/2022/01/2.rkt @@ -4,7 +4,7 @@ (define lines (call-with-input-file "input" - (lambda (fp) + (λ (fp) (get-lines fp)))) (define elves (split-list-by "" lines)) diff --git a/advent-of-code/2022/02/1.rkt b/advent-of-code/2022/02/1.rkt index 35c5a5c..5114c9c 100644 --- a/advent-of-code/2022/02/1.rkt +++ b/advent-of-code/2022/02/1.rkt @@ -3,26 +3,26 @@ (require "../../lib/utils.rkt") (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 (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))
\ No newline at end of file diff --git a/advent-of-code/2022/02/2.rkt b/advent-of-code/2022/02/2.rkt index 4b0dd02..608d8aa 100644 --- a/advent-of-code/2022/02/2.rkt +++ b/advent-of-code/2022/02/2.rkt @@ -3,43 +3,43 @@ (require "../../lib/utils.rkt") (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 (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 - (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 (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))
\ No newline at end of file diff --git a/advent-of-code/2023/01/1.rkt b/advent-of-code/2023/01/1.rkt index 57d366d..8953e84 100644 --- a/advent-of-code/2023/01/1.rkt +++ b/advent-of-code/2023/01/1.rkt @@ -8,7 +8,7 @@ (define (extract-number 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)))) number-list)) (+ (last number-list) (* 10 (car number-list)))) diff --git a/advent-of-code/2023/01/2.rkt b/advent-of-code/2023/01/2.rkt index 7b252db..f39d87c 100644 --- a/advent-of-code/2023/01/2.rkt +++ b/advent-of-code/2023/01/2.rkt @@ -21,7 +21,7 @@ (define (extract-number rawline) (define line (replace-digit rawline)) (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)))) number-list)) (+ (last number-list) (* 10 (car number-list)))) diff --git a/advent-of-code/2023/02/1.rkt b/advent-of-code/2023/02/1.rkt index e0a7bf9..500e916 100644 --- a/advent-of-code/2023/02/1.rkt +++ b/advent-of-code/2023/02/1.rkt @@ -18,35 +18,35 @@ (define b 0) (define balls (string-split str ",")) (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)) (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)) (define (possible? 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 - (filter (lambda (game) + (filter (λ (game) (possible? (car game))) (enumerate (map extract-game lines))))) diff --git a/advent-of-code/2023/02/2.rkt b/advent-of-code/2023/02/2.rkt index 87599d7..7da2cd2 100644 --- a/advent-of-code/2023/02/2.rkt +++ b/advent-of-code/2023/02/2.rkt @@ -18,21 +18,21 @@ (define b 0) (define balls (string-split str ",")) (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)) (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)) (define (power game) diff --git a/advent-of-code/2023/03/1.rkt b/advent-of-code/2023/03/1.rkt index 2b9a1d2..5e86227 100644 --- a/advent-of-code/2023/03/1.rkt +++ b/advent-of-code/2023/03/1.rkt @@ -5,7 +5,7 @@ (define (read-input) (call-with-input-file "input" - (lambda (fp) + (λ (fp) (list->vector (get-lines fp))))) (define schema (read-input)) @@ -22,29 +22,29 @@ (define nums '()) (let loop ((i 0)) (if (>= i height) - (void) - (let () - (let loop ((j 0)) + (void) + (let () + (let loop ((j 0)) (define curline (vector-ref schema i)) (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)) (define nums (scan-nums)) @@ -55,30 +55,30 @@ (define (collect-adjacent num) (define left (if (= 0 (num 'col)) - '() - (list (char-at (num 'line) (- (num 'col) 1))))) + '() + (list (char-at (num 'line) (- (num 'col) 1))))) (define right (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 (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 (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)) (define (is-part-num? num) (findf is-symbol? (collect-adjacent num))) -(apply + (map (lambda (x) (x 'value)) +(apply + (map (λ (x) (x 'value)) (filter is-part-num? nums)))
\ No newline at end of file diff --git a/advent-of-code/2023/03/2.rkt b/advent-of-code/2023/03/2.rkt index 2bc8c6b..71c3221 100644 --- a/advent-of-code/2023/03/2.rkt +++ b/advent-of-code/2023/03/2.rkt @@ -5,7 +5,7 @@ (define (read-input) (call-with-input-file "input" - (lambda (fp) + (λ (fp) (list->vector (get-lines fp))))) (define schema (read-input)) @@ -22,60 +22,56 @@ (define nums '()) (let loop ((i 0)) (if (>= i height) - (void) - (let () - (let loop ((j 0)) + (void) + (let () + (let loop ((j 0)) (define curline (vector-ref schema i)) (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)) (define nums (scan-nums)) -(define (is-symbol? c) - (and (not (char-numeric? c)) - (not (char=? #\. c)))) - (define (collect-adjacent-positions num) (define (position-range line start end) (define delta (- end start)) (map list (repeat delta line) (range start end))) (define left (if (= 0 (num 'col)) - '() - (list (list (num 'line) (- (num 'col) 1))))) + '() + (list (list (num 'line) (- (num 'col) 1))))) (define right (if (= width (+ (num 'col) (num 'length))) - '() - (list (list (num 'line) (+ (num 'col) (num 'length)))))) + '() + (list (list (num 'line) (+ (num 'col) (num 'length)))))) (define up (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 (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)) (define asterisks (make-hash)) @@ -84,11 +80,11 @@ (define adjs (collect-adjacent-positions num)) (define (mark 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-adj-asterisk nums) diff --git a/advent-of-code/2023/04/1.rkt b/advent-of-code/2023/04/1.rkt index 8198403..e8dd242 100644 --- a/advent-of-code/2023/04/1.rkt +++ b/advent-of-code/2023/04/1.rkt @@ -21,9 +21,9 @@ (make-card win-nums nums)) (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) - 0 - (expt 2 (- wins 1)))) + 0 + (expt 2 (- wins 1)))) (apply + (map point (map parse-card lines))) diff --git a/advent-of-code/2023/04/2.rkt b/advent-of-code/2023/04/2.rkt index fa1dd86..eec9632 100644 --- a/advent-of-code/2023/04/2.rkt +++ b/advent-of-code/2023/04/2.rkt @@ -25,23 +25,23 @@ (define card-count (make-vector (length cards) 1)) (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)) (let loop ((i 0)) (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))
\ No newline at end of file diff --git a/advent-of-code/2023/05/1.rkt b/advent-of-code/2023/05/1.rkt index f0ad5ed..352e10c 100755 --- a/advent-of-code/2023/05/1.rkt +++ b/advent-of-code/2023/05/1.rkt @@ -2,11 +2,11 @@ (define port (open-input-file "input")) -(define seed +(define seed (let () - (define nums-str + (define nums-str (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 " ")))) (begin (read-line port) (void)) @@ -19,9 +19,9 @@ (define (loop ret) (define line (string-trim (read-line-convert-eof port))) (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) (loop '())) @@ -38,16 +38,16 @@ (define (mapper x) (define (loop 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)) mapper) diff --git a/advent-of-code/2023/05/2.rkt b/advent-of-code/2023/05/2.rkt index 5024c1e..8e39c40 100755 --- a/advent-of-code/2023/05/2.rkt +++ b/advent-of-code/2023/05/2.rkt @@ -2,24 +2,24 @@ (define port (open-input-file "input")) -(define seed +(define seed (let () - (define nums-str + (define nums-str (string-trim - (list-ref (string-split (read-line port) ":") 1))) + (list-ref (string-split (read-line port) ":") 1))) (define (pairing l) (define (loop ret l) (if (null? l) - ret - (loop - (cons (cons - (car l) + ret + (loop + (cons (cons + (car l) (+ (car l) (cadr l))) - ret) - (cddr l)))) + ret) + (cddr 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)) @@ -31,9 +31,9 @@ (define (loop ret) (define line (string-trim (read-line-convert-eof port))) (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) (loop '())) @@ -53,40 +53,40 @@ (define start (car r)) (define end (cdr r)) (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))) (define (gen-range-mapper the-map) (define (mapper range-list) (apply append - (map - (lambda (range) - (range-map the-map range)) + (map + (λ (range) + (range-map the-map range)) range-list))) mapper) diff --git a/advent-of-code/2023/06/1.rkt b/advent-of-code/2023/06/1.rkt index ea0d013..5496476 100644 --- a/advent-of-code/2023/06/1.rkt +++ b/advent-of-code/2023/06/1.rkt @@ -3,11 +3,11 @@ (define in (open-input-file "input")) (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 distances (read-num-list)) @@ -15,18 +15,18 @@ (define games (map cons times distances)) (define (calc time hold-time) - (* hold-time (- time hold-time))) + (* hold-time (- time hold-time))) (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) diff --git a/advent-of-code/2023/06/2.rkt b/advent-of-code/2023/06/2.rkt index 5d124e0..2d666ed 100644 --- a/advent-of-code/2023/06/2.rkt +++ b/advent-of-code/2023/06/2.rkt @@ -3,27 +3,27 @@ (define in (open-input-file "input")) (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 distance (read-num)) (define (calc time hold-time) - (* hold-time (- time hold-time))) + (* hold-time (- time hold-time))) (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)) (newline) diff --git a/advent-of-code/2023/07/1.rkt b/advent-of-code/2023/07/1.rkt index a22571d..286a0e8 100644 --- a/advent-of-code/2023/07/1.rkt +++ b/advent-of-code/2023/07/1.rkt @@ -1,18 +1,18 @@ #lang racket -(define input +(define input (with-input-from-file "input" - (lambda () + (λ () (let loop ((cards '())) (define line (read-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) (cond ((eq? char #\A) 12) @@ -27,51 +27,51 @@ (define vec (make-vector 13 0)) (let loop ((i 0)) (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) (if (or (null? type1) (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) (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 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 type1 (hand-type hand1)) (define type2 (hand-type hand2)) (if (hand-type=? type1 type2) - (raw-hand<? hand1 hand2) - (hand-type<? type1 type2))) + (raw-hand<? hand1 hand2) + (hand-type<? type1 type2))) (define sorted-cards - (sort input (lambda (a b) + (sort input (λ (a b) (hand<? (car a) (car b))))) (define (calc-points card) - (* (cadar card) (cadr card))) + (* (cadar card) (cadr card))) (define (enumerate lst) (let loop ((i 1) (ret '()) (remain lst)) (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 (apply + (map calc-points (enumerate sorted-cards)))) diff --git a/advent-of-code/2023/07/2.rkt b/advent-of-code/2023/07/2.rkt index 50d9526..07ff61b 100644 --- a/advent-of-code/2023/07/2.rkt +++ b/advent-of-code/2023/07/2.rkt @@ -1,18 +1,18 @@ #lang racket -(define input +(define input (with-input-from-file "input" - (lambda () + (λ () (let loop ((cards '())) (define line (read-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) (cond ((eq? char #\A) 12) @@ -34,51 +34,51 @@ (define vec (make-vector 13 0)) (let loop ((i 0)) (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) (if (or (null? type1) (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) (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 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 type1 (hand-type hand1)) (define type2 (hand-type hand2)) (if (hand-type=? type1 type2) - (raw-hand<? hand1 hand2) - (hand-type<? type1 type2))) + (raw-hand<? hand1 hand2) + (hand-type<? type1 type2))) (define sorted-cards - (sort input (lambda (a b) + (sort input (λ (a b) (hand<? (car a) (car b))))) (define (calc-points card) - (* (cadar card) (cadr card))) + (* (cadar card) (cadr card))) (define (enumerate lst) (let loop ((i 1) (ret '()) (remain lst)) (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 (apply + (map calc-points (enumerate sorted-cards)))) diff --git a/advent-of-code/lib/obj.rkt b/advent-of-code/lib/obj.rkt index 9217976..8ffc401 100644 --- a/advent-of-code/lib/obj.rkt +++ b/advent-of-code/lib/obj.rkt @@ -5,13 +5,13 @@ obj-show) (define (alist->obj alist) - (lambda key + (λ key (if (null? key) - alist - (cadr (assoc (car key) alist))))) + alist + (cadr (assoc (car key) alist))))) (define (obj-maker . fields) - (lambda inits + (λ inits (define alist (map list fields inits)) (alist->obj alist))) @@ -20,13 +20,13 @@ (define new-alist (let loop ((new-list '()) (cur alist) (is-set #f)) (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)) (define (obj-show x) (x))
\ No newline at end of file diff --git a/advent-of-code/lib/utils.rkt b/advent-of-code/lib/utils.rkt index 42b36af..2ce8a51 100644 --- a/advent-of-code/lib/utils.rkt +++ b/advent-of-code/lib/utils.rkt @@ -8,21 +8,21 @@ (define (repeat n e) (let loop ((i 0) (ret '())) (if (>= i n) - ret - (loop (+ 1 i) (cons e ret))))) + ret + (loop (+ 1 i) (cons e ret))))) (define (get-lines fp) (let loop ((lines '())) (define l (read-line fp)) (if (eof-object? l) - (reverse lines) - (loop (cons l lines))))) + (reverse lines) + (loop (cons l lines))))) (define (enumerate lst) (let loop ((i 1) (ret '()) (remain lst)) (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 cmp (if (null? eq) equal? eq)) @@ -30,7 +30,7 @@ (cur '()) (lst 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))))))
\ No newline at end of file + (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)))))) |
