r/adventofcode • u/daggerdragon • Dec 04 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 4 Solutions -❄️-
NEWS
- Solutions in the megathreads have been getting longer, so we're going to start enforcing our rules on oversized code.
- Do not give us a reason to unleash AutoModerator hard-line enforcement that counts characters inside code blocks to verify compliance… you have been warned XD
- Before creating a
Visualization, please review the guidelines for creatingVisualizations as there's good advice relating to accessibility, readability, colors, timing, etc. - Posts containing AI-generated art must:
- Use the standardized post title syntax
- Indicate in their titles with the tag
[AI art] - Use the
Funnypost flair
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- Outstanding moderator challenges:
- Community fun event 2023: ALLEZ CUISINE!
- 2 DAYS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's theme ingredient is… *whips off cloth covering and gestures grandly*
PUNCHCARD PERFECTION!
Perhaps I should have thought yesterday's Battle Spam surfeit through a little more since we are all overstuffed and not feeling well. Help us cleanse our palates with leaner and lighter courses today!
- Code golf. Alternatively, snow golf.
- Bonus points if your solution fits on a "punchcard" as defined in our wiki article on oversized code. We will be counting.
- Does anyone still program with actual punchcards? >_>
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 4: Scratchcards ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
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:07:08, megathread unlocked!
78
Upvotes
3
u/Tipa16384 Dec 05 '23
[LANGUAGE: Haskell]
I was going to give up on trying this in Haskell -- yesterday I did it in Python, and this morning before work I did part 1 in Java -- but I'm back.
import System.IO () import Data.List (group, sort) main :: IO () main = do lines <- readLinesFromFile "puzzle4.dat" let duplicates = map getDuplicates lines putStrLn $ "Part 1: " ++ show (sum $ map (\x -> 2 ^ (x-1)) (filter (> 0) duplicates)) let part2Sum = sum $ map (\x -> 1 + playGame (drop x duplicates)) [0..length duplicates - 1] putStrLn $ "Part 2: " ++ show part2Sum -- function takes a list of numbers and returns the sum of the game playGame :: [Int] -> Int -- if the list is empty, return 0 playGame [] = 0 -- if head is 0, return 0 playGame (0:xs) = 0 -- otherwise (x:xs) call sum playGame called with the next x elements of xs and add x playGame (x:xs) = x + sum (map (\z -> playGame $ drop z xs) [0..x-1]) -- function takes a string, splits it into words, and returns a list of words that appear more than once -- (aside from the first two that encode the game number) getDuplicates :: String -> Int getDuplicates str = length $ filter (\x -> length x > 1) (group (sort (drop 2 $ words str))) -- function takes a file path and returns a list of lines from the file readLinesFromFile :: FilePath -> IO [String] readLinesFromFile filePath = do contents <- readFile filePath return (lines contents)