aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-gleam/src/day13/solve.gleam
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
committerH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
commit8777ff071f7bb37631baa7b6717ad29961e50911 (patch)
tree6d59c4ed58e454b960339c3d1151f0a879e8d7cb /aoc2023-gleam/src/day13/solve.gleam
parent6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff)
downloadgleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz
gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip
sorting by language
Diffstat (limited to 'aoc2023-gleam/src/day13/solve.gleam')
-rw-r--r--aoc2023-gleam/src/day13/solve.gleam87
1 files changed, 0 insertions, 87 deletions
diff --git a/aoc2023-gleam/src/day13/solve.gleam b/aoc2023-gleam/src/day13/solve.gleam
deleted file mode 100644
index 6f9b9a0..0000000
--- a/aoc2023-gleam/src/day13/solve.gleam
+++ /dev/null
@@ -1,87 +0,0 @@
-import adglent.{First, Second}
-import gleam/io
-import gleam/list
-import gleam/string
-import gleam/bool
-
-type SymmetryType {
- Horizontal(Int)
- Vertical(Int)
-}
-
-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(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)) {
- use acc, note <- list.fold(symmetries, 0)
- acc
- + case note {
- Horizontal(n) -> 100 * n
- Vertical(n) -> n
- }
-}
-
-fn solve(input: String, errors: Int) {
- input
- |> string.split("\n\n")
- |> list.map(fn(strs) {
- strs
- |> string.split("\n")
- |> list.map(string.to_graphemes)
- |> get_symmetry_type(errors)
- })
- |> summarize_notes
- |> string.inspect
-}
-
-pub fn part1(input: String) {
- solve(input, 0)
-}
-
-pub fn part2(input: String) {
- solve(input, 1)
-}
-
-pub fn main() {
- let assert Ok(part) = adglent.get_part()
- let assert Ok(input) = adglent.get_input("13")
- case part {
- First ->
- part1(input)
- |> adglent.inspect
- |> io.println
- Second ->
- part2(input)
- |> adglent.inspect
- |> io.println
- }
-}