Move alternative Day5 impl to separate files
This commit is contained in:
parent
207313fd22
commit
57dab49806
5 changed files with 55 additions and 27 deletions
12
day5/README
12
day5/README
|
@ -45,10 +45,16 @@ I quite enjoyed this one, maybe because it's a Saturday and I wasn't burnt out f
|
||||||
|
|
||||||
** Update **
|
** 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
|
They use this bitstring-comprehension:
|
||||||
commit code to my solutions after reading forums, but as long as it's clearly labelled, I think it's fine.
|
|
||||||
|
<<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. 🦾
|
It converts each character to a bit on the fly, and interprets the result as a 10-bit integer. 🦾
|
||||||
|
|
||||||
|
|
19
day5/day5part1-bitstring.exs
Normal file
19
day5/day5part1-bitstring.exs
Normal 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()
|
|
@ -27,18 +27,6 @@ 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()
|
||||||
|
|
27
day5/day5part2-bitstring.exs
Normal file
27
day5/day5part2-bitstring.exs
Normal 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()
|
|
@ -35,18 +35,6 @@ 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()
|
||||||
|
|
Loading…
Reference in a new issue