Day 7, Part 1
This commit is contained in:
parent
1b5a8ee458
commit
67aabf4d63
19
Day7/puzzle1.hs
Normal file
19
Day7/puzzle1.hs
Normal file
@ -0,0 +1,19 @@
|
||||
import Data.List (transpose)
|
||||
import Data.List.Split (splitOn)
|
||||
|
||||
type Equation = (Int, [Int])
|
||||
|
||||
isSolvable :: Int -> Equation -> Bool
|
||||
isSolvable cur (result, []) = cur == result
|
||||
isSolvable cur (result, [x]) = cur + x == result || cur * x == result
|
||||
isSolvable cur (result, x:y:ys) = isSolvable (cur + x + y) (result, ys)
|
||||
|| isSolvable ((cur + x) * y) (result, ys)
|
||||
|| isSolvable (cur * x + y) (result, ys)
|
||||
|| isSolvable (cur * x * y) (result, ys)
|
||||
|
||||
main = do
|
||||
[x, y] <- transpose . map (splitOn ":") . lines <$> readFile "day7.txt"
|
||||
let results = map read x
|
||||
values = map read <$> map words y
|
||||
equations = zip results values
|
||||
print . sum . map fst $ filter (isSolvable 0) equations
|
Loading…
x
Reference in New Issue
Block a user