1
0
Fork 0

2015 day 16

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent 0de9cc93cd
commit b86a291b6a
2 changed files with 81 additions and 1 deletions

View file

@ -16,7 +16,7 @@
| :--: | :--: | :-: | :-: | :-: | :-: | :-: | | :--: | :--: | :-: | :-: | :-: | :-: | :-: |
| | | [1] | [2] | [3] | [4] | [5] | | | | [1] | [2] | [3] | [4] | [5] |
| [6] | [7] | [8] | [9] | [10] | [11] | [12] | | [6] | [7] | [8] | [9] | [10] | [11] | [12] |
| [13] | [14] | [15] | 16 | 17 | 18 | 19 | | [13] | [14] | [15] | [16]| 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | | | 20 | 21 | 22 | 23 | 24 | 25 | |
[1]: ./lib/2015/1.ex [1]: ./lib/2015/1.ex
@ -34,3 +34,4 @@
[13]: ./lib/2015/13.ex [13]: ./lib/2015/13.ex
[14]: ./lib/2015/14.ex [14]: ./lib/2015/14.ex
[15]: ./lib/2015/15.ex [15]: ./lib/2015/15.ex
[16]: ./lib/2015/16.ex

79
2015/lib/2015/16.ex Normal file
View file

@ -0,0 +1,79 @@
import AOC
aoc 2015, 16 do
@moduledoc """
children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1
"""
def known_properties() do
"""
children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1
"""
|> String.split("\n", trim: true)
|> Enum.map(&String.split(&1, ": "))
|> Enum.map(fn [l, v] -> {l, String.to_integer(v)} end)
end
def get_value(sue, label) do
[value] = Regex.run(~r/#{label}: (\d+)/, sue, capture: :all_but_first)
value |> String.to_integer()
end
def p1 do
input_stream()
|> Stream.filter(fn sue ->
known_properties()
|> Enum.filter(fn {label, _} -> String.contains?(sue, label) end)
|> Enum.all?(fn {label, value} ->
if String.contains?(sue, label) do
String.contains?(sue, "#{label}: #{value}")
else
true
end
end)
end)
|> Stream.take(1)
|> Enum.to_list()
|> hd()
end
def p2 do
input_stream()
|> Stream.filter(fn sue ->
known_properties()
|> Enum.filter(fn {label, _} -> String.contains?(sue, label) end)
|> Enum.all?(fn
{label, value} when label in ~w[cats trees] ->
IO.inspect(label, label: "label")
IO.inspect(value, label: "value")
IO.inspect(get_value(sue, label), label: "get_value(sue, label)")
value < get_value(sue, label)
{label, value} when label in ~w[goldfish pomeranians] ->
value > get_value(sue, label)
{label, value} ->
String.contains?(sue, "#{label}: #{value}")
end)
end)
|> Enum.to_list()
end
end