From 569983751a3afe8f9ced5b4140ab7b2c246f2c4f Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 15 Dec 2023 21:55:17 -0800 Subject: [PATCH] Use parallelism --- solutions/solutions.cabal | 3 ++- solutions/src/2023/16.hs | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/solutions/solutions.cabal b/solutions/solutions.cabal index c0d8bf8..eff4009 100644 --- a/solutions/solutions.cabal +++ b/solutions/solutions.cabal @@ -1063,4 +1063,5 @@ executable sln_2023_15 executable sln_2023_16 import: day main-is: 2023/16.hs - build-depends: array + build-depends: array, parallel + ghc-options: -threaded \ No newline at end of file diff --git a/solutions/src/2023/16.hs b/solutions/src/2023/16.hs index 0e9051f..3372285 100644 --- a/solutions/src/2023/16.hs +++ b/solutions/src/2023/16.hs @@ -36,7 +36,8 @@ module Main (main) where import Advent (getInputArray, arrIx, ordNub) import Advent.Coord (east, invert, invert', north, origin, south, west, coordCol, coordRow, Coord(C)) -import Advent.Search (bfs) +import Advent.Search (bfsOn) +import Control.Parallel.Strategies (parMap, rpar) import Data.Array.Unboxed (inRange, bounds, UArray ) -- | Parse the input grid and print answers to both parts. @@ -48,11 +49,16 @@ main :: IO () main = do input <- getInputArray 2023 16 print (solve input (origin, east)) - print (maximum (map (solve input) (edges (bounds input)))) + print (maximum (parMap rpar (solve input) (edges (bounds input)))) -- | Count the number of energized tiles given an input beam. solve :: UArray Coord Char -> (Coord, Coord) -> Int -solve input = length . ordNub . map fst . bfs (step input) +solve input = length . ordNub . map fst . bfsOn rep (step input) + +-- | Use a more compact representative of the state space to speed +-- up the visited test. This saves about a 3rd of the runtime as without. +rep :: (Coord, Coord) -> Int +rep (C y x, C dy dx) = y * 4096 + x * 16 + (dy+1) * 2 + (dx+1) -- | Find all the incoming light possibilities for part 2 edges :: (Coord, Coord) -> [(Coord, Coord)]