diff options
author | H.J <thechairman@thechairman.info> | 2024-06-03 10:36:31 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-06-03 10:36:31 -0400 |
commit | 7d704786ec158349f2f34590a858df6e78e844d6 (patch) | |
tree | 2a5b0f0af3bc2c322e941d25e2da9f9e66a6cdd3 /aoc2017-gleam/src/aoc_2017 | |
parent | 9f0484222417d6b1495636123ae7de278b9fd0e5 (diff) | |
download | gleam_aoc-7d704786ec158349f2f34590a858df6e78e844d6.tar.gz gleam_aoc-7d704786ec158349f2f34590a858df6e78e844d6.zip |
gleam day 1 2017
Diffstat (limited to 'aoc2017-gleam/src/aoc_2017')
-rw-r--r-- | aoc2017-gleam/src/aoc_2017/day_1.gleam | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/aoc2017-gleam/src/aoc_2017/day_1.gleam b/aoc2017-gleam/src/aoc_2017/day_1.gleam new file mode 100644 index 0000000..786d0dd --- /dev/null +++ b/aoc2017-gleam/src/aoc_2017/day_1.gleam @@ -0,0 +1,32 @@ +import gleam/int +import gleam/list +import gleam/result +import gleam/string + +pub fn parse(input: String) { + input + |> string.to_graphemes() + |> list.map(int.parse) + |> result.values() +} + +pub fn pt_1(input: List(Int)) { + pair_by(numbers: input, considering: 1) +} + +pub fn pt_2(input: List(Int)) { + pair_by(numbers: input, considering: list.length(input) / 2) +} + +fn find_neighbor_matches(number_pairs: List(#(Int, Int))) { + case number_pairs { + [] -> 0 + [#(a, b), ..rest] if a == b -> a + find_neighbor_matches(rest) + [_, ..rest] -> find_neighbor_matches(rest) + } +} + +fn pair_by(numbers xs: List(Int), considering by: Int) { + list.zip(xs, list.append(list.drop(xs, by), list.take(xs, by))) + |> find_neighbor_matches() +} |