aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-06-14 15:18:07 -0400
committerH.J <thechairman@thechairman.info>2024-06-14 15:18:07 -0400
commitd30f8a654da7d07fffd504b0ca063cc893bea785 (patch)
tree1294c45cf2669816851f9541b566f61670c2088d
parent10e7706062c64cc0da55587bfb1a85c988ae950c (diff)
downloadgleam_aoc-d30f8a654da7d07fffd504b0ca063cc893bea785.tar.gz
gleam_aoc-d30f8a654da7d07fffd504b0ca063cc893bea785.zip
gleam 2017 day 9
-rw-r--r--aoc2017-gleam/manifest.toml2
-rw-r--r--aoc2017-gleam/src/aoc_2017/day_9.gleam48
2 files changed, 49 insertions, 1 deletions
diff --git a/aoc2017-gleam/manifest.toml b/aoc2017-gleam/manifest.toml
index 12fa60f..e078678 100644
--- a/aoc2017-gleam/manifest.toml
+++ b/aoc2017-gleam/manifest.toml
@@ -27,7 +27,7 @@ packages = [
]
[requirements]
-gary = { version = ">= 1.0.1 and < 2.0.0"}
+gary = { version = ">= 1.0.1 and < 2.0.0" }
gladvent = { version = ">= 0.7.3 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
diff --git a/aoc2017-gleam/src/aoc_2017/day_9.gleam b/aoc2017-gleam/src/aoc_2017/day_9.gleam
new file mode 100644
index 0000000..90eb4b3
--- /dev/null
+++ b/aoc2017-gleam/src/aoc_2017/day_9.gleam
@@ -0,0 +1,48 @@
+import gleam/list
+import gleam/option.{Some}
+import gleam/regex
+import gleam/string
+
+pub fn parse(input: String) {
+ let assert Ok(cancel) = regex.from_string("!.")
+
+ replace(input, with: cancel)
+}
+
+pub fn pt_1(input: String) {
+ input
+ |> strip_to_brackets()
+ |> next_bracket(0, 1)
+}
+
+fn replace(input: String, with regex: regex.Regex) -> String {
+ input |> regex.split(with: regex) |> string.concat()
+}
+
+fn strip_to_brackets(input: String) -> String {
+ let assert Ok(garbage) = regex.from_string("<.*?>")
+ let assert Ok(not_group) = regex.from_string("[^{}]")
+
+ input
+ |> replace(with: garbage)
+ |> replace(with: not_group)
+}
+
+fn next_bracket(brackets: String, score: Int, depth: Int) -> Int {
+ case string.pop_grapheme(brackets) {
+ Error(Nil) -> score
+ Ok(#("{", rest)) -> next_bracket(rest, score + depth, depth + 1)
+ Ok(#("}", rest)) -> next_bracket(rest, score, depth - 1)
+ _ -> panic as "unrecognized character"
+ }
+}
+
+pub fn pt_2(input: String) {
+ let assert Ok(garbage) = regex.from_string("<(.*?)>")
+
+ use acc, match <- list.fold(regex.scan(input, with: garbage), 0)
+ case match.submatches {
+ [Some(g)] -> string.length(g) + acc
+ _ -> acc
+ }
+}