From b86a291b6a8c128a4d9592672eae858dca642b32 Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:53 -0400 Subject: [PATCH] 2015 day 16 --- 2015/README.md | 3 +- 2015/lib/2015/16.ex | 79 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 2015/lib/2015/16.ex diff --git a/2015/README.md b/2015/README.md index b6e8f1a..9a2933d 100644 --- a/2015/README.md +++ b/2015/README.md @@ -16,7 +16,7 @@ | :--: | :--: | :-: | :-: | :-: | :-: | :-: | | | | [1] | [2] | [3] | [4] | [5] | | [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 | | [1]: ./lib/2015/1.ex @@ -34,3 +34,4 @@ [13]: ./lib/2015/13.ex [14]: ./lib/2015/14.ex [15]: ./lib/2015/15.ex +[16]: ./lib/2015/16.ex diff --git a/2015/lib/2015/16.ex b/2015/lib/2015/16.ex new file mode 100644 index 0000000..a850988 --- /dev/null +++ b/2015/lib/2015/16.ex @@ -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