Last active
December 2, 2023 23:28
-
-
Save rugyoga/dbd354e105d447e3220d9c056b765609 to your computer and use it in GitHub Desktop.
Advent of Code 2023 Day 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AOC | |
import String, only: [split: 2, to_integer: 1] | |
import Enum, only: [map: 2, max: 1, product: 1, reduce: 3, sum: 1, zip_with: 2] | |
aoc 2023, 2 do | |
def parse_game(line) do | |
["Game " <> id, hands] = split(line, ": ") | |
{to_integer(id), hands |> split("; ") |> map(&parse_hand/1)} | |
end | |
def parse_hand(hand) do | |
hand |> split(", ") |> reduce([0,0,0], &parse_count/2) | |
end | |
def parse_count(count, [red, green, blue]) do | |
case split(count, " ") do | |
[n, "red"] -> [to_integer(n), green, blue] | |
[n, "green"] -> [red, to_integer(n), blue] | |
[n, "blue"] -> [red, green, to_integer(n)] | |
end | |
end | |
def common(input, f) do | |
input |> split("\n") |> map(&(&1 |> parse_game() |> f.())) |> sum() | |
end | |
def p1(input) do | |
common( | |
input, | |
fn {id, games} -> | |
[r, g, b] = zip_with(games, &max/1) | |
if(r <= 12 and g <= 13 and b <= 14, do: id, else: 0) | |
end) | |
end | |
def p2(input) do | |
common(input, fn {_, hands} -> hands |> zip_with(&max/1) |> product() end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment