summaryrefslogtreecommitdiff
path: root/01
diff options
context:
space:
mode:
authorPrefetch2024-03-02 19:36:12 +0100
committerPrefetch2024-03-02 19:36:12 +0100
commit1fbb07c54523c7a576bfff1cb689e155dd55f15a (patch)
tree7aa9f92a7d99ae9203b538803b7efefd846b67e0 /01
parentaf589b238c1d51960d8af3b36041aca2bad7855b (diff)
Add first five days
Diffstat (limited to '01')
-rw-r--r--01/input.txt1
-rw-r--r--01/lib.scm34
-rw-r--r--01/main.scm14
-rw-r--r--01/test.scm43
4 files changed, 92 insertions, 0 deletions
diff --git a/01/input.txt b/01/input.txt
new file mode 100644
index 0000000..eb275f7
--- /dev/null
+++ b/01/input.txt
@@ -0,0 +1 @@
+649713959682898259577777982349515784822684939966191359164369933435366431847754488661965363557985166219358714739318371382388296151195361571216131925158492441461844687324923315381358331571577613789649166486152237945917987977793891739865149734755993241361886336926538482271124755359572791451335842534893192693558659991171983849285489139421425933638614884415896938914992732492192458636484523228244532331587584779552788544667253577324649915274115924611758345676183443982992733966373498385685965768929241477983727921279826727976872556315428434799161759734932659829934562339385328119656823483954856427365892627728163524721467938449943358192632262354854593635831559352247443975945144163183563723562891357859367964126289445982135523535923113589316417623483631637569291941782992213889513714525342468563349385271884221685549996534333765731243895662624829924982971685443825366827923589435254514211489649482374876434549682785459698885521673258939413255158196525696236457911447599947449665542554251486847388823576937167237476556782133227279324526834946534444718161524129285919477959937684728882592779941734186144138883994322742484853925383518651687147246943421311287324867663698432546619583638976637733345251834869985746385371617743498627111441933546356934671639545342515392536574744795732243617113574641284231928489312683617154536648219244996491745718658151648246791826466973654765284263928884137863647623237345882469142933142637583644258427416972595241737254449718531724176538648369253796688931245191382956961544775856872281317743828552629843551844927913147518377362266554334386721313244223233396453291224932499277961525785755863852487141946626663835195286762947172384186667439516367219391823774338692151926472717373235612911848773387771244144969149482477519437822863422662157461968444281972353149695515494992537927492111388193837553844671719291482442337761321272333982924289323437277224565149928416255435841327756139118119744528993269157174414264387573331116323982614862952264597611885999285995516357519648695594299657387614793341626318866519144574571816535351149394735916975448425618171572917195165594323552199346814729617189679698944337146
diff --git a/01/lib.scm b/01/lib.scm
new file mode 100644
index 0000000..fcdf642
--- /dev/null
+++ b/01/lib.scm
@@ -0,0 +1,34 @@
+(library (lib)
+ (export solve-part1 solve-part2)
+ (import (chezscheme))
+
+ ; Rotate contents of `ilist' to the right `times' times
+ (define (list-rotate ilist times)
+ (let loop ((xs ilist) (n times))
+ (if (= n 0)
+ xs
+ (loop (append (cdr xs) (list (car xs))) (- n 1)))))
+
+ (define (solve-puzzle chars chars-rot)
+ (fold-left
+ (lambda (accum c1 c2)
+ ; Does `k'th char in list match with `k'th char in rotated list?
+ (if (char=? c1 c2)
+ ; Assume `c1' is a digit, convert it to integer in [0,9]
+ (+ accum (string->number (string c1)))
+ accum))
+ 0 chars chars-rot))
+
+ ; Compare each char to its right neighbour in circular list
+ (define (solve-part1 str)
+ (let* ((chars (string->list str))
+ (chars-rot (list-rotate chars 1)))
+ (solve-puzzle chars chars-rot)))
+
+ ; Compare each char to char halfway across circular list
+ (define (solve-part2 str)
+ (let* ((chars (string->list str))
+ (chars-rot (list-rotate chars (div (length chars) 2))))
+ (solve-puzzle chars chars-rot)))
+
+)
diff --git a/01/main.scm b/01/main.scm
new file mode 100644
index 0000000..9ec5afa
--- /dev/null
+++ b/01/main.scm
@@ -0,0 +1,14 @@
+(import (chezscheme))
+
+; Where the magic happens
+(import (lib))
+
+; Read my personal puzzle input
+(define input
+ (call-with-input-file "input.txt" get-line))
+
+; Part 1 gives 1228 for me
+(printf "Part 1 solution: ~a\n" (solve-part1 input))
+
+; Part 2 gives 1238 for me
+(printf "Part 2 solution: ~a\n" (solve-part2 input))
diff --git a/01/test.scm b/01/test.scm
new file mode 100644
index 0000000..6dbff81
--- /dev/null
+++ b/01/test.scm
@@ -0,0 +1,43 @@
+(import (chezscheme))
+
+; Where the magic happens
+(import (lib))
+
+; My quick-and-dirty unit testing framework (copied for each day)
+(define (run-test name proc input expected)
+ (let ((result (proc input)))
+ (if (= result expected)
+ (printf "\x1b;[32;1mPASS\x1b;[0m: ~a\n"
+ name)
+ (printf "\x1b;[31;1mFAIL\x1b;[0m: ~a: got ~a, expected ~a\n"
+ name result expected))))
+
+(printf "Part 1 tests:\n")
+
+(define (test-part1 name input expected)
+ (run-test name solve-part1 input expected))
+
+(test-part1 "part 1 example 1"
+ "1122" 3)
+(test-part1 "part 1 example 2"
+ "1111" 4)
+(test-part1 "part 1 example 3"
+ "1234" 0)
+(test-part1 "part 1 example 4"
+ "91212129" 9)
+
+(printf "Part 2 tests:\n")
+
+(define (test-part2 name input expected)
+ (run-test name solve-part2 input expected))
+
+(test-part2 "part 2 example 1"
+ "1212" 6)
+(test-part2 "part 2 example 2"
+ "1221" 0)
+(test-part2 "part 2 example 3"
+ "123425" 4)
+(test-part2 "part 2 example 4"
+ "123123" 12)
+(test-part2 "part 2 example 5"
+ "12131415" 4)