aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/days/day01.gleam
blob: 0d2a9949278eeb347528459588b2b4440bc8805e (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 as res
import ext/resultx as resx
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 })
  |> res.map(with: int.product)
  |> resx.assert_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
}