aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-racket/day-13
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2024-05-30 21:49:58 -0400
committerJ.J <thechairman@thechairman.info>2024-05-30 21:49:58 -0400
commit231c2b688d1e6cf0846d46e883da30e042a9c6cf (patch)
tree98a6d3a461fe190b38b2cf33a708a1d01703fa70 /aoc2023-racket/day-13
parentfe088aa5778dcdbaab4dd8d4a7395a91c444b45c (diff)
parenta2c2b728ec6051323ed937f54816089cd2ae9d20 (diff)
downloadgleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.tar.gz
gleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2023-racket/day-13')
-rw-r--r--aoc2023-racket/day-13/day-13.rkt47
1 files changed, 47 insertions, 0 deletions
diff --git a/aoc2023-racket/day-13/day-13.rkt b/aoc2023-racket/day-13/day-13.rkt
new file mode 100644
index 0000000..47718f8
--- /dev/null
+++ b/aoc2023-racket/day-13/day-13.rkt
@@ -0,0 +1,47 @@
+#lang racket
+
+(require advent-of-code
+ threading)
+
+(define input
+ (~>(fetch-aoc-input (find-session) 2023 13 #:cache #true)
+ (string-split "\n\n")
+ (map (λ~> string-split) _)))
+
+(define (do-symmetric? lefts rights errs)
+ (cond
+ [(empty? rights) #f]
+ [else
+ (define found-errs
+ (for/sum ([l (in-string (string-join lefts ""))]
+ [r (in-string (string-join rights ""))]
+ #:unless (char=? l r))
+ 1))
+ (if (= errs found-errs)
+ (length lefts)
+ (do-symmetric? (cons (first rights) lefts)
+ (rest rights)
+ errs))]))
+
+(define (symmetric? xss errs)
+ (do-symmetric? (list (first xss)) (rest xss) errs))
+
+(define (transpose strs)
+ (~> strs
+ (map string->list _)
+ (apply map list _)
+ (map list->string _)))
+
+(define (find-symmetry-score xss errs)
+ (cond
+ [(symmetric? xss errs) => (curry * 100)]
+ [else (symmetric? (transpose xss) errs)]))
+
+;; part 1
+(for/sum ([note (in-list input)])
+ (find-symmetry-score note 0))
+
+;; part 2
+(for/sum ([note (in-list input)])
+ (find-symmetry-score note 1))
+