From 2e7a0fd7ce39296ec9056bc4edda45f8c28146f6 Mon Sep 17 00:00:00 2001 From: HJ Date: Sat, 2 Dec 2023 00:58:28 -0500 Subject: day 2 complete --- aoc2023/src/day2/solve.gleam | 45 +++++++++++++++++++++++++++++++++++++-- aoc2023/test/day2/day2_test.gleam | 30 ++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 6 deletions(-) (limited to 'aoc2023') 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() { diff --git a/aoc2023/test/day2/day2_test.gleam b/aoc2023/test/day2/day2_test.gleam index 9a28304..6cddda1 100644 --- a/aoc2023/test/day2/day2_test.gleam +++ b/aoc2023/test/day2/day2_test.gleam @@ -4,22 +4,44 @@ import adglent.{type Example, Example} import day2/solve type Problem1AnswerType = - String + Int type Problem2AnswerType = - String + Int /// Add examples for part 1 here: /// ```gleam ///const part1_examples: List(Example(Problem1AnswerType)) = [Example("some input", "")] /// ``` -const part1_examples: List(Example(Problem1AnswerType)) = [] +const part1_examples: List(Example(Problem1AnswerType)) = [ + Example( + "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", + 8, + ), +] /// Add examples for part 2 here: /// ```gleam ///const part2_examples: List(Example(Problem2AnswerType)) = [Example("some input", "")] /// ``` -const part2_examples: List(Example(Problem2AnswerType)) = [] +const part2_examples: List(Example(Problem2AnswerType)) = [ + Example( + "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", + 2286, + ), + Example( + "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", + 48, + ), +] pub fn part1_test() { part1_examples -- cgit v1.2.3