aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/days/day01.gleam
blob: ac465814b04ce53b0f847997e9a48455d1ced948 (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
33
34
import gleam/io
import gleam/int
import gleam/list
import gleam/result
import ext/resultx
import util/input_util

fn solve(numbers: List(Int), n: Int) -> Int {
  numbers
  |> list.combinations(by: n)
  |> list.find(one_that: fn(p) { int.sum(p) == 2020 })
  |> result.map(with: int.product)
  |> resultx.force_unwrap
}

fn part1(numbers: List(Int)) -> Int {
  solve(numbers, 2)
}

fn part2(numbers: List(Int)) -> Int {
  solve(numbers, 3)
}

pub fn run() -> Nil {
  let test = input_util.read_numbers("test01")
  assert 514_579 = part1(test)
  assert 241_861_950 = part2(test)

  let input = input_util.read_numbers("day01")
  io.debug(part1(input))
  io.debug(part2(input))

  Nil
}