From 57dab498064dedaf082b88adeee68c2001d407d5 Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Sun, 6 Dec 2020 11:42:48 +0900 Subject: [PATCH] Move alternative Day5 impl to separate files --- day5/README | 12 +++++++++--- day5/day5part1-bitstring.exs | 19 +++++++++++++++++++ day5/day5part1.exs | 12 ------------ day5/day5part2-bitstring.exs | 27 +++++++++++++++++++++++++++ day5/day5part2.exs | 12 ------------ 5 files changed, 55 insertions(+), 27 deletions(-) create mode 100644 day5/day5part1-bitstring.exs create mode 100644 day5/day5part2-bitstring.exs diff --git a/day5/README b/day5/README index ea138dd..efd104c 100644 --- a/day5/README +++ b/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 ** -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: + + <> = for <>, 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. 🦾 diff --git a/day5/day5part1-bitstring.exs b/day5/day5part1-bitstring.exs new file mode 100644 index 0000000..6ab58ab --- /dev/null +++ b/day5/day5part1-bitstring.exs @@ -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 + <> = for <>, into: <<>>, do: char_to_bit(char) + id + end +end + +Day5Part1.run() diff --git a/day5/day5part1.exs b/day5/day5part1.exs index 28a9ac9..6e36cf2 100644 --- a/day5/day5part1.exs +++ b/day5/day5part1.exs @@ -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) - <> = for <>, into: <<>>, do: char_to_bit(char) - id - end end Day5Part1.run() diff --git a/day5/day5part2-bitstring.exs b/day5/day5part2-bitstring.exs new file mode 100644 index 0000000..d76225f --- /dev/null +++ b/day5/day5part2-bitstring.exs @@ -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 + <> = for <>, into: <<>>, do: char_to_bit(char) + id + end +end + +Day5Part2.run() diff --git a/day5/day5part2.exs b/day5/day5part2.exs index e4d4127..53cce2a 100644 --- a/day5/day5part2.exs +++ b/day5/day5part2.exs @@ -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) - <> = for <>, into: <<>>, do: char_to_bit(char) - id - end end Day5Part2.run()