aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-13 20:33:34 -0500
committerHJ <thechairman@thechairman.info>2023-12-13 20:33:34 -0500
commit27d3a02a88826e51842b162b9a9d0bb621c72db8 (patch)
treeee79796e7ea9d3a7d9e3670f7bb9b051cedb7b21
parenta3edcaeaf4395b95ff5b21238e5fd2e45a5f14a4 (diff)
downloadgleam_aoc-27d3a02a88826e51842b162b9a9d0bb621c72db8.tar.gz
gleam_aoc-27d3a02a88826e51842b162b9a9d0bb621c72db8.zip
day 13 revisions
-rw-r--r--aoc2023-other/day-13/day-13.rkt47
-rw-r--r--aoc2023/src/day13/solve.gleam54
2 files changed, 75 insertions, 26 deletions
diff --git a/aoc2023-other/day-13/day-13.rkt b/aoc2023-other/day-13/day-13.rkt
new file mode 100644
index 0000000..47718f8
--- /dev/null
+++ b/aoc2023-other/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))
+
diff --git a/aoc2023/src/day13/solve.gleam b/aoc2023/src/day13/solve.gleam
index a597074..2b3fca2 100644
--- a/aoc2023/src/day13/solve.gleam
+++ b/aoc2023/src/day13/solve.gleam
@@ -2,40 +2,43 @@ import adglent.{First, Second}
import gleam/io
import gleam/list
import gleam/string
-import gleam/result
+import gleam/bool
type SymmetryType {
Horizontal(Int)
Vertical(Int)
}
-fn is_symmetric(xs: List(List(a)), errors: Int, index: Int) -> Result(Int, Nil) {
- case list.split(xs, index) {
- #(_, []) -> Error(Nil)
- #(ls, rs) -> {
- let zipped = list.zip(list.flatten(list.reverse(ls)), list.flatten(rs))
- let found_errors =
- zipped
- |> list.filter(fn(tup) { tup.1 != tup.0 })
- |> list.length
- case found_errors == errors {
- True -> Ok(index)
- False -> is_symmetric(xs, errors, index + 1)
- }
- }
+fn is_symmetric(xss: List(List(a)), errs: Int) {
+ let assert [left, ..right] = xss
+ do_is_symmetric([left], right, errs)
+}
+
+fn do_is_symmetric(
+ left: List(List(a)),
+ right: List(List(a)),
+ errors: Int,
+) -> Result(Int, Nil) {
+ use <- bool.guard(list.is_empty(right), Error(Nil))
+ let assert [h, ..t] = right
+ let found_errors =
+ list.zip(list.flatten(left), list.flatten(right))
+ |> list.filter(fn(tup) { tup.1 != tup.0 })
+ |> list.length
+ case found_errors == errors {
+ True -> Ok(list.length(left))
+ False -> do_is_symmetric([h, ..left], t, errors)
}
}
-fn get_symmetry_type(xs: List(List(String)), errors: Int) {
- result.or(
- xs
- |> is_symmetric(errors, 1)
- |> result.map(Horizontal(_)),
- xs
- |> list.transpose()
- |> is_symmetric(errors, 1)
- |> result.map(Vertical(_)),
- )
+fn get_symmetry_type(xss: List(List(String)), errors: Int) {
+ case is_symmetric(xss, errors) {
+ Ok(n) -> Horizontal(n)
+ _ -> {
+ let assert Ok(n) = is_symmetric(list.transpose(xss), errors)
+ Vertical(n)
+ }
+ }
}
fn summarize_notes(symmetries: List(SymmetryType)) {
@@ -55,7 +58,6 @@ fn solve(input: String, errors: Int) {
|> list.map(string.to_graphemes)
|> get_symmetry_type(errors)
})
- |> result.values
|> summarize_notes
|> string.inspect
}