diff options
author | H.J <thechairman@thechairman.info> | 2024-06-17 10:29:16 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-06-17 10:29:16 -0400 |
commit | df4168b0da34f8dbedcfc34e2b2ad8182a87c434 (patch) | |
tree | 547b3fee34783ca21e43a1bf99b4939fa19d10db | |
parent | 29d7ae5048297a754b157a45ad65e7bbc18dc977 (diff) | |
parent | f356385e661269d3ce21986bb058883596ac388d (diff) | |
download | gleam_aoc-df4168b0da34f8dbedcfc34e2b2ad8182a87c434.tar.gz gleam_aoc-df4168b0da34f8dbedcfc34e2b2ad8182a87c434.zip |
day 12 started
-rw-r--r-- | aoc2017-gleam/src/aoc_2017/day_12.gleam | 47 | ||||
-rw-r--r-- | aoc2017-gleam/src/aoc_2017/day_13.gleam (renamed from aoc2017-gleam/src/aoc_2017/day_11.gleam) | 0 |
2 files changed, 31 insertions, 16 deletions
diff --git a/aoc2017-gleam/src/aoc_2017/day_12.gleam b/aoc2017-gleam/src/aoc_2017/day_12.gleam index 87747d0..a9d73c5 100644 --- a/aoc2017-gleam/src/aoc_2017/day_12.gleam +++ b/aoc2017-gleam/src/aoc_2017/day_12.gleam @@ -1,29 +1,44 @@ -import gleam/dict.{type Dict} -import gleam/int +import gleam/dict import gleam/list -import gleam/result +import gleam/set.{type Set} import gleam/string type Pipes = - Dict(Int, List(Int)) + dict.Dict(String, List(String)) pub fn parse(input: String) -> Pipes { - { - use row <- list.map(string.split(input, "\n")) - let assert Ok(#(from, to)) = string.split_once(row, " <-> ") - - let assert Ok(from) = int.parse(from) - let to = to |> string.split(", ") |> list.map(int.parse) |> result.values - - #(from, to) - } - |> dict.from_list + use acc, row <- list.fold(string.split(input, "\n"), dict.new()) + let assert Ok(#(from, to)) = string.split_once(row, " <-> ") + let to_nodes = string.split(to, ", ") + dict.insert(acc, from, to_nodes) } pub fn pt_1(input: Pipes) { - input + next_nodes("0", input, set.new()) |> set.size() } pub fn pt_2(input: Pipes) { - todo as "part 2 not implemented" + count_groups(dict.keys(input), input, 0) +} + +fn next_nodes(current: String, pipes: Pipes, found: Set(String)) { + let assert Ok(to_nodes) = dict.get(pipes, current) + + use acc, node <- list.fold(to_nodes, found) + case set.contains(found, node) { + False -> acc |> set.insert(node) |> next_nodes(node, pipes, _) + True -> acc + } +} + +fn count_groups(all_nodes: List(String), pipes: Pipes, count: Int) { + case all_nodes { + [] -> count + [first, ..] -> { + let next_subgraph = next_nodes(first, pipes, set.new()) + let remaining = + list.filter(all_nodes, fn(n) { !set.contains(next_subgraph, n) }) + count_groups(remaining, pipes, count + 1) + } + } } diff --git a/aoc2017-gleam/src/aoc_2017/day_11.gleam b/aoc2017-gleam/src/aoc_2017/day_13.gleam index 7d3df0b..7d3df0b 100644 --- a/aoc2017-gleam/src/aoc_2017/day_11.gleam +++ b/aoc2017-gleam/src/aoc_2017/day_13.gleam |