aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src/day2
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-02 00:58:28 -0500
committerHJ <thechairman@thechairman.info>2023-12-02 00:58:28 -0500
commit2e7a0fd7ce39296ec9056bc4edda45f8c28146f6 (patch)
tree492745b0ae7829e5ab133649a65686d8f7d367ed /aoc2023/src/day2
parente06c1b7ee377fbb09066bc05472757759a3b6435 (diff)
downloadgleam_aoc-2e7a0fd7ce39296ec9056bc4edda45f8c28146f6.tar.gz
gleam_aoc-2e7a0fd7ce39296ec9056bc4edda45f8c28146f6.zip
day 2 complete
Diffstat (limited to 'aoc2023/src/day2')
-rw-r--r--aoc2023/src/day2/solve.gleam45
1 files changed, 43 insertions, 2 deletions
diff --git a/aoc2023/src/day2/solve.gleam b/aoc2023/src/day2/solve.gleam
index 5f16a12..916704c 100644
--- a/aoc2023/src/day2/solve.gleam
+++ b/aoc2023/src/day2/solve.gleam
@@ -1,12 +1,53 @@
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 [_, 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 [n_str, color] = string.split(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)
+ }
+}
pub fn part1(input: String) {
- todo as "Implement solution to part 1"
+ 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) {
- todo as "Implement solution to part 2"
+ {
+ 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() {