LOADING: An error occurred. Update Chrome, try Firefox, or visit this post for more details.

⚠️Reddit changed how removals work, which breaks Reveddit's website. Install the extension to track removed content:Add to chromeAdd to firefoxWhat changed?
✖︎
about reveddit
⚙F.A.Q.add-ons
r/
status
copy sharelink
[+] show filters
54
SOLUTION MEGATHREAD-🎄- 2019 Day 3 Solutions -🎄-(self.adventofcode)
submitted 6 years, 1 month ago* (edited 14 minutes after) by daggerdragon to /r/adventofcode (134.8k)
512 commentsredditother-discussionssubreddit-indexmessage modsop-focus

--- Day 3: Crossed Wires ---


Post your solution using /u/topaz2078's [paste](https://topaz....

... view full text

since 6 years, 1 month ago
5 of 5

Tip Reveddit Real-Time can notify you when your content is removed.

your account history
(check your username's removed content. why?)
Tip Check if your account has any removed comments.
view my removed comments
you are viewing a single comment's thread.
view all comments
[–][deleted]6 points6 years, 1 month ago* (edited 9 minutes after)

Haskell:
Represented each wire as a Map of points to the number of steps to reach it, and Map has handy intersection and intersectionWith functions.

import Data.List.Split (splitOn)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M

parseWire :: String -> Map (Int, Int) Int
parseWire = M.fromListWith min . flip zip [1..] . tail
            . scanl move (0, 0) . concatMap expandSteps . splitOn ","
    where expandSteps (d:ds) = replicate (read ds) d
          expandSteps [] = error "Unable to parse instr"
          move (x, y) 'U' = (x, y+1)
          move (x, y) 'D' = (x, y-1)
          move (x, y) 'L' = (x-1, y)
          move (x, y) 'R' = (x+1, y)
          move _      _   = error "Unknown direction"

part1 :: String -> Int
part1 = minimum . map (\(x, y) -> abs x + abs y) . M.keys
        . foldr1 M.intersection . map parseWire . lines

part2 :: String -> Int
part2 = minimum . M.elems . foldr1 (M.intersectionWith (+)) . map parseWire . lines
permalinkhide replies (3)as-of
[–]nirgle2 points6 years, 1 month ago

That "fromListWith min" is smart. I wasted a lot of time recursing in the State monad and checking membership in the map

permalinkparentcontexthide replies (1)author-focusas-ofpreserve
[–][deleted]1 point6 years, 1 month ago

Thanks! For what it's worth I was also recursing and checking membership before but went back and simplified after. I've found the State monad to be more overhead than it's worth for simpler iterative mutations like this.

permalinkparentcontextas-of
[–]Tarmen2 points6 years, 1 month ago

Wow, replicate + scanl is a slick way to get the step list!

permalinkparentcontextauthor-focusas-ofpreserve
[–][deleted]1 point6 years, 1 month ago

Funny how your part2 function is identical to mine but implementation of parseWire is totally different. Power of Haskell 💪

permalinkparentcontextas-of
r/revedditremoved.substack.com
🚨 NEWS 🚨
✖︎

Important: Reddit Changed How Removals Work

A recent Reddit update makes mod-removed content disappear from profile pages, which breaks Reveddit's website.

Install the browser extension to receive removal alerts.

Add to chromeAdd to firefox

What changed?

r/revedditremoved.substack.com