-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
56 lines (42 loc) · 1.27 KB
/
run.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
import Data.List.Split (splitOn)
type N = Int
type Row = (N, [N])
type Input = [Row]
parse :: String -> Row
parse input =
let [x, xs] = splitOn ":" input
in (read x, map read $ words xs)
parseAll :: String -> Input
parseAll = map parse . lines
type PreppedRow = (N, [(N, N)])
prep :: Row -> PreppedRow
prep (t, xs) = (t, zip xs factors)
where factors = map ((10 ^) . length . show) xs
solve :: Bool -> PreppedRow -> [()]
solve conc (t, nums) = go 0 False nums
where go v _ [] | v == t = [()]
| otherwise = []
go v canConc ((x, f):xs)
= go (x + v) True xs
++ go (x * v) True xs
++ (if conc && canConc then go (v * f + x) True xs else [])
solveAll :: Bool -> Input -> [N]
solveAll conc = map fst . filter (not . null . solve conc . prep)
part1 :: Input -> N
part1 = sum . solveAll False
part2 :: Input -> N
part2 = sum . solveAll True
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)