From d2dc8fb98ccd9078160d5652857aa12036b752b5 Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Sun, 6 Dec 2020 01:48:26 +0900 Subject: [PATCH] Add alternative Day 5 parsing solution (to README) --- day5/README | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/day5/README b/day5/README index f48fdd8..1ea9a0a 100644 --- a/day5/README +++ b/day5/README @@ -42,3 +42,19 @@ Did a lot of refactoring here but the end result, especially for part 2, is pret Even if I decided to refactor the parser, it can just be swapped in for part 2. Zipping was a nice idea. I quite enjoyed this one, maybe because it's a Saturday and I wasn't burnt out from work :-) + +** Update ** + +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 +commit code to my solutions after reading forums, so I'll add it here instead :-) 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 'FL', do: <<0::1>> + def char_to_bit(c) when c in 'BR', do: <<1::1>> + + def seat_id_bitstring(boarding_pass \\ "FBFBBFFRLR") do + <> = for <>, into: <<>>, do: char_to_bit(char) + id + end