1
0
Fork 0

solve 2023 2.1

This commit is contained in:
sloane 2023-12-02 09:37:03 -05:00
parent dd0729091d
commit 5820ac1c3b
No known key found for this signature in database
6 changed files with 106 additions and 3 deletions

View file

@ -2,7 +2,7 @@
| S | M | T | W | T | F | S | | S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| | | | | | [1] | 2 | | | | | | | [1] | [2] |
| 3 | 4 | 5 | 6 | 7 | 8 | 9 | | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 | | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
@ -10,3 +10,4 @@
[1]: ./elixir/lib/2023/1.ex [1]: ./elixir/lib/2023/1.ex
[2]: ./elixir/lib/2023/2.ex

View file

@ -1,4 +1,5 @@
# Used by "mix format" # Used by "mix format"
[ [
inport_deps: [:nimble_parsec],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
] ]

99
2023/elixir/lib/2023/2.ex Normal file
View file

@ -0,0 +1,99 @@
import AOC
aoc 2023, 2 do
import NimbleParsec
dice =
integer(min: 1)
|> ignore(string(" "))
|> choice([
string("blue"),
string("green"),
string("red")
])
|> label("die")
|> optional(ignore(string(", ")))
|> times(min: 1, max: 3)
|> label("dice")
game =
ignore(string("Game "))
|> integer(min: 1)
|> ignore(string(": "))
|> label("game")
defparsec(:game_p, game)
defparsec(:dice_p, dice)
def read_game(input) do
case game_p(input) do
{:ok, [id], rest, _, _, _} ->
dice_and_rest =
Stream.unfold(rest, fn
nil ->
nil
rest ->
case dice_p(rest) do
{:ok, dice, rest, _, _, _} ->
dice =
dice
|> Enum.chunk_every(2)
|> Enum.map(&Enum.reverse/1)
|> Enum.map(&List.to_tuple/1)
|> Enum.into(%{})
{dice, String.replace_prefix(rest, "; ", "")}
{:error, _, rest, _, _, _} ->
{rest, nil}
end
end)
|> Enum.to_list()
[rest | dice] = Enum.reverse(dice_and_rest)
{:ok, {id, dice, rest}}
_ ->
:halt
end
end
def read_games(input) do
Stream.unfold(input, fn rest ->
input =
rest
|> String.replace_prefix("\n", "")
case read_game(input) do
{:ok, {id, dice, rest}} ->
{{id, dice}, rest}
_ ->
nil
end
end)
end
def p1(input) do
red_max = 12
green_max = 13
blue_max = 14
input
|> read_games()
|> Stream.reject(fn {_id, dice} ->
dice
|> Enum.any?(fn d ->
Map.get(d, "red", 0) > red_max or Map.get(d, "green", 0) > green_max or
Map.get(d, "blue", 0) > blue_max
end)
end)
|> Stream.map(&elem(&1, 0))
|> Enum.sum()
end
def p2(_input) do
end
end

View file

@ -21,7 +21,8 @@ defmodule AdventOfCode.MixProject do
# Run "mix help deps" to learn about dependencies. # Run "mix help deps" to learn about dependencies.
defp deps do defp deps do
[ [
{:advent_of_code_utils, "~> 4.0.0"} {:advent_of_code_utils, "~> 4.0.0"},
{:nimble_parsec, "~> 1.4.0"}
] ]
end end
end end

View file

@ -1,5 +1,6 @@
%{ %{
"advent_of_code_utils": {:hex, :advent_of_code_utils, "4.0.0", "28df8cde0d6cccf95b72880ab89b00e1cb7c0492164bbf537bdf229c0953f7aa", [:mix], [{:floki, "~> 0.34", [hex: :floki, repo: "hexpm", optional: false]}, {:tz, "~> 0.26", [hex: :tz, repo: "hexpm", optional: false]}], "hexpm", "6f44c1f5b901e6a5bd7561361f3441ee7fa4ee67ea28d7fc3b2df29303d80ff4"}, "advent_of_code_utils": {:hex, :advent_of_code_utils, "4.0.0", "28df8cde0d6cccf95b72880ab89b00e1cb7c0492164bbf537bdf229c0953f7aa", [:mix], [{:floki, "~> 0.34", [hex: :floki, repo: "hexpm", optional: false]}, {:tz, "~> 0.26", [hex: :tz, repo: "hexpm", optional: false]}], "hexpm", "6f44c1f5b901e6a5bd7561361f3441ee7fa4ee67ea28d7fc3b2df29303d80ff4"},
"floki": {:hex, :floki, "0.35.2", "87f8c75ed8654b9635b311774308b2760b47e9a579dabf2e4d5f1e1d42c39e0b", [:mix], [], "hexpm", "6b05289a8e9eac475f644f09c2e4ba7e19201fd002b89c28c1293e7bd16773d9"}, "floki": {:hex, :floki, "0.35.2", "87f8c75ed8654b9635b311774308b2760b47e9a579dabf2e4d5f1e1d42c39e0b", [:mix], [], "hexpm", "6b05289a8e9eac475f644f09c2e4ba7e19201fd002b89c28c1293e7bd16773d9"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"tz": {:hex, :tz, "0.26.2", "a40e4bb223344c6fc7b74dda25df1f26b88a30db23fa6e55de843bd79148ccdb", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:mint, "~> 1.5", [hex: :mint, repo: "hexpm", optional: true]}], "hexpm", "224b0618dd1e032778a094040bc710ef9aff6e2fa8fffc2716299486f27b9e68"}, "tz": {:hex, :tz, "0.26.2", "a40e4bb223344c6fc7b74dda25df1f26b88a30db23fa6e55de843bd79148ccdb", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:mint, "~> 1.5", [hex: :mint, repo: "hexpm", optional: true]}], "hexpm", "224b0618dd1e032778a094040bc710ef9aff6e2fa8fffc2716299486f27b9e68"},
} }

View file

@ -14,7 +14,7 @@
1. [2020] **17/50** 🌟 1. [2020] **17/50** 🌟
1. [2021] **43/50** 🌟 1. [2021] **43/50** 🌟
1. [2022] **14/50** 🌟 1. [2022] **14/50** 🌟
1. [2023] **2/50** 🌟 1. [2023] **3/50** 🌟
[2015]: ./2015 [2015]: ./2015
[2017]: ./2017 [2017]: ./2017