Advent of Code 2023 Day 4 Part 1
Mix.install([
{:kino_aoc, "~> 0.1"}
])
Get Inputs
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2023", "4", System.fetch_env!("LB_SESSION"))
My answer
defmodule Resolver do
def parse(input) do
input
|> String.split("\n")
|> Enum.map(fn line ->
[_, card] = String.split(line, ":")
[winning_card, my_card] = String.split(card, "|")
%{
winning_card: parse_card(winning_card),
my_card: parse_card(my_card)
}
end)
end
defp parse_card(card) do
card
|> String.split(" ")
|> Enum.filter(fn value -> value != "" end)
|> Enum.map(fn value -> String.to_integer(value) end)
end
def resolve(games) do
games
|> Enum.map(fn %{winning_card: winning_card, my_card: my_card} ->
winning_card
|> Enum.count(fn winnig_number ->
Enum.member?(my_card, winnig_number)
end)
|> get_point()
end)
|> Enum.sum()
end
defp get_point(0), do: 0
defp get_point(1), do: 1
defp get_point(nuber_of_matches) do
0..(nuber_of_matches - 2)
|> Enum.map(&Integer.pow(2, &1))
|> Enum.sum()
|> Kernel.+(1)
end
end
games =
"""
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
"""
|> String.slice(0..-2//1)
|> Resolver.parse()
Resolver.resolve(games)
games = Resolver.parse(puzzle_input)
Resolver.resolve(games)