Day 6 - Advent of Code 2022
Mix.install([:kino, :benchee])
:ok
Links
Prompt
--- Day 6: Tuning Trouble ---
The preparations are finally complete; you and the Elves leave camp on foot and begin to make your way toward the star fruit grove.
As you move through the dense undergrowth, one of the Elves gives you a handheld device. He says that it has many fancy features, but the most important one to set up right now is the communication system.
However, because he's heard you have significant experience dealing with signal-based systems, he convinced the other Elves that it would be okay to give you their one malfunctioning device - surely you'll have no problem fixing it.
As if inspired by comedic timing, the device emits a few colorful sparks.
To be able to communicate with the Elves, the device needs to lock on to their signal. The signal is a series of seemingly-random characters that the device receives one at a time.
To fix the communication system, you need to add a subroutine to the device that detects a start-of-packet marker in the datastream. In the protocol being used by the Elves, the start of a packet is indicated by a sequence of four characters that are all different.
The device will send your subroutine a datastream buffer (your puzzle input); your subroutine needs to identify the first position where the four most recently received characters were all different. Specifically, it needs to report the number of characters from the beginning of the buffer to the end of the first such four-character marker.
For example, suppose you receive the following datastream buffer:
mjqjpqmgbljsphdztnvjfqwrcgsmlb
After the first three characters (mjq) have been received, there haven't been enough characters received yet to find the marker. The first time a marker could occur is after the fourth character is received, making the most recent four characters mjqj. Because j is repeated, this isn't a marker.
The first time a marker appears is after the seventh character arrives. Once it does, the last four characters received are jpqm, which are all different. In this case, your subroutine should report the value 7, because the first start-of-packet marker is complete after 7 characters have been processed.
Here are a few more examples:
bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character5nppdvjthqldpwncqszvftbrmjlhg: first marker after character6nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character10zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character11
How many characters need to be processed before the first start-of-packet marker is detected?
To begin, get your puzzle input.
--- Part Two ---
Your device's communication system is correctly detecting packets, but still isn't working. It looks like it also needs to look for messages.
A start-of-message marker is just like a start-of-packet marker, except it consists of 14 distinct characters rather than 4.
Here are the first positions of start-of-message markers for all of the above examples:
mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character19bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character23nppdvjthqldpwncqszvftbrmjlhg: first marker after character23nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character29zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character26
How many characters need to be processed before the first start-of-message marker is detected?
Although it hasn't changed, you can still get your puzzle input.
Input
input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"dvgdvvbpbtbhbdhbhmmmcctmcmccggtrgghnhmnnqffpcprrqssnhsnhnshhsrsqqhchcdcfcqqcncrrzpppmzpzzhjzzzvrvnnpbpzzswzwswnngjgjgcjcfcllhffjbfjfhhppvnppfmfcmmcnnmnfnzfnzffphpnnnsvswvvnwnfntftjftfvvztzqzhzddttjpjbpphhlnnwgnwggdmmczclczzqddlcdldrlrccfflwwgqwwrjwrwzrrsdrrfssddcmmvrvlvhvfhfzhzbhbzzdmdvvsppwswmwdwjjzmzhhvghgthghvvtcthcttgsttpqpbbhthppzznntpnttshtshthhwrrgbbjzbjblblzbllcblcblbvvqfqqnjqqhfhftftwfftvvmddgzzmdzmdmtttttqmqmnqmqpmqpqqjlldpllfvlffjdfjjvlvjjjjbsjbjnnhmnhnrrwtrrfvvtppwmmpnnjbnjjcncjcttrcrjjlqjljccpzccvqvzzscctgccsmmnznhzhnnjggmjgmgjgtgtlgglhhrlhhrvhvrvtvvlvvwmmrjjzqqbzqqtgtzgttmgggwpgwgbwggwwprwwvswszznczncnccjnccjdjdjssqmmgcmcjmmhwhswhwdwrddtccmzmjmsmrmrddhrhhqbbspbsppqvqmvqmmclcddzjzvvslsffdhhqgqrqjrqjqjrrmpmrppcjpjffwhfwwppqvvzjzqqtftgfghgpglgnglldlvvwzwmmfzzhzmzhhswhhnvhvphpmphphrrwwfccmgmqgmmzgmgngznnzbzttjwttgngzzdndffwhfwhhlmllwqlwwgfgfpfnnldnldndbdcdddmbdmbbdpbpwbpptrrpggtrtrnttsmmcrmmlhltlgttblttbhttjzzvnnsbsqbqwbbtwtmtbbcjcljjslscswwglgqlgqgmmcpctcrttprpjjfvjfjqqljlvjljnnfhhfzhzmhmghhjnhjjbppmqqwlwswwgbbzszspzsppcscvvnhndhdwdmdrdssqrqdqsddnbnpplqppjbjdbdqdcccffsnntzntnmnlnbbrzzzvgvvqttbbzpbzpzdpdvppjmjpjbbdrbbgwgffzpzzjddmldlwwjdwwscwcfccnznmmfttvvmpmvpmvmcvmmjmwjwbbwqwjjzqjzjbbjqbjqqqthhsssmrsshvvprvvwccjlcctwwmgwgpwgpgcpptrppjppltlhthctttfptppwgpwprpbbqzbzznssfcsffbnnndffqmmqwmmpvvpvdppjqqttfpttjccbnbddvtdvddlsstjtccphchsswbbrqqgtqgqpqjqnjjvccwscwssjwjttqvtvmtmjjghghccpnpsplslmmfjfjtffgtgrtrftfhttpsttzzmhmllwqwhhnffdpffwbwnnbrrfbrrnjrjlljqqnjqnqqwlqqbbbtvvwgvgbgddpwdwvdwvdvvwzvwwtzwttppnttqccjtjrttqbbprpsrpssfjsffmdfmfbmbggjhggcjgcgvvjqqbhbllhrlhlclzzplpqqdtddrhhfssdvsvrrscsjjnwjjfpjfffcbfcfmmlpmphhhlpltptcpttppfssppgwppjgjnnblbhbtbrbgbpplnlvlsvlvwvwpptmtsswzswsnscsfcsfsfggzhghvvgbbfddgbbrmrtrqtthvhzhzthtbtjjcljjlcllzglzzdszdzjzjfzzzblbslbljjdnndvvnwnqnwqqscscmczcwwvrrlclttbtdtvddbrddvllnppvpmvmbbvfvttfcttggwgffrfwwwpwcpchppcrrgprgprgrjgjdgdgvddzndzdbzzswsvvtgtccrrnggbwggpjpnnsjjwfjjqfqvffrrhdrrsfrfjjfzfdzzqfqwbztszjqtttfdqvzmznzjlsjnwdthtwdtfslgdmgfpwsqcsqdhnsnsmghttfvlzqgspzdtlstdmthzftwmnqrznldpmwqbtthggjwcgjjmbpqgrnwspggjvrlcmtvpchmqhlwwtswqgpdjpbznqnssqhdjzgbjnfmgssrvnmmcvvhgmcvqbfdhgrhnqqzdmttmdzwgtprzqhplwnhhmlrvcbwpllqprtltdvqrwhvwzvlqsvfqsfjwmrnzlqpdgfpmtfdczqdnfjjbjmrdnffcmtwlzcmvnwmlpmqhvggdhptnzlvzwzwjbcszsnzgpwncfgvzfgbzwclvrbmllzpltzwjrftmppsfwhvvvhvqjtstnnczgtdbmpjjsscbdwplftgcgmtrnrnzplzhghrqgdtjwntwfstjwqjjrlhtwhnfqwfqgsjptjfpsrbnvvlgsltnvtfvscttwvrfzblzmfmnfrlrnzrrzhclggtntpjbbcphdvrfhnrtzvdmwbwgbftgzwlcqztghdhdmzwlmjbgptfnnzbmwsnzlzcpprqzmbbdsplmhpgmzthqcsfjcnfbfvsdsqzfvfcnpgqsvpgwsdbgjmsglrwmfjfpddczwvgdppfmrtszbtfdwbmlmzhqvvwmvlzvjfpffjnhwwhssfjnbzlqwqvjbjbhfntmhgswntdpbzlwwfbdbhrfhzfjsjbtlrqhlnrpfbwtpmrfvbhlmmsgtvcmrqmdpwvhqfqpgmfgnfrbvprhprtnpzjcnltndfsvjgndwblhwphtpsmnczgbtpwdvjsrctjbvbfslvslzlwbtstqvgcrqmfphwztpjqdmvcjpjqmjbdndfpzwsfwplchsmqwwbggptjdtztszmpfwgfwnqpdwfcpgrrhmfglsctjllflfltbcfvcpfcnqbwrvzmcmjpwptgsrlbrdchngwsdstfmcbrqvdsvvbnppdmnfwcgvpjjzqwcpvqfncvqlsfnjzprvhpgqscshqwsttdrsmqjfwlhcwlvnzvgvclqfjdgctvsrbwzflcldmrwlfhbgdtstqsqlblndnpgqlfbjzslcpcwvdwdffshhrzvhqwdsdmwtmtvcnrhmstvrnscppmbpmjbfjhljmsjnbjlhjhmnmcvvfgbdrblwbzrcctrjwjjwjtgnfjhhqbsmdjvdrdjtjbscfrsljnvqjlgjwqrvfmdttsvqjwdbswdtcfqsrpbvzrbsdqlqfjlrgcwzbqtqrpsrfcmbzcvjngcsmvqlbnghllcqcztbtvdrfcmpgfdprghsmbjvzbdnrdqnjdzslclgdsqglvpvcjpzqfwztlssljtmcdfcqdnqzwcttvpqfdpvzlhjfvvsgphgqrmzppvnjznqmdzfnfztjppstjfwddftcpcjnssznqbrvlvrzfhbvsjrwghttwlwfrptsvsrwfnvjtthwrppbngbgqvbsdgcrjcwjjljcwptrvgmbjpjtdbmhmzcfhzbsbrmzhdsrjbbmnwbsntpffdrrlgcrcgbcfwvlpmrzvsmvpjthtdjdvcspdsdvshlrwzcqnjmcnrgzbqzhfzbmtrvzzmjwbnjggtrtgcsnrmzbtjzgdmffdntspdhgnvgrmpbtnsspcqhsrvppjbrmdbggjbftnnbrgdsmdscqthdzflldfnplqccthpwccsfsnstttwztqnmnfshntqngmcndbsbftmgnhhwjvhchdfqzzgpdnfgvnjzjzfdzvsvtdqqcftrvmdcszcwpfrbcsmlqqfprrjgncwcvcngmrnwntcvzzlnwrhrznnldslhqdscbgsrqnvnmdtqvlttwqljmvbpbfldtbgzhvwzghnhwrwdqphhhgjpnmtlcmvfbdffnsvcswtmffzsrvczbntfpdsmwbqphvvcflpwgsrmjhrljlvzdgrcwpfphmvtwqwhjmrvmjzjlzlbflhzrdrzcdwhblpqwjljbvprddtvnccmchgctncwbpnmlqppfmhwchvjvpmblqhccfhlprdrczdfhmnsqhddbqlppgsnvhhfrwhqhfdpvsfcvzbqhgswtmnpmzrwsvnmztnqwdrhllssmgtzbztsprpsj"
Solution
defmodule Day06 do
defdelegate parse(input), to: __MODULE__.Input
def part1(input) do
input |> parse() |> find_marker(4)
end
def part2(input) do
input |> parse() |> find_marker(14)
end
def find_marker(char_list, prior_chars \\ [], marker_size, index \\ 0)
def find_marker([h1 | t1], prior, n, index) when length(prior) < n do
find_marker(t1, prior ++ [h1], n, index + 1)
end
def find_marker([h1 | t1], [_ | t2] = prior, n, index) do
if set_size(prior) < n,
do: find_marker(t1, t2 ++ [h1], n, index + 1),
else: index
end
def set_size(list), do: list |> MapSet.new() |> MapSet.size()
defmodule Input do
def parse(input) when is_binary(input) do
input |> String.to_charlist()
end
end
end
{:module, Day06, <<70, 79, 82, 49, 0, 0, 12, ...>>,
{:module, Day06.Input, <<70, 79, 82, ...>>, {:parse, 1}}}
How many characters need to be processed before the first start-of-packet marker is detected?
Your puzzle answer was 1965.
Day06.part1(input)
1965
How many characters need to be processed before the first start-of-message marker is detected?
Your puzzle answer was 2773.
Day06.part2(input)
2773
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
Benchmarking
defmodule Benchmark do
defdelegate set_size(list), to: Day06
def list_recursion(input, n) do
Day06.find_marker(input, n)
end
def chunk(input, n, module) do
input
|> module.chunk_every(n, 1)
|> module.with_index(n)
|> Enum.find(fn {list, _} ->
n == set_size(list)
end)
|> elem(1)
end
def enum_slice(input, n) do
n..length(input)
|> Enum.find(fn i ->
n ==
input
|> Enum.slice((i - n)..(i - 1))
|> set_size()
end)
end
end
{:module, Benchmark, <<70, 79, 82, 49, 0, 0, 12, ...>>, {:enum_slice, 2}}
# https://github.com/bencheeorg/benchee
input = Day06.Input.parse(input)
Benchee.run(
%{
"list_recursion" => fn n -> Benchmark.list_recursion(input, n) end,
"enum_chunk" => fn n -> Benchmark.chunk(input, n, Enum) end,
"stream_chunk" => fn n -> Benchmark.chunk(input, n, Stream) end,
"enum_slice" => fn n -> Benchmark.enum_slice(input, n) end
},
memory_time: 2,
reduction_time: 2,
inputs: %{
"Part 1" => 4,
"Part 2" => 14
}
)
nil
Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 10
Available memory: 16 GB
Elixir 1.14.1
Erlang 25.1.2
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: Part 1, Part 2
Estimated total run time: 1.47 min
Benchmarking enum_chunk with input Part 1 ...
Benchmarking enum_chunk with input Part 2 ...
Benchmarking enum_slice with input Part 1 ...
Benchmarking enum_slice with input Part 2 ...
Benchmarking list_recursion with input Part 1 ...
Benchmarking list_recursion with input Part 2 ...
Benchmarking stream_chunk with input Part 1 ...
Benchmarking stream_chunk with input Part 2 ...
##### With input Part 1 #####
Name ips average deviation median 99th %
list_recursion 5.70 K 175.41 μs ±11.00% 172.25 μs 215.33 μs
stream_chunk 4.68 K 213.74 μs ±6.24% 211.33 μs 238.05 μs
enum_chunk 2.24 K 447.10 μs ±13.87% 425.04 μs 649.15 μs
enum_slice 0.33 K 2993.49 μs ±2.02% 2968.00 μs 3196.44 μs
Comparison:
list_recursion 5.70 K
stream_chunk 4.68 K - 1.22x slower +38.34 μs
enum_chunk 2.24 K - 2.55x slower +271.69 μs
enum_slice 0.33 K - 17.07x slower +2818.08 μs
Memory usage statistics:
Name Memory usage
list_recursion 0.43 MB
stream_chunk 1.20 MB - 2.76x memory usage +0.77 MB
enum_chunk 1.47 MB - 3.40x memory usage +1.04 MB
enum_slice 0.66 MB - 1.52x memory usage +0.22 MB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
list_recursion 27.20 K
stream_chunk 78.42 K - 2.88x reduction count +51.23 K
enum_chunk 121.36 K - 4.46x reduction count +94.17 K
enum_slice 1993.70 K - 73.31x reduction count +1966.50 K
**All measurements for reduction count were the same**
##### With input Part 2 #####
Name ips average deviation median 99th %
list_recursion 1.61 K 0.62 ms ±7.74% 0.60 ms 0.73 ms
stream_chunk 1.36 K 0.74 ms ±3.86% 0.73 ms 0.80 ms
enum_chunk 0.84 K 1.19 ms ±9.97% 1.16 ms 1.55 ms
enum_slice 0.160 K 6.25 ms ±1.99% 6.20 ms 6.64 ms
Comparison:
list_recursion 1.61 K
stream_chunk 1.36 K - 1.18x slower +0.115 ms
enum_chunk 0.84 K - 1.92x slower +0.57 ms
enum_slice 0.160 K - 10.06x slower +5.63 ms
Memory usage statistics:
Name Memory usage
list_recursion 1.34 MB
stream_chunk 2.84 MB - 2.12x memory usage +1.50 MB
enum_chunk 3.18 MB - 2.38x memory usage +1.84 MB
enum_slice 1.66 MB - 1.24x memory usage +0.32 MB
**All measurements for memory usage were the same**
Reduction count statistics:
Name average deviation median 99th %
list_recursion 37.51 K ±0.00% 37.51 K 37.51 K
stream_chunk 165.11 K ±0.00% 165.11 K 165.11 K
enum_chunk 223.39 K ±0.02% 223.38 K 223.48 K
enum_slice 3961.35 K ±0.00% 3961.35 K 3961.35 K
Comparison:
list_recursion 37.51 K
stream_chunk 165.11 K - 4.40x reduction count +127.60 K
enum_chunk 223.39 K - 5.96x reduction count +185.88 K
enum_slice 3961.35 K - 105.61x reduction count +3923.85 K
nil
MapSet.size vs Enum.uniq.length
Benchee.run(
%{
"MapSet.size" => fn input -> MapSet.new(input) |> MapSet.size() end,
"Enum.uniq.length" => fn input -> Enum.uniq(input) |> length() end
},
inputs: %{
"1: 10" => Enum.to_list(1..10),
"2: 1000" => Enum.to_list(1..1000),
"3: 100_000" => Enum.to_list(1..100_000)
},
memory_time: 2,
reduction_time: 2
)
nil
Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 10
Available memory: 16 GB
Elixir 1.14.1
Erlang 25.1.2
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: 1: 10, 2: 1000, 3: 100_000
Estimated total run time: 1.10 min
Benchmarking Enum.uniq.length with input 1: 10 ...
Benchmarking Enum.uniq.length with input 2: 1000 ...
Benchmarking Enum.uniq.length with input 3: 100_000 ...
Benchmarking MapSet.size with input 1: 10 ...
Benchmarking MapSet.size with input 2: 1000 ...
Benchmarking MapSet.size with input 3: 100_000 ...
##### With input 1: 10 #####
Name ips average deviation median 99th %
MapSet.size 1.73 M 578.41 ns ±3594.61% 417 ns 625 ns
Enum.uniq.length 1.41 M 709.62 ns ±3393.79% 583 ns 750 ns
Comparison:
MapSet.size 1.73 M
Enum.uniq.length 1.41 M - 1.23x slower +131.20 ns
Memory usage statistics:
Name Memory usage
MapSet.size 1.36 KB
Enum.uniq.length 2.46 KB - 1.81x memory usage +1.10 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
MapSet.size 164
Enum.uniq.length 202 - 1.23x reduction count +38
**All measurements for reduction count were the same**
##### With input 2: 1000 #####
Name ips average deviation median 99th %
MapSet.size 13.57 K 73.68 μs ±6.35% 72.58 μs 90.29 μs
Enum.uniq.length 12.33 K 81.09 μs ±6.72% 80.46 μs 94.33 μs
Comparison:
MapSet.size 13.57 K
Enum.uniq.length 12.33 K - 1.10x slower +7.41 μs
Memory usage statistics:
Name Memory usage
MapSet.size 3.25 KB
Enum.uniq.length 283.48 KB - 87.23x memory usage +280.23 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name average deviation median 99th %
MapSet.size 1.37 K ±1.18% 1.37 K 1.42 K
Enum.uniq.length 5.94 K ±0.00% 5.94 K 5.94 K
Comparison:
MapSet.size 1.37 K
Enum.uniq.length 5.94 K - 4.34x reduction count +4.57 K
##### With input 3: 100_000 #####
Name ips average deviation median 99th %
MapSet.size 81.59 12.26 ms ±5.72% 11.84 ms 13.59 ms
Enum.uniq.length 41.67 24.00 ms ±5.35% 23.41 ms 27.86 ms
Comparison:
MapSet.size 81.59
Enum.uniq.length 41.67 - 1.96x slower +11.74 ms
Memory usage statistics:
Name Memory usage
MapSet.size 0.90 MB
Enum.uniq.length 50.47 MB - 56.05x memory usage +49.57 MB
**All measurements for memory usage were the same**
Reduction count statistics:
Name average deviation median 99th %
MapSet.size 94.52 K ±1.78% 94.38 K 99.96 K
Enum.uniq.length 448.43 K ±0.07% 448.35 K 450.47 K
Comparison:
MapSet.size 94.38 K
Enum.uniq.length 448.43 K - 4.74x reduction count +353.91 K
nil
Tests
ExUnit.start(auto_run: false)
defmodule Day06Test do
use ExUnit.Case, async: false
describe "part1/1" do
test "returns expected value" do
assert Day06.part1("bvwbjplbgvbhsrlpgdmjqwftvncz") == 5
assert Day06.part1("nppdvjthqldpwncqszvftbrmjlhg") == 6
assert Day06.part1("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg") == 10
assert Day06.part1("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw") == 11
end
end
describe "part2/1" do
test "returns expected value" do
assert Day06.part2("mjqjpqmgbljsphdztnvjfqwrcgsmlb") == 19
assert Day06.part2("bvwbjplbgvbhsrlpgdmjqwftvncz") == 23
assert Day06.part2("nppdvjthqldpwncqszvftbrmjlhg") == 23
assert Day06.part2("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg") == 29
assert Day06.part2("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw") == 26
end
end
describe "Benchmarks" do
setup do
[
input: "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw",
p1_n: 4,
p1_value: 11,
p2_n: 14,
p2_value: 26
]
end
test "returns expected value for part 1", %{input: input, p1_value: x, p1_n: n} do
assert x == Benchmark.list_recursion(input, n)
assert x == Benchmark.chunk(input, n, Enum)
assert x == Benchmark.chunk(input, n, Stream)
assert x == Benchmark.enum_slice(input, n)
end
test "returns expected value for part 2", %{input: input, p2_value: x, p2_n: n} do
assert x == Benchmark.list_recursion(input, n)
assert x == Benchmark.chunk(input, n, Enum)
assert x == Benchmark.chunk(input, n, Stream)
assert x == Benchmark.enum_slice(input, n)
end
end
end
ExUnit.run()
....
Finished in 0.00 seconds (0.00s async, 0.00s sync)
4 tests, 0 failures
Randomized with seed 548399
%{excluded: 0, failures: 0, skipped: 0, total: 4}