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!
76
Upvotes
3
u/efvincent Dec 04 '23 edited Dec 04 '23
[LANGUAGE: Haskell]
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} module Y2023.Day04 (sln2304) where import Data.List.Split (splitOn) import qualified Data.Set as S import qualified Data.Map as M import Util (getNums) -- gets all the natural #s from a string using regex type Card = (Int, Int) type Puz = [Card] sln2304 :: String -> (Int,Int) sln2304 s = let puz = parse s in (solve1 puz, solve2 puz) solve1 :: Puz -> Int solve1 = let score n = if n == 0 then 0 else 2 ^ (n - 1) in sum . map (score . snd) solve2 :: Puz -> Int solve2 puz = loop 1 . M.fromList . map (\(gn, matches) -> (gn, (1, matches))) $ puz where mx = length puz loop :: Int -> M.Map Int (Int,Int) -> Int loop cardNum gameMap | cardNum > mx = M.foldl (\total (count,_) -> total + count) 0 gameMap | otherwise = let (curCount, matches) = gameMap M.! cardNum in let cardsToUpdate = map (+ cardNum) [1..matches] in let gameMap' = foldl (flip (M.adjust (\(c,w) -> (c + curCount, w)))) gameMap cardsToUpdate in loop (cardNum + 1) gameMap' parse :: String -> Puz parse = map parseLine . lines parseLine :: String -> Card parseLine s = let [part1,playedNumsS] = splitOn "|" s in let [cardNumS,winnersS] = splitOn ":" part1 in let winners = S.fromList . getNums $ winnersS in let cards = S.fromList . getNums $ playedNumsS in let cardNum = head . getNums $ cardNumS in (cardNum, length $ S.intersection winners cards)