solve 2021 day 10
This commit is contained in:
parent
66d7e21761
commit
1beb3e6341
4 changed files with 87 additions and 7 deletions
|
@ -12,12 +12,12 @@
|
|||
```
|
||||
</details>
|
||||
|
||||
| S | M | T | W | T | F | S |
|
||||
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|
||||
| | | | [1] | [2] | [3] | [4] |
|
||||
| [5] | [6] | [7] | [8] | [9] | 10 | 11 |
|
||||
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
|
||||
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
|
||||
| S | M | T | W | T | F | S |
|
||||
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|
||||
| | | | [1] | [2] | [3] | [4] |
|
||||
| [5] | [6] | [7] | [8] | [9] | [10] | 11 |
|
||||
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
|
||||
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
|
||||
|
||||
<!-- links -->
|
||||
|
||||
|
@ -30,3 +30,4 @@
|
|||
[7]: ./lib/2021/7.ex
|
||||
[8]: ./lib/2021/8.ex
|
||||
[9]: ./lib/2021/9.ex
|
||||
[10]: ./lib/2021/10.ex
|
||||
|
|
76
2021/lib/2021/10.ex
Normal file
76
2021/lib/2021/10.ex
Normal file
|
@ -0,0 +1,76 @@
|
|||
import AOC
|
||||
|
||||
aoc 2021, 10 do
|
||||
@openers '([{<'
|
||||
@closers ')]}>'
|
||||
|
||||
def points_for_invalid_char(?)), do: 3
|
||||
def points_for_invalid_char(?]), do: 57
|
||||
def points_for_invalid_char(?}), do: 1197
|
||||
def points_for_invalid_char(?>), do: 25137
|
||||
|
||||
def points_for_missing_char(?)), do: 1
|
||||
def points_for_missing_char(?]), do: 2
|
||||
def points_for_missing_char(?}), do: 3
|
||||
def points_for_missing_char(?>), do: 4
|
||||
|
||||
def pair_for(?(), do: ?)
|
||||
def pair_for(?[), do: ?]
|
||||
def pair_for(?{), do: ?}
|
||||
def pair_for(?<), do: ?>
|
||||
|
||||
def find_line_errors(inp, state \\ [], inv \\ [])
|
||||
|
||||
def find_line_errors([], state, inv), do: {inv, state}
|
||||
|
||||
def find_line_errors([x | xs], [x | ys], inv), do: find_line_errors(xs, ys, inv)
|
||||
|
||||
def find_line_errors([x | xs], state, inv) when x in @closers,
|
||||
do: find_line_errors(xs, Enum.drop(state, 1), [x | inv])
|
||||
|
||||
def find_line_errors([x | xs], ys, inv) when x in @openers,
|
||||
do: find_line_errors(xs, [pair_for(x) | ys], inv)
|
||||
|
||||
def score_missing_charlist(charlist, score \\ 0)
|
||||
def score_missing_charlist([], score), do: score
|
||||
|
||||
def score_missing_charlist([x | xs], score),
|
||||
do: score_missing_charlist(xs, points_for_missing_char(x) + score * 5)
|
||||
|
||||
def input_charlist_stream(), do: input_stream() |> Stream.map(&String.to_charlist/1)
|
||||
|
||||
def invalid_chars_stream(),
|
||||
do:
|
||||
input_charlist_stream()
|
||||
|> Stream.map(&find_line_errors/1)
|
||||
|> Stream.reject(&match?({[], _}, &1))
|
||||
|> Stream.map(&elem(&1, 0))
|
||||
|> Stream.map(&hd/1)
|
||||
|
||||
def missing_charlists_stream(),
|
||||
do:
|
||||
input_charlist_stream()
|
||||
|> Stream.map(&find_line_errors/1)
|
||||
|> Stream.filter(&match?({[], _}, &1))
|
||||
|> Stream.reject(&match?({_, []}, &1))
|
||||
|> Stream.map(&elem(&1, 1))
|
||||
|
||||
def p1 do
|
||||
invalid_chars_stream()
|
||||
|> Stream.map(&points_for_invalid_char/1)
|
||||
|> Enum.sum()
|
||||
end
|
||||
|
||||
def p2 do
|
||||
scores =
|
||||
missing_charlists_stream()
|
||||
|> Stream.map(&score_missing_charlist/1)
|
||||
|> Enum.sort()
|
||||
|
||||
scores_count = length(scores)
|
||||
|
||||
winner = scores |> Enum.at(div(scores_count, 2))
|
||||
|
||||
winner
|
||||
end
|
||||
end
|
|
@ -29,7 +29,8 @@ defmodule AdventOfCode.MixProject do
|
|||
{:benchee, "~> 1.0", only: :dev},
|
||||
{:exla, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "exla"},
|
||||
{:nimble_parsec, "~> 1.0"},
|
||||
{:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}
|
||||
{:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true},
|
||||
{:rexbug, ">= 1.0.0"}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
"nimble_pool": {:hex, :nimble_pool, "0.2.4", "1db8e9f8a53d967d595e0b32a17030cdb6c0dc4a451b8ac787bf601d3f7704c3", [:mix], [], "hexpm", "367e8071e137b787764e6a9992ccb57b276dc2282535f767a07d881951ebeac6"},
|
||||
"nx": {:git, "https://github.com/elixir-nx/nx.git", "e23a678bf0ebcbbafe03f1b5ed78623f052ad486", [sparse: "nx"]},
|
||||
"progress_bar": {:hex, :progress_bar, "2.0.1", "7b40200112ae533d5adceb80ff75fbe66dc753bca5f6c55c073bfc122d71896d", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "2519eb58a2f149a3a094e729378256d8cb6d96a259ec94841bd69fdc71f18f87"},
|
||||
"redbug": {:hex, :redbug, "1.2.2", "366d8961770ddc7bb5d209fbadddfa7271005487f938c087a0e385a57abfee33", [:rebar3], [], "hexpm", "b5fe7b94e487be559cb0ec1c0e938c9761205d3e91a96bf263bdf1beaebea729"},
|
||||
"rexbug": {:hex, :rexbug, "1.0.5", "e4fce59d1cb4f574b2d84181507b4782bc4b6afcb64e2cd276003c563ffef766", [:mix], [{:mix_test_watch, ">= 0.5.0", [hex: :mix_test_watch, repo: "hexpm", optional: true]}, {:redbug, "~> 1.2", [hex: :redbug, repo: "hexpm", optional: false]}], "hexpm", "13a3f180a9e490686a774725a07a21caf05735b7c012824d86960e9541aab46a"},
|
||||
"telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"},
|
||||
"typed_struct": {:hex, :typed_struct, "0.2.1", "e1993414c371f09ff25231393b6430bd89d780e2a499ae3b2d2b00852f593d97", [:mix], [], "hexpm", "8f5218c35ec38262f627b2c522542f1eae41f625f92649c0af701a6fab2e11b3"},
|
||||
"xla": {:hex, :xla, "0.2.0", "689887888afb22587168d461f0e9ff83d7b06040273ea7082dbf9ff7eca33dcc", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "a2e7b81413db49a159eabfb12dbd784a7c04b5c68c7b4057238d5ec9b110f2ec"},
|
||||
|
|
Loading…
Reference in a new issue