aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-gleam/src/day6/solve.gleam
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
committerH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
commit8777ff071f7bb37631baa7b6717ad29961e50911 (patch)
tree6d59c4ed58e454b960339c3d1151f0a879e8d7cb /aoc2023-gleam/src/day6/solve.gleam
parent6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff)
downloadgleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz
gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip
sorting by language
Diffstat (limited to 'aoc2023-gleam/src/day6/solve.gleam')
-rw-r--r--aoc2023-gleam/src/day6/solve.gleam85
1 files changed, 0 insertions, 85 deletions
diff --git a/aoc2023-gleam/src/day6/solve.gleam b/aoc2023-gleam/src/day6/solve.gleam
deleted file mode 100644
index 88044c4..0000000
--- a/aoc2023-gleam/src/day6/solve.gleam
+++ /dev/null
@@ -1,85 +0,0 @@
-import adglent.{First, Second}
-import gleam/io
-import gleam/string
-import gleam/int
-import gleam/list
-import gleam/result
-
-type Race {
- Race(time: Int, distance: Int)
-}
-
-fn parse_with_bad_kerning(input: String) {
- input
- |> string.split("\n")
- |> list.map(fn(str) {
- str
- |> string.split(" ")
- |> list.map(int.parse)
- |> result.values
- })
- |> list.transpose
- |> list.map(fn(ns) {
- let assert [t, d] = ns
- Race(t, d)
- })
-}
-
-fn find_bound(race: Race, button_time: Int, step: Int) {
- let travel_time = race.time - button_time
- case button_time * travel_time > race.distance {
- True -> button_time
- False -> find_bound(race, button_time + step, step)
- }
-}
-
-fn lower_bound(race: Race) {
- find_bound(race, 1, 1)
-}
-
-fn upper_bound(race: Race) {
- find_bound(race, race.time, -1)
-}
-
-pub fn part1(input: String) {
- {
- use acc, race <- list.fold(parse_with_bad_kerning(input), 1)
- acc * { upper_bound(race) - lower_bound(race) + 1 }
- }
- |> string.inspect
-}
-
-fn parse_properly(input: String) {
- input
- |> string.replace(" ", "")
- |> string.split("\n")
- |> list.flat_map(string.split(_, ":"))
- |> list.map(int.parse)
- |> result.values
-}
-
-pub fn part2(input: String) {
- let assert [time, distance] =
- input
- |> parse_properly
-
- let race = Race(time, distance)
-
- upper_bound(race) - lower_bound(race) + 1
- |> string.inspect
-}
-
-pub fn main() {
- let assert Ok(part) = adglent.get_part()
- let assert Ok(input) = adglent.get_input("6")
- case part {
- First ->
- part1(input)
- |> adglent.inspect
- |> io.println
- Second ->
- part2(input)
- |> adglent.inspect
- |> io.println
- }
-}