diff options
Diffstat (limited to 'aoc2019_gleam/src/aoc_2019/day_1.gleam')
-rw-r--r-- | aoc2019_gleam/src/aoc_2019/day_1.gleam | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/aoc2019_gleam/src/aoc_2019/day_1.gleam b/aoc2019_gleam/src/aoc_2019/day_1.gleam new file mode 100644 index 0000000..7e23172 --- /dev/null +++ b/aoc2019_gleam/src/aoc_2019/day_1.gleam @@ -0,0 +1,32 @@ +import gleam/int +import gleam/list +import gleam/result +import gleam/string + +pub fn parse(input: String) -> List(Int) { + input + |> string.split("\n") + |> list.map(int.parse) + |> result.values() +} + +pub fn pt_1(input: List(Int)) { + input + |> list.fold(0, fn(total, next) { total + naive_fuel(next) }) +} + +pub fn pt_2(input: List(Int)) { + input + |> list.fold(0, fn(total, next) { total + recursive_fuel(next) }) +} + +fn naive_fuel(weight: Int) -> Int { + { weight / 3 } - 2 +} + +fn recursive_fuel(weight: Int) -> Int { + case { weight / 3 } - 2 { + n if n <= 0 -> 0 + n -> n + recursive_fuel(n) + } +} |