From: Neil Smith Date: Sun, 3 Dec 2023 11:13:55 +0000 (+0000) Subject: Better definitions of touching X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;ds=sidebyside;h=ff385a27338f254cfb5d75912959338e62430167;hp=b3a89bb2f720c65da9c9866219a47914c2e3dd06;p=advent-of-code-23.git Better definitions of touching --- diff --git a/advent03/Main.hs b/advent03/Main.hs index f164bad..062b66e 100644 --- a/advent03/Main.hs +++ b/advent03/Main.hs @@ -25,7 +25,9 @@ main = text <- readFile dataFileName let engine = mkEngine text let allNums = findNumbers engine - let partNums = filter (numTouchingSymbols engine) allNums + let symbols = findSymbols engine + let partNums = filter (touchRegion symbols) allNums + -- print partNums -- print engine -- print $ findNumbers engine print $ part1 engine partNums @@ -37,8 +39,8 @@ part1 engine partNums = sum partNumValues where partNumValues = map (readNumber engine) partNums part2 engine partNums = sum $ fmap product gearRatios - where sts = stars engine - touchingStars = fmap (adjacentNumbers partNums) sts + where stars = findStars engine + touchingStars = fmap (possibleGears partNums) stars gears = filter ((==) 2 . length) touchingStars gearRatios = [[readNumber engine n | n <- pNs] | pNs <- gears] @@ -82,22 +84,22 @@ neighbours :: Position -> [Position] neighbours p = [p ^+^ V2 dr dc | dr <- [-1..1], dc <- [-1..1] , (dr, dc) /= (0, 0) ] -touchingSymbol :: Engine -> Position -> Bool -touchingSymbol engine p = any (isEngineSymbol . (engine !)) nbrs - where nbrs = filter (inRange (bounds engine)) $ neighbours p -numTouchingSymbols :: Engine -> NumPos -> Bool -numTouchingSymbols engine ps = any (touchingSymbol engine) ps +touchPoint :: [Position] -> Position -> Bool +touchPoint region point = not $ null $ intersect region $ neighbours point + +touchRegion :: [Position] -> [Position] -> Bool +touchRegion region1 region2 = any (touchPoint region2) region1 + readNumber :: Engine -> NumPos -> Int readNumber engine ps = read $ map (engine !) ps -stars :: Engine -> [Position] -stars engine = filter ((==) '*' . (engine !)) $ indices engine +findSymbols :: Engine -> [Position] +findSymbols engine = filter (isEngineSymbol . (engine !)) $ indices engine -starTouchesNumber :: Position -> NumPos -> Bool -starTouchesNumber star ps = not $ null $ intersect ps halo - where halo = neighbours star +findStars :: Engine -> [Position] +findStars engine = filter ((==) '*' . (engine !)) $ indices engine -adjacentNumbers :: [NumPos] -> Position -> [NumPos] -adjacentNumbers nums star = filter (starTouchesNumber star) nums +possibleGears :: [NumPos] -> Position -> [NumPos] +possibleGears nums star = filter (flip touchPoint star) nums