aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2022
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-02-17 22:46:02 +0800
committerMistivia <i@mistivia.com>2024-02-17 22:46:02 +0800
commit3a4e5447a79096d813cdaf4498b9a1e2c086b7ff (patch)
tree16c6d8452de6bbef3b38bc4f85b3d91300eb9e9f /advent-of-code/2022
parent936a3523e268e621e7c754002c5aab61ac6dbb54 (diff)
aoc 2022 02
Diffstat (limited to 'advent-of-code/2022')
-rw-r--r--advent-of-code/2022/01/1.rkt2
-rw-r--r--advent-of-code/2022/01/2.rkt2
-rw-r--r--advent-of-code/2022/02/1.rkt28
-rw-r--r--advent-of-code/2022/02/2.rkt45
4 files changed, 75 insertions, 2 deletions
diff --git a/advent-of-code/2022/01/1.rkt b/advent-of-code/2022/01/1.rkt
index 340a531..e7c335e 100644
--- a/advent-of-code/2022/01/1.rkt
+++ b/advent-of-code/2022/01/1.rkt
@@ -1,4 +1,4 @@
-#lang racket/base
+#lang racket
(require "../../lib/utils.rkt")
diff --git a/advent-of-code/2022/01/2.rkt b/advent-of-code/2022/01/2.rkt
index 40b4f23..1644885 100644
--- a/advent-of-code/2022/01/2.rkt
+++ b/advent-of-code/2022/01/2.rkt
@@ -1,4 +1,4 @@
-#lang racket/base
+#lang racket
(require "../../lib/utils.rkt")
diff --git a/advent-of-code/2022/02/1.rkt b/advent-of-code/2022/02/1.rkt
new file mode 100644
index 0000000..35c5a5c
--- /dev/null
+++ b/advent-of-code/2022/02/1.rkt
@@ -0,0 +1,28 @@
+#lang racket
+
+(require "../../lib/utils.rkt")
+
+(define lines
+ (call-with-input-file "input"
+ (lambda (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))))
+
+(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
new file mode 100644
index 0000000..4b0dd02
--- /dev/null
+++ b/advent-of-code/2022/02/2.rkt
@@ -0,0 +1,45 @@
+#lang racket
+
+(require "../../lib/utils.rkt")
+
+(define lines
+ (call-with-input-file "input"
+ (lambda (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")))))
+
+(set! games
+ (map
+ (lambda (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))))
+
+(apply + (map score games)) \ No newline at end of file