aboutsummaryrefslogtreecommitdiff
path: root/aoc2017-gleam/src/aoc_2017/day_7.gleam
diff options
context:
space:
mode:
Diffstat (limited to 'aoc2017-gleam/src/aoc_2017/day_7.gleam')
-rw-r--r--aoc2017-gleam/src/aoc_2017/day_7.gleam42
1 files changed, 42 insertions, 0 deletions
diff --git a/aoc2017-gleam/src/aoc_2017/day_7.gleam b/aoc2017-gleam/src/aoc_2017/day_7.gleam
new file mode 100644
index 0000000..593282b
--- /dev/null
+++ b/aoc2017-gleam/src/aoc_2017/day_7.gleam
@@ -0,0 +1,42 @@
+import gleam/int
+import gleam/io
+import gleam/list
+import gleam/option.{None, Some}
+import gleam/regex.{type Match, Match}
+import gleam/set
+import gleam/string
+
+pub type Program {
+ Program(name: String, weight: Int, supporting: List(String))
+}
+
+pub fn parse(input: String) {
+ let assert Ok(re) = regex.from_string("([a-z]+) \\(([0-9]+)\\)(?> -> (.*))?")
+
+ use match <- list.map(string.split(input, "\n"))
+ case regex.scan(re, match) {
+ [Match(submatches: [Some(name), Some(weight)], ..)] ->
+ Program(name, to_int(weight), [])
+ [Match(submatches: [Some(name), Some(weight), Some(supporting)], ..)] ->
+ Program(name, to_int(weight), string.split(supporting, ", "))
+ _ -> panic as { "couldn't parse" <> match }
+ }
+}
+
+fn to_int(str: String) -> Int {
+ let assert Ok(n) = int.parse(str)
+ n
+}
+
+pub fn pt_1(input: List(Program)) {
+ let supporters = input |> list.map(fn(p) { p.name }) |> set.from_list()
+ let supporting =
+ input |> list.flat_map(fn(p) { p.supporting }) |> set.from_list()
+
+ let assert [base] = set.difference(supporters, supporting) |> set.to_list
+ base
+}
+
+pub fn pt_2(input: List(Program)) {
+ todo as "part 2 not implemented"
+}