mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Compare commits
2 Commits
a737a35227
...
be6c3d37ea
| Author | SHA1 | Date | |
|---|---|---|---|
| be6c3d37ea | |||
| ff2c86a11b |
15
2025/day07/README.md
Normal file
15
2025/day07/README.md
Normal 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
|
||||
```
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user