diff options
author | J.J <thechairman@thechairman.info> | 2024-05-30 21:49:58 -0400 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2024-05-30 21:49:58 -0400 |
commit | 231c2b688d1e6cf0846d46e883da30e042a9c6cf (patch) | |
tree | 98a6d3a461fe190b38b2cf33a708a1d01703fa70 /aoc2023-gleam/src/day2/solve.gleam | |
parent | fe088aa5778dcdbaab4dd8d4a7395a91c444b45c (diff) | |
parent | a2c2b728ec6051323ed937f54816089cd2ae9d20 (diff) | |
download | gleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.tar.gz gleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.zip |
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2023-gleam/src/day2/solve.gleam')
-rw-r--r-- | aoc2023-gleam/src/day2/solve.gleam | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/aoc2023-gleam/src/day2/solve.gleam b/aoc2023-gleam/src/day2/solve.gleam new file mode 100644 index 0000000..608955f --- /dev/null +++ b/aoc2023-gleam/src/day2/solve.gleam @@ -0,0 +1,66 @@ +import adglent.{First, Second} +import gleam/io +import gleam/int +import gleam/string +import gleam/list + +pub type Game { + Game(red: Int, blue: Int, green: Int) +} + +fn parse(input: String) -> List(List(Game)) { + use line <- list.map(string.split(input, "\n")) + let assert [_, rounds] = string.split(line, on: ": ") + use match <- list.map(string.split(rounds, on: "; ")) + use acc, draw <- list.fold( + over: string.split(match, on: ", "), + from: Game(0, 0, 0), + ) + let assert Ok(#(n_str, color)) = string.split_once(draw, " ") + let assert Ok(n) = int.parse(n_str) + case color { + "red" -> Game(..acc, red: n) + "blue" -> Game(..acc, blue: n) + "green" -> Game(..acc, green: n) + _ -> panic as "unrecognized color" + } +} + +pub fn part1(input: String) { + use acc, game, i <- list.index_fold(parse(input), 0) + case list.any(game, fn(m) { m.red > 12 || m.green > 13 || m.blue > 14 }) { + False -> acc + i + 1 + True -> acc + } +} + +pub fn part2(input: String) { + { + use game <- list.map(parse(input)) + use acc, match <- list.fold(game, Game(0, 0, 0)) + let Game(red: red, green: green, blue: blue) = match + Game( + red: int.max(red, acc.red), + blue: int.max(blue, acc.blue), + green: int.max(green, acc.green), + ) + } + |> list.fold(from: 0, with: fn(acc, g: Game) { + acc + g.red * g.blue * g.green + }) +} + +pub fn main() { + let assert Ok(part) = adglent.get_part() + let assert Ok(input) = adglent.get_input("2") + case part { + First -> + part1(input) + |> adglent.inspect + |> io.println + Second -> + part2(input) + |> adglent.inspect + |> io.println + } +} |