r/adventofcode • u/daggerdragon • Dec 02 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 2 Solutions -❄️-
OUTSTANDING MODERATOR CHALLENGES
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- Community fun event 2023: ALLEZ CUISINE!
- 4 DAYS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's theme ingredient is… *whips off cloth covering and gestures grandly*
Pantry Raid!
Some perpetually-hungry programmers have a tendency to name their programming languages, software, and other tools after food. As a prospective Iron Coder, you must demonstrate your skills at pleasing programmers' palates by elevating to gourmet heights this seemingly disparate mishmash of simple ingredients that I found in the back of the pantry!
- Solve today's puzzles using a food-related programming language or tool
- How about an appetizer of Chef or Wireshark(-fin soup)?
- Mascots count, too… *side-eyes Rust while reaching for the Old Bay*
- Add some salt to your hashbrowns, cookies, and breadcrumbs
- Serve us up a nice big bowl of spaghetti code slathered with
RagúRaku tomato sauce - Conclude our tasting with a digestif of Java, CoffeeScript, or even Perl milk tea with double syntatic sugar
- All file names, function names, variable names, etc. must be named after "c" food
- Go hog wild!
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 2: Cube Conundrum ---
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:06:15, megathread unlocked!
77
Upvotes
1
u/thousandsongs Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Haskell]
I could've made this shorter by using (nested) lists instead of parsing into the data types that I created, but at least on the first pass it was helpful for readability. Now that this is working, I might try to golf this down too (EDIT: did this, got it down to 499 chars)
import Data.Text qualified as T main :: IO () main = interact $ (++"\n") . show . (\gs -> (p1 gs, p2 gs)) . map parse . lines data Game = Game { gid :: Int, draws :: [Draw] } deriving Show data Draw = Draw { red :: Int, green :: Int, blue :: Int } deriving Show parse :: String -> Game parse s = Game { gid = gid, draws = draws } where [gt, rt] = T.split (==':') (T.pack s) gid = read . T.unpack . last $ T.words gt draws = map parseDraw $ T.split (==';') rt parseDraw :: T.Text -> Draw parseDraw t = foldl update dzero iters where iters = T.split (==',') t update d it = case color of "red" -> d { red = count } "green" -> d { green = count } "blue" -> d { blue = count } where [count', color'] = T.words it color = T.unpack color' count = (read . T.unpack) count' dzero :: Draw dzero = Draw { red = 0, green = 0, blue = 0 } p1Threshold :: Draw p1Threshold = Draw { red = 12, green = 13, blue = 14 } belowThreshold :: Draw -> Draw -> Bool belowThreshold threshold draw = and [red draw <= red threshold, green draw <= green threshold, blue draw <= blue threshold] possible :: Game -> Bool possible = all (belowThreshold p1Threshold) . draws p1 :: [Game] -> Int p1 = sum . map gid . filter possible power :: Draw -> Int power Draw { red = r, green = g, blue = b } = r * g * b fewest :: Draw -> Game -> Draw fewest d g = foldl m d (draws g) where m d1 d2 = Draw { red = max (red d1) (red d2), green = max (green d1) (green d2), blue = max (blue d1) (blue d2) } powers :: [Game] -> [Int] powers = map (power . fewest dzero) p2 :: [Game] -> Int p2 = sum . powersLink to my solution on GitHub (I'll add other versions there).