Add Day 5 alternative parser implementation

This commit is contained in:
Adam Millerchip 2020-12-06 02:05:06 +09:00
parent d2dc8fb98c
commit 207313fd22
3 changed files with 26 additions and 8 deletions

View file

@ -48,13 +48,7 @@ I quite enjoyed this one, maybe because it's a Saturday and I wasn't burnt out f
After reading the Elixir Forum thread: https://elixirforum.com/t/advent-of-code-2020-day-5/35997/ After reading the Elixir Forum thread: https://elixirforum.com/t/advent-of-code-2020-day-5/35997/
I really had to have a go at just parsing the boarding pass directly into an integer. I said I wouldn't I really had to have a go at just parsing the boarding pass directly into an integer. I said I wouldn't
commit code to my solutions after reading forums, so I'll add it here instead :-) It converts each commit code to my solutions after reading forums, but as long as it's clearly labelled, I think it's fine.
character to a bit on the fly, and interprets the result as a 10-bit integer. 🦾
def char_to_bit(c) when c in 'FL', do: <<0::1>> It converts each character to a bit on the fly, and interprets the result as a 10-bit integer. 🦾
def char_to_bit(c) when c in 'BR', do: <<1::1>>
def seat_id_bitstring(boarding_pass \\ "FBFBBFFRLR") do
<<id::integer-10>> = for <<char <- boarding_pass>>, into: <<>>, do: char_to_bit(char)
id
end

View file

@ -27,6 +27,18 @@ defmodule Day5Part1 do
row * 8 + col row * 8 + col
end end
# ALTERNATIVE PARSER ADDED AFTER READING OTHER SUBMISSIONS
# The boarding pass is actually just a binary number, so we can parse it as such directly.
# (The *8 above is eqivalent to the 3-bit shift to the left)
def char_to_bit(c) when c in 'FL', do: <<0::1>>
def char_to_bit(c) when c in 'BR', do: <<1::1>>
def seat_id_bitstring(boarding_pass \\ "FBFBBFFRLR") do
boarding_pass = String.trim(boarding_pass)
<<id::integer-10>> = for <<char <- boarding_pass>>, into: <<>>, do: char_to_bit(char)
id
end
end end
Day5Part1.run() Day5Part1.run()

View file

@ -35,6 +35,18 @@ defmodule Day5Part2 do
row * 8 + col row * 8 + col
end end
# ALTERNATIVE PARSER ADDED AFTER READING OTHER SUBMISSIONS
# The boarding pass is actually just a binary number, so we can parse it as such directly.
# (The *8 above is eqivalent to the 3-bit shift to the left)
def char_to_bit(c) when c in 'FL', do: <<0::1>>
def char_to_bit(c) when c in 'BR', do: <<1::1>>
def seat_id_bitstring(boarding_pass \\ "FBFBBFFRLR") do
boarding_pass = String.trim(boarding_pass)
<<id::integer-10>> = for <<char <- boarding_pass>>, into: <<>>, do: char_to_bit(char)
id
end
end end
Day5Part2.run() Day5Part2.run()