-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.hs
executable file
·329 lines (292 loc) · 8.25 KB
/
10.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env stack
{- stack --resolver=nightly-2018-11-14 script --compile
--package "ansi-terminal containers here megaparsec mtl pretty-show safe scanf split time"
-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
import Control.Monad.State
import Debug.Trace
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.List
import Data.List.Split
import qualified Data.Map as M
import Data.Maybe
import Data.Ord
import qualified Data.Sequence as S
import Data.String.Here
import Data.Time.Calendar
import Data.Time.Clock
import Debug.Trace
import System.Console.ANSI
import System.Environment
import System.IO
import System.Exit
import qualified Text.Megaparsec as P
import qualified Text.Megaparsec.Char as P
import Text.Printf
import qualified Text.Scanf as SC (fmt,printf,scanf)
import Text.Show.Pretty
pp :: Show a => a -> IO ()
pp = putStrLn . ppShow
ltrace :: Show a => String -> a -> a
ltrace msg x =
trace (msg++": "++show x)
x
iterateN :: Int -> (a -> a) -> (a -> a)
iterateN n f = (!! n) . iterate f -- n must be positive
first3 (x,_,_) = x
second3 (_,x,_) = x
third3 (_,_,x) = x
first4 (x,_,_,_) = x
second4 (_,x,_,_) = x
third4 (_,_,x,_) = x
fourth4 (_,_,_,x) = x
{-
see 10.txt
KBJHEZCB
10369
-}
test = [here|
position=< 9, 1> velocity=< 0, 2>
position=< 7, 0> velocity=<-1, 0>
position=< 3, -2> velocity=<-1, 1>
position=< 6, 10> velocity=<-2, -1>
position=< 2, -4> velocity=< 2, 2>
position=<-6, 10> velocity=< 2, -2>
position=< 1, 8> velocity=< 1, -1>
position=< 1, 7> velocity=< 1, 0>
position=<-3, 11> velocity=< 1, -2>
position=< 7, 6> velocity=<-1, -1>
position=<-2, 3> velocity=< 1, 0>
position=<-4, 3> velocity=< 2, 0>
position=<10, -3> velocity=<-1, 1>
position=< 5, 11> velocity=< 1, -2>
position=< 4, 7> velocity=< 0, -1>
position=< 8, -2> velocity=< 0, 1>
position=<15, 0> velocity=<-2, 0>
position=< 1, 6> velocity=< 1, 0>
position=< 8, 9> velocity=< 0, -1>
position=< 3, 3> velocity=<-1, 1>
position=< 0, 5> velocity=< 0, -1>
position=<-2, 2> velocity=< 2, 0>
position=< 5, -2> velocity=< 1, 2>
position=< 1, 4> velocity=< 2, 1>
position=<-2, 7> velocity=< 2, -2>
position=< 3, 6> velocity=<-1, -1>
position=< 5, 0> velocity=< 1, 0>
position=<-6, 0> velocity=< 2, 0>
position=< 5, 9> velocity=< 1, -2>
position=<14, 7> velocity=<-2, 0>
position=<-3, 6> velocity=< 2, -1>
|] & parsePoints
parsePoints s = s
& lines
& map (SC.scanf [SC.fmt|position=<%d, %d> velocity=<%d, %d>|])
& catMaybes
& map (SC.printf [SC.fmt|((%d,%d), (%d,%d))|])
& map read :: [Point]
testresults = [here|
........#.............
................#.....
.........#.#..#.......
......................
#..........#.#.......#
...............#......
....#.................
..#.#....#............
.......#..............
......#...............
...#...#.#...#........
....#..#..#.........#.
.......#..............
...........#..#.......
#...........#.........
...#.......#..........
......................
......................
..........#....#......
........#.....#.......
..#.........#......#..
......................
......#...............
....##.........#......
......#.#.............
.....##.##..#.........
........#.#...........
........#...#.....#...
..#...........#.......
....#.....#.#.........
......................
......................
......................
......................
......................
..............#.......
....#..#...####..#....
......................
........#....#........
......#.#.............
.......#...#..........
.......#..#..#.#......
....#....#.#..........
.....#...#...##.#.....
........#.............
......................
......................
......................
......................
......................
......................
......................
......#...#..###......
......#...#...#.......
......#...#...#.......
......#####...#.......
......#...#...#.......
......#...#...#.......
......#...#...#.......
......#...#..###......
......................
......................
......................
......................
......................
......................
......................
............#.........
........##...#.#......
......#.....#..#......
.....#..##.##.#.......
.......##.#....#......
...........#....#.....
..............#.......
....#......#...#......
.....#.....##.........
...............#......
...............#......
......................
......................
|]
& splitOn "\n\n"
& map (S.fromList . map S.fromList . lines) :: [Sky]
type X = Int
type Y = Int
type VX = Int
type VY = Int
type Point = ((X,Y),(VX,VY))
type Sky = S.Seq (S.Seq Char)
skyw = 22
skyh = 16
defsky = S.replicate skyh $ S.replicate skyw '.'
sky w h = S.replicate h $ S.replicate w '.'
ox =skyw`div`2
oy =skyh`div`2
myx = ox-5
myy = oy-4
printSky :: Sky -> IO ()
printSky = putStr . unlines . map toList . toList
printSkies :: [Sky] -> IO ()
printSkies = mapM_ (\(i,s) -> print i >> printSky s) . zip [0..]
pointsDimensions :: [Point] -> (Int,Int)
pointsDimensions ps = (xhi - xlo, yhi - ylo)
where
(xs,ys) = (map (fst . fst) ps, map (snd . fst) ps)
(xlo,xhi) = (minimum xs, maximum xs)
(ylo,yhi) = (minimum ys, maximum ys)
pointsTopLeft :: [Point] -> (Int,Int)
pointsTopLeft ps = (minimum xs, minimum ys)
where
(xs,ys) = (map (fst . fst) ps, map (snd . fst) ps)
drawPoints :: Double -> (Int,Int) -> [Point] -> Sky
drawPoints extrascale (translatex,translatey) ps =
let
(w,h) = pointsDimensions ps
xscale = fromIntegral draww / (fromIntegral w) * extrascale
yscale = fromIntegral drawh / (fromIntegral h) * extrascale
(x,y) = pointsTopLeft ps
s = sky draww drawh
in
snd $ flip runState s $ do
mapM_ (drawPoint . scalePoint xscale yscale . translatePoint translatex translatey) ps
get
draww = 160
drawh = 40
translatePoint :: Int -> Int -> Point -> Point
translatePoint dx dy ((x,y),v) = ((x+dx,y+dy),v)
scalePoint :: Double -> Double -> Point -> Point
scalePoint sx sy ((x,y),(vx,vy)) =
((scale sx x, scale sy y),(scale sx vx, scale sy vy))
where
scale s x = round $ s * fromIntegral x
drawPoint :: Point -> State Sky ()
drawPoint ((x,y),(_,_)) = do
s <- get
put $ S.adjust' (S.update (myx+x) '#') (myy+y) s
updatePoints :: Int -> [Point] -> [Point]
updatePoints vscale = map (movePoint vscale)
movePoint :: Int -> Point -> Point
movePoint vscale ((x,y),(vx,vy)) = ((x+vx*vscale,y+vy*vscale),(vx,vy))
-- t0 = head testresults
-- p0 = test
-- pss = take 4 $ iterate updatePoints p0
main1 = do
-- putStrLn "t0"
-- printSky t0
-- putStrLn "p0"
-- printSky $ drawPoints p0
-- printSkies $ map drawPoints $ p0:pss
[inputfile,vscale',startidx'] <- getArgs
let [vscale,startidx] = map read [vscale',startidx']
input <- readFile inputfile
let
ps0 = parsePoints input
pss = iterate' (updatePoints vscale) ps0
s = 1 -- drawing scale
t = (0,0) -- drawing translation
present i s t@(tx,ty) = do
clearScreen
setCursorPosition 0 0
let ps = pss !! i
printSky $ drawPoints s t ps
printf "time:%4d scale:%10.6f translate:%s points dimensions:%s\n" i s (show t) (show $ pointsTopLeft ps)
putStrLn $ intercalate " " [
"j/k:next/prev"
,"J/K:next/prev fast"
,"0:reset time"
,"-/+:zoom"
,"1:reset zoom"
,"w/x/a/d:pan"
,"W/X/A/D:pan fast"
,"q: quit"
]
c <- getChar
case c of
'q' -> exitSuccess
-- prev/next
'k' -> present (max 0 (i-1)) s t
'j' -> present (i+1) s t
'K' -> present (max 0 (i-100)) s t
'J' -> present (i+100) s t
'0' -> present 0 s t
-- scale
'-' -> present i (s*0.9) t
'=' -> present i (s*1.1) t
'1' -> present i 1 t
-- translation
'w' -> present i s (tx ,ty+1 )
'x' -> present i s (tx ,ty-1 )
'a' -> present i s (tx+1 ,ty )
'd' -> present i s (tx-1 ,ty )
'W' -> present i s (tx ,ty+1000)
'X' -> present i s (tx ,ty-1000)
'A' -> present i s (tx+1000,ty )
'D' -> present i s (tx-1000,ty )
--
_ -> present i s t
hSetBuffering stdin NoBuffering
present startidx s t