1
0
Fork 0

solve 2015 day 12

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent abdcf1d982
commit e0b0d94df4
4 changed files with 51 additions and 6 deletions

View file

@ -15,7 +15,7 @@
| S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| | | [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 |
| 20 | 21 | 22 | 23 | 24 | 25 | |
@ -30,3 +30,4 @@
[9]: ./lib/2015/9.ex
[10]: ./lib/2015/10.ex
[11]: ./lib/2015/11.ex
[12]: ./lib/2015/12.ex

42
2015/lib/2015/12.ex Normal file
View file

@ -0,0 +1,42 @@
import AOC
aoc 2015, 12 do
def p1 do
input_stream()
|> Stream.flat_map(fn l ->
Regex.scan(~r/-?\d+/, l)
|> List.flatten()
end)
|> Stream.map(&String.to_integer/1)
|> Enum.sum()
end
def p2 do
input_string()
|> String.trim()
|> Jason.decode!()
|> count_document()
end
def count_document(document) when is_map(document) do
values = Map.values(document)
if Enum.member?(values, "red") do
0
else
document
|> Map.values()
|> Enum.map(&count_document/1)
|> Enum.sum()
end
end
def count_document(document) when is_list(document) do
document
|> Enum.map(&count_document/1)
|> Enum.sum()
end
def count_document(document) when is_number(document), do: document
def count_document(_), do: 0
end

View file

@ -23,6 +23,7 @@ defmodule AdventOfCode2015.MixProject do
[
{:advent_of_code_utils, "~> 1.0"},
{:exla, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "exla"},
{:jason, "~> 1.2"},
{:libgraph, "~> 0.13.3"},
{:nimble_parsec, "~> 1.0"},
{:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}

View file

@ -2,6 +2,7 @@
"advent_of_code_utils": {:hex, :advent_of_code_utils, "1.0.0", "e9ce8be22988e095991168e9d2dde802afc213aaae64491e9fde84dd877db41c", [:mix], [], "hexpm", "1fa5217ccb95a38ce2312dd7ea6057b0d0002a3eba7490feab704df4b8a94038"},
"elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"},
"exla": {:git, "https://github.com/elixir-nx/nx.git", "e23a678bf0ebcbbafe03f1b5ed78623f052ad486", [sparse: "exla"]},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"libgraph": {:hex, :libgraph, "0.13.3", "20732b7bafb933dcf7351c479e03076ebd14a85fd3202c67a1c197f4f7c2466b", [:mix], [], "hexpm", "78f2576eef615440b46f10060b1de1c86640441422832052686df53dc3c148c6"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.0", "b44d75e2a6542dcb6acf5d71c32c74ca88960421b6874777f79153bbbbd7dccc", [:mix], [], "hexpm", "52b2871a7515a5ac49b00f214e4165a40724cf99798d8e4a65e4fd64ebd002c1"},
"nx": {:git, "https://github.com/elixir-nx/nx.git", "e23a678bf0ebcbbafe03f1b5ed78623f052ad486", [sparse: "nx"]},