Move alternative Day5 impl to separate files

This commit is contained in:
Adam Millerchip 2020-12-06 11:42:48 +09:00
parent 207313fd22
commit 57dab49806
5 changed files with 55 additions and 27 deletions

View File

@ -45,10 +45,16 @@ I quite enjoyed this one, maybe because it's a Saturday and I wasn't burnt out f
** Update **
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, so I added
simplified versions as day5part1-bitstring.exs and day5part2-bitstring.exs
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, but as long as it's clearly labelled, I think it's fine.
They use this bitstring-comprehension:
<<id::10>> = for <<char <- boarding_pass>>, into: <<>>, do: char_to_bit(char)
def char_to_bit(c) when c in 'FL', do: <<0::1>>
def char_to_bit(c) when c in 'BR', do: <<1::1>>
It converts each character to a bit on the fly, and interprets the result as a 10-bit integer. 🦾

View File

@ -0,0 +1,19 @@
defmodule Day5Part1 do
def run do
File.stream!("input")
|> Stream.map(&String.trim/1)
|> Stream.map(&seat_id/1)
|> Enum.max()
|> IO.puts()
end
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(boarding_pass) do
<<id::10>> = for <<char <- boarding_pass>>, into: <<>>, do: char_to_bit(char)
id
end
end
Day5Part1.run()

View File

@ -27,18 +27,6 @@ defmodule Day5Part1 do
row * 8 + col
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
Day5Part1.run()

View File

@ -0,0 +1,27 @@
defmodule Day5Part2 do
def run do
File.stream!("input")
|> Stream.map(&String.trim/1)
|> Stream.map(&seat_id/1)
|> Enum.sort()
|> find_seat()
|> IO.puts()
end
def find_seat([_ | next_seats] = seats) do
Stream.zip(seats, next_seats)
|> Enum.find(fn {seat, next} -> next - seat > 1 end)
|> elem(0)
|> Kernel.+(1)
end
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(boarding_pass) do
<<id::10>> = for <<char <- boarding_pass>>, into: <<>>, do: char_to_bit(char)
id
end
end
Day5Part2.run()

View File

@ -35,18 +35,6 @@ defmodule Day5Part2 do
row * 8 + col
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
Day5Part2.run()