diff options
author | H.J <thechairman@thechairman.info> | 2024-06-03 17:16:30 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-06-03 17:16:30 -0400 |
commit | 23c7ca166ac87a339c6ee6ed71aed41afefb5513 (patch) | |
tree | 904bea816aa94775084dc08ea740ac692112184a /aoc2017-gleam/src/aoc_2017/day_7.gleam | |
parent | a679186bed8e5e284604fab7ef8ac932b66d4d51 (diff) | |
download | gleam_aoc-23c7ca166ac87a339c6ee6ed71aed41afefb5513.tar.gz gleam_aoc-23c7ca166ac87a339c6ee6ed71aed41afefb5513.zip |
gleam 2017 up to day 7 part 1
Diffstat (limited to 'aoc2017-gleam/src/aoc_2017/day_7.gleam')
-rw-r--r-- | aoc2017-gleam/src/aoc_2017/day_7.gleam | 42 |
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" +} |