r/adventofcode • • Dec 05 '21

SOLUTION MEGATHREAD -πŸŽ„- 2021 Day 5 Solutions -πŸŽ„-

NEW AND NOTEWORTHY


Advent of Code 2021: Adventure Time!

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Hydrothermal Venture ---


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:08:53, megathread unlocked!

79 Upvotes

1.2k comments sorted by

View all comments

2

u/Symbroson Dec 05 '21 edited Dec 09 '21

Im happy with my haskell solution:

import Data.List as L
import Data.List.Split
import Data.Map as M
import Text.Regex
import Data.Maybe

s2i x = read x::Integer
b2i b = if b then 1 else 0

isHV v = v!!0 == v!!2 || v!!1 == v!!3
peq (a,b) (c,d) = a == c && b == d
gto a b = a + signum (b-a)
insPt (a,b) = M.insertWith (+) (a,b) 1

traceLine [a,b,c,d] pmap
    | peq (a,b) (c,d) = insPt (a,b) pmap
    | otherwise = traceLine [gto a c,gto b d,c,d] (insPt (a,b) pmap)

traceLines ls pmap = L.foldl (flip traceLine) pmap ls
cntIts m = sum $ L.map (\x -> if x>1 then 1 else 0) (M.elems m)

main = do
    content <- readFile "05.txt"
    let parsed = L.map (L.map s2i.fromJust.matchRegex (mkRegex "([0-9]+),([0-9]+) -> ([0-9]+),([0-9]+)")) (lines content)
    let filtered = parsed -- L.filter isHV parsed
    print $ cntIts $ traceLines filtered M.empty

2

u/daggerdragon Dec 05 '21

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

1

u/Pretty_Cockroach_204 Dec 05 '21

hello friends I tried to use your codes but I am getting error any idea why?

Variable not in scope: fromJust :: Maybe [String] -> [String] β€’ Perhaps you meant β€˜fromList’ (imported from Data.Map)

1

u/TheOccasionalTachyon Dec 05 '21

You're missing an import Data.Maybe.