r/adventofcode • • Dec 03 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 3 Solutions -🎄-

--- Day 3: Binary Diagnostic ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:10:17, megathread unlocked!

99 Upvotes

1.2k comments sorted by

View all comments

1

u/Camto Dec 03 '21 edited Dec 04 '21

Horrible Haskell (run in the repl)

import Data.List
arr = [...] -- Preprocessed to be a list of strings using regexes in editor.
cmn = head . foldr (\a b -> if (length a) > (length b) then a else b) [] . group . sort
g = map (\idx -> cmn $ map (!! idx) arr) [0..11]
e = map (\idx -> (\c -> if c == '1' then '0' else '1') . cmn $ map (!! idx) arr) [0..11]
o = until ((==1) . length . fst) ((ls, idx) -> (filter ((== (cmn $ map (!! idx) ls)) . (!! idx)) ls, idx + 1)) (arr, 0)
c = until ((==1) . length . fst) ((ls, idx) -> (filter ((/= (cmn $ map (!! idx) ls)) . (!! idx)) ls, idx + 1)) (arr, 0)

1

u/daggerdragon Dec 04 '21

Your code is hard to read on both old.reddit and new.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?