aboutsummaryrefslogtreecommitdiff
path: root/aoc2017-gleam/src/aoc_2017/day_1.gleam
blob: 786d0ddccd44eb8ed820ed8a6e0e6dd6487a2665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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()
}