-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
70 lines (58 loc) · 1.59 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
import AoC
import AoC.Grid
import Control.Applicative (liftA2)
import Data.Bits (xor)
import Data.Ord (comparing)
import Data.Bifunctor
import Data.Foldable
import Data.Function ((&))
import Data.Maybe
import Data.List
import Data.List.Split
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
type Line = (V2 Int, V2 Int)
parse :: String -> Line
parse input =
let [[fx, fy], [tx, ty]] = map (map read . splitOn ",") $ splitOn " -> " input
in (v2 (fx, fy), v2 (tx, ty))
parseAll = map parse . lines
hv (f, t) = any (== 0) (f - t)
diag (f, t) = let V2 (x, y) = abs <$> f - t
in x == y
range :: Int -> Int -> [Int]
range f t | f <= t = [f..t]
| otherwise = [f,f-1..t]
lspan :: Line -> [V2 Int]
lspan v@(V2 (fx, fy), V2 (tx, ty))
| hv v = sequence $ v2 (range fx tx, range fy ty)
| diag v = map v2 $ zip (range fx tx) (range fy ty)
part1 = length
. filter (> 1)
. toList
. counter
. concat
. map lspan
. filter hv
part2 = length
. filter (> 1)
. toList
. counter
. concat
. map lspan
. filter ((||) <$> hv <*> diag)
main = main' "input.txt"
exampleMain = main' "example.txt"
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)