Day 03
Mix.install([
{:kino, "~> 0.6.2"},
{:kino_aoc, git: "https://github.com/ljgago/kino_aoc"}
])
:ok
Input
{:ok, result} = KinoAOC.download_puzzle("2022", "3", System.fetch_env!("LB_AOC_SESSION"))
input = result
"gfWpjRRQffQGCHHJsGqjsj\nSclzJZZvmmnPbJtVSqqNBqVCBdSCsd\ntlbvZJDZtmtPcJmlPnhMFQWWpMRFTfLDRRTWRp\nHjMPgSWjVrjgbHRRSSMRgjRdpdbGdlcdCvQfcCdlwQJfdf\nLNDnhtNtLNFFZDtFnhzvdldDflvvDCdlJfldpJ\nZFLFZZmFtFtTNTSPRrVPWWMpRP\nqLBSBLRwmgzqCbzCffDlrfCV\nTFFFHNWFMFFMpHpGHMTHGNhrldWZCsdZsslZlZfrflDVss\nPTMcPGntTThHhTGctnMvSwjjvmmqLBmnjqqgCR\nnClJtMwwntqVVPJcgZqq\nmjpsDcrcSSFFPZqFBWWgVP\nvQcjsvhrvvrmhbmNHMNnlHbNMtCtNM\nbgvvhnTQtjrrrhsDDf\npLSMltLzLLSjFrSSjrSJHD\nzNWRLBdZPllPQtCvttgCqb\nDRlDrrFTNDNlgzsGTBfcnqhhcnJfcrCSqc\nMMmmdWtdLmvtldHjMmQfPBqSJWnfCCCqcWSSPJ\nvjHMjLmjpLtHptQLmHvwTRgNVVpTzZFZgZRlsVTN\nrzpMpDCGFCFFjRFsRPFRNFPv\nfWclbHCHtSmfvjnmfsvZ\nwTcTlSwwtQtWclBQBLGMLMCLVzVLwJGqLd\nMQSjLNjPPLLSBPjfQhSPHjDVCjDtVVpDHwbwVpbD\nRcmWzsRrzZrmTszWRqWlmRJscbtHwCbndCtcDVddDpdnVnbt\nJTsrGGTqmwTlWmTzJzWmhhPLLGgPFgBffSSPhFFM\nqMMRNZMDDNWLPqfzCgDcGncVDCgG\nwwBFhwhhBgmcVzhghG\ntbJbjjtJvwtdtwjpFtlbvtdTLNSMqNqMMgqNHPlZRTNggL\nqmjMHsZmZSbjbZMjSLFF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RcDrs\nllZmgCZqgCFgmdRdJcscBdJsmQ\nFZlgfqCFfgZHlqCMCglwCFGWntLLSMRSPGPVttWRtVGL\nvtnDsDtrnrSvrMVmbrrJg" <> ...
Part 1
defmodule RuckSack do
def find_common(first, second) do
MapSet.intersection(MapSet.new(first), MapSet.new(second))
|> MapSet.to_list()
end
def to_priority(char) do
val = :binary.first(char)
cond do
val >= 97 and val <= 122 -> val - 96
val >= 65 and val <= 90 -> val - 38
true -> val
end
end
end
defmodule PartOne do
def resolve(input) do
IO.puts("--- Part One ---")
IO.puts("Result: #{run(input)}")
end
def run(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(fn x ->
x = String.split(x, "", trim: true)
half = (length(x) / 2) |> round()
Enum.chunk_every(x, half)
end)
|> Enum.map(fn x -> RuckSack.find_common(Enum.at(x, 0), Enum.at(x, 1)) end)
|> List.flatten()
|> Enum.map(&RuckSack.to_priority/1)
|> Enum.sum()
end
end
{:module, PartOne, <<70, 79, 82, 49, 0, 0, 12, ...>>, {:run, 1}}
input
|> PartOne.resolve()
--- Part One ---
Result: 7845
:ok
ExUnit.start(autorun: false)
defmodule PartOneTest do
use ExUnit.Case, async: true
import PartOne
test "part one" do
input = "vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw"
result = run(input)
assert result == 157
end
end
ExUnit.run()
.
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 0 failures
Randomized with seed 67809
%{excluded: 0, failures: 0, skipped: 0, total: 1}
Part 2
defmodule RuckSack do
def find_common(first, second, third) do
MapSet.intersection(MapSet.new(first), MapSet.new(second))
|> MapSet.intersection(MapSet.new(third))
|> MapSet.to_list()
end
def to_priority(char) do
val = :binary.first(char)
cond do
val >= 97 and val <= 122 -> val - 96
val >= 65 and val <= 90 -> val - 38
end
end
end
defmodule PartTwo do
def resolve(input) do
IO.puts("--- Part One ---")
IO.puts("Result: #{run(input)}")
end
def run(input) do
input
|> String.split("\n", trim: true)
|> Enum.chunk_every(3)
|> Enum.map(fn x ->
Enum.map(x, fn x -> String.split(x, "", trim: true) end)
end)
|> Enum.map(fn x -> RuckSack.find_common(Enum.at(x, 0), Enum.at(x, 1), Enum.at(x, 2)) end)
|> List.flatten()
|> Enum.map(&RuckSack.to_priority/1)
|> Enum.sum()
end
end
{:module, PartTwo, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:run, 1}}
input
|> PartTwo.resolve()
--- Part One ---
Result: 2790
:ok
ExUnit.start(autorun: false)
defmodule PartTwoTest do
use ExUnit.Case, async: true
import PartTwo
test "part one" do
input = "vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw"
result = run(input)
assert result == 70
end
end
ExUnit.run()
.
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 0 failures
Randomized with seed 135085
%{excluded: 0, failures: 0, skipped: 0, total: 1}