2 Commits

Author SHA1 Message Date
be6c3d37ea Minor cleanup
Avoid repeatedly creating sets that live for almost no time, as well as
some readability fixes
2025-12-07 22:38:39 +01:00
ff2c86a11b Missing compilation instructions 2025-12-07 22:26:25 +01:00
2 changed files with 23 additions and 10 deletions

15
2025/day07/README.md Normal file
View File

@@ -0,0 +1,15 @@
# Day 07: Haskell
This one took me the longest, mostly because I really do not know Haskell. Nevertheless, I find the
resulting solution quite elegant. The `Makefile` I made uses dynamic linkage as that's how Haskell
works on Arch Linux. If you want to do static linking, `ghc solve.hs` should work.
```console
$ make
ghc -dynamic -O -g -o solve solve.hs
[1 of 2] Compiling Main ( solve.hs, solve.o )
[2 of 2] Linking solve
$ ./solve sample.txt
21
40
```

View File

@@ -1,7 +1,6 @@
import Data.Array
import Data.List
import Data.Maybe
import Data.Set
import System.Environment
import System.Exit
@@ -13,18 +12,17 @@ stringToArray s = listArray (0, length s - 1) s
run :: String -> IO ()
run input =
let gridLines = lines input
startingPoint = findStartingPoint (head gridLines)
initial = Data.Set.singleton startingPoint
arrLines = Data.List.map stringToArray (tail gridLines)
startingPoint = findStartingPoint $ head gridLines
arrLines = Data.List.map stringToArray $ tail gridLines
in do
print (part1 arrLines startingPoint)
print (part2 arrLines startingPoint)
print $ part1 arrLines startingPoint
print $ part2 arrLines startingPoint
findStartingPoint :: [Char] -> Int
findStartingPoint line = fromJust (elemIndex 'S' line)
findStartingPoint line = fromJust $ elemIndex 'S' line
part1 arrLines startingPoint =
let initial = Data.Set.singleton startingPoint
let initial = [startingPoint]
in simulate arrLines 0 initial
checkHit :: Array Int Char -> Int -> [Int]
@@ -35,8 +33,8 @@ checkHit line idx = case line ! idx of
simulate [] count active = count
simulate lines count active =
let arr = head lines
followUp = Data.Set.fromList $ concatMap (checkHit arr) (Data.Set.toList active)
hits = length (Data.Set.filter (\i -> arr ! i == '^') active)
followUp = nub $ concatMap (checkHit arr) active
hits = length $ Data.List.filter (\i -> arr ! i == '^') active
remainder = tail lines
in simulate remainder (hits + count) followUp