-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOC10.hs
132 lines (101 loc) · 3.16 KB
/
AOC10.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
module AOC10 where
import Data.List
import Text.Parsec
import Text.Parsec.ByteString (Parser, parseFromFile)
import Graphics.SpriteKit
newtype X = X
{ unX :: Int
} deriving (Eq, Num, Ord, Enum)
newtype Y = Y
{ unY :: Int
} deriving (Eq, Num, Ord, Enum)
instance Show X where
show x = show $ unX x
instance Show Y where
show y = show $ unY y
data Coord = Coord
{ x :: X
, y :: Y
} deriving (Eq)
data Velocity = Velocity
{ vx :: X
, vy :: Y
} deriving (Eq)
type Star = (Coord, Velocity)
instance Show Coord where
show (Coord (X x) (Y y)) = show x ++ "x" ++ show y
instance Show Velocity where
show (Velocity (X x) (Y y)) = show x ++ "x" ++ show y
instance Ord Coord where
compare (Coord (X x) (Y y)) (Coord (X x') (Y y')) =
mappend (compare x x') (compare y y')
number :: Parser Int
number = do
s <- (char '-' >> return negate) <|> (optional (char '+') >> return id)
s . read <$> many1 digit
velocityParser :: Parser Velocity
velocityParser = do
_ <- string "velocity=<" <* many space
x <- number <* string "," <* many space
y <- number <* many space <* string ">" <* many space
pure $ Velocity (X x) (Y y)
coordParser :: Parser Coord
coordParser = do
_ <- string "position=<" <* many space
x <- number <* string "," <* many space
y <- number <* many space <* string ">" <* many space
pure $ Coord (X x) (Y y)
pointParser :: Parser (Coord, Velocity)
pointParser = do
c <- coordParser
v <- velocityParser
pure (c, v)
fromRight :: b -> Either a b -> b
fromRight _ (Right b) = b
fromRight b _ = b
--input = fromRight mempty <$> parseFromFile (many pointParser) "example.txt"
input = fromRight mempty <$> parseFromFile (many pointParser) "AOC10.input"
tick :: [(Coord, Velocity)] -> [(Coord, Velocity)]
tick cvs = move <$> cvs
where
move (coord, velo) = (Coord (x coord + vx velo) (y coord + vy velo), velo)
--toPlot (time, cs) = sequence $ putStrLn <$> (\((Coord cx cy),_) -> --(show cx) ++ ", " ++ (show cy) ++ ", " ++ (show time)) <$> cs
toSprite :: Star -> Node nodeData
toSprite (Coord (X x) (Y y), _) =
whitePixel (fromIntegral x) (fromIntegral (height - y))
-- ZNNRZJXP
-- 10418
solution1 = do
start <- input
let time = 10400
skipSome = head . drop time $ iterate tick start
zero = AOC10UserData skipSome time
--sequence $ toPlot <$> sky
pure $ skyScene zero
skyScene zeroData =
(sceneWithSize (Size width height))
{ sceneHandleEvent = Just advance
, sceneUpdate = Just updateScene
, sceneData = zeroData
}
width = 500
height = 500
white = colorWithRGBA 1 1 1 1
whitePixel x y =
(spriteNodeWithColorSize white (Size 1 1)) {nodePosition = Point x y}
data AOC10UserData = AOC10UserData
{ stars :: [Star]
, time :: Int
}
updateScene scene@Scene {sceneData = d} _ =
scene
{ sceneChildren =
labelNodeWithText (" Time: " ++ show (time d)) :
(toSprite <$> stars d)
}
advance :: EventHandler AOC10UserData
advance KeyEvent {keyEventType = KeyDown} (AOC10UserData s t) =
Just $ AOC10UserData (tick s) (succ t)
advance _ _ = Nothing