Skip to content

Commit 9c524b3

Browse files
committed
Update some old directories
1 parent 7c9331a commit 9c524b3

File tree

5 files changed

+140
-20
lines changed

5 files changed

+140
-20
lines changed

haskell_snippet/crawler/bas.hs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import qualified Data.ByteString.Char8 as B
2+
import Data.Tree.NTree.TypeDefs
3+
import Data.Maybe
4+
import Text.XML.HXT.Core
5+
import Control.Monad
6+
import Control.Monad.Trans
7+
import Control.Monad.Maybe
8+
import Network.HTTP
9+
import Network.URI
10+
import System.Environment
11+
import Control.Concurrent.ParallelIO
12+
13+
openUrl :: String -> MaybeT IO String
14+
openUrl url = case parseURI url of
15+
Nothing -> fail ""
16+
Just u -> liftIO (getResponseBody =<< simpleHTTP (mkRequest GET u))
17+
18+
css :: ArrowXml a => String -> a XmlTree XmlTree
19+
css tag = multi (hasName tag)
20+
21+
get :: String -> IO (IOSArrow XmlTree (NTree XNode))
22+
get url = do
23+
contents <- runMaybeT $ openUrl url
24+
return $ readString [withParseHTML yes, withWarnings no] (fromMaybe "" contents)
25+
26+
main = do
27+
url <- parseArgs
28+
doc <- get url
29+
imgs <- runX . images $ doc
30+
-- sequence_ $ map download imgs
31+
parallel_ $ map download imgs
32+
stopGlobalPool
33+
34+
convertSlash :: Char -> Char
35+
convertSlash '/' = '-'
36+
convertSlash c = c
37+
38+
images tree = tree >>> css "img" >>> getAttrValue "src"
39+
40+
download url = do
41+
content <- runMaybeT $ openUrl url
42+
case content of
43+
Nothing -> putStrLn $ "bad url: " ++ url
44+
Just _content -> do
45+
let name = tail . uriPath . fromJust . parseURI $ url
46+
let name = map convertSlash name
47+
print name
48+
B.writeFile name (B.pack _content)
49+
50+
parseArgs = do
51+
args <- getArgs
52+
case args of
53+
(url:[]) -> return url
54+
otherwise -> error "usage: getbrabber [url]"

haskell_snippet/crawler/requirements

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
hxt
2+
url
3+
http
4+
maybet
5+
parallel-io
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
class Factor f
4+
5+
main :: IO ()
6+
main = do
7+
print 1

haskell_snippet/learnyouhaskell/rd.hs

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import Data.List
22

33
data Section = Section {
44
getA :: Int,
@@ -27,7 +27,33 @@ roadStep (pathA, pathB) (Section a b c) =
2727
else (C, c):(A, a):pathA
2828
in (newPathToA, newPathToB)
2929

30+
optimalPath :: RoadSystem -> Path
31+
optimalPath roadSystem =
32+
let (bestAPath, bestBPath) = foldl roadStep ([], []) roadSystem
33+
in if sum (map snd bestAPath) <= sum (map snd bestBPath)
34+
then reverse bestAPath
35+
else reverse bestBPath
36+
37+
groupsOf :: Int -> [a]-> [[a]]
38+
groupsOf 0 _ = undefined
39+
groupsOf _ [] = []
40+
groupsOf n xs = take n xs: groupsOf n (drop n xs)
3041

3142
main :: IO ()
3243
main = do
33-
return ()
44+
contents <- getContents
45+
let threes = groupsOf 3 (map read $ lines contents)
46+
roadSystem = map (\[a, b, c] -> Section a b c) threes
47+
path = optimalPath roadSystem
48+
pathString = concat $ map (show . fst) path
49+
pathTime = sum $ map snd path
50+
putStrLn $ "The best path to take is:" ++ pathString
51+
putStrLn $ "Time taken: " ++ show pathTime
52+
print $ optimalPath headhrowToLondon
53+
where
54+
headhrowToLondon = [
55+
Section 50 10 30,
56+
Section 5 90 20,
57+
Section 40 2 25,
58+
Section 10 8 0
59+
]

python/wegame/tiaoyitiao/auto.py

100644100755
+46-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/sh
22
# -*- coding: utf-8 -*-
3+
from __future__ import print_function
34
''''which python2 >/dev/null && exec python2 "$0" "$@" # '''
45
''''which python >/dev/null && exec python "$0" "$@" # '''
56

@@ -95,15 +96,13 @@ def is_area_circle(points, diff_ratio = 0.05):
9596
top_bottom_diff = corner_points[3][1] - corner_points[2][1]
9697
return abs(top_bottom_diff - left_right_diff) < (left_right_diff + top_bottom_diff) * 0.5 * diff_ratio
9798

98-
iteration = 0
9999
auto_mode = True
100100
restart_game_after_fail = False
101101
show_screen_when_jump = False
102-
train_iterations = 3
103-
iteration_sleep_time = 1.3
102+
iteration_sleep_time = 2
104103
step_one_distance = -1
105104
step_one_time = -1
106-
DEBUG = 0
105+
DEBUG = False
107106

108107
if len(sys.argv) > 1:
109108
try:
@@ -114,16 +113,20 @@ def is_area_circle(points, diff_ratio = 0.05):
114113

115114
if len(sys.argv) > 2:
116115
try:
117-
restart_game_after_fail = bool(sys.argv[2])
116+
restart_game_after_fail = int(sys.argv[2])
118117
print("[INFO] Set restart_game_after_fail to ", restart_game_after_fail)
119118
except:
120119
pass
121120

122121
if cv2:
123122
cv2.namedWindow("img")
124123

124+
iteration = 0
125+
bullseye_cnt = 0
126+
previous_state = ((0, 0), (0, 0))
125127
def main():
126-
global iteration, auto_mode, train_iterations, iteration_sleep_time, step_one_distance, step_one_time, DEBUG
128+
global auto_mode, iteration_sleep_time, step_one_distance, step_one_time, DEBUG, iteration, bullseye_cnt
129+
global previous_state
127130
img_filename = "screen.png"
128131
if not DEBUG:
129132
jump.screen_capture(img_filename)
@@ -223,7 +226,7 @@ def main():
223226
first_color = colors[x][y]
224227
break
225228
else:
226-
if first_color == -1 and colors[x][y] != colors[x][initial_height] and sum_of_tuple(arr[x, y], arr[current_position]) > 300 and color_sum[colors[x][y]] > 100:
229+
if first_color == -1 and colors[x][y] != colors[x][initial_height] and sum_of_tuple(arr[x, y], arr[current_position]) > 60 and color_sum[colors[x][y]] > 100:
227230
first_color = colors[x][y]
228231
break
229232

@@ -302,6 +305,11 @@ def main():
302305

303306
print("distance", distance)
304307

308+
if bullseye_color != -1:
309+
bullseye_cnt += 1
310+
else:
311+
bullseye_cnt = 0
312+
305313
suggestion_time = 0
306314

307315
if step_one_distance == -1:
@@ -312,10 +320,18 @@ def main():
312320
#step_one_time = 7.2
313321
suggestion_time = step_one_time * 1.0 / step_one_distance * distance
314322

315-
if color_sum[first_color] > 3000:
316-
random_diff = (random.random() - 0.5) * 1.2
317-
print("[VERBOSE] random_diff: ", random_diff)
318-
suggestion_time += random_diff
323+
random_diff = 0
324+
target_area_size = color_sum[first_color]
325+
if target_area_size > 3000:
326+
random_diff = (random.random() - 0.5) * 0.9
327+
elif target_area_size <= 3000 and target_area_size > 2000:
328+
random_diff = (random.random() - 0.5) * 0.3
329+
330+
if bullseye_cnt > 4 and target_area_size > 1000:
331+
random_diff = random_diff if random_diff > 0.444444 else 0.4444444
332+
333+
print("[VERBOSE] random_diff: ", random_diff)
334+
suggestion_time += random_diff
319335

320336
### : if the direction is right-top, this suggestion time will be a little larger than actually correct value
321337
if target_position[0] > current_position[0]: #and suggestion_time > 9:
@@ -338,10 +354,21 @@ def main():
338354
if not DEBUG:
339355
#sys_time.sleep(iteration_sleep_time)
340356
time = jump.jump(time)
341-
sys_time.sleep(iteration_sleep_time)
357+
sys_time.sleep(iteration_sleep_time + random.random())
358+
359+
if previous_state[0] == current_position and previous_state[1] == target_position:
360+
raise Exception("Same state!")
361+
else:
362+
previous_state = (current_position, target_position)
342363

343364
print("current iteration: ", iteration)
344365
iteration += 1
366+
if iteration % 20 == 0:
367+
sys_time.sleep(random.random() * 10)
368+
if iteration % 50 == 0:
369+
sys_time.sleep(random.random() * 100)
370+
if iteration % 100 == 0:
371+
sys_time.sleep(random.random() * 200)
345372

346373
game_count = 0
347374
#if __name__ == "__main__":
@@ -351,17 +378,18 @@ def main():
351378
if DEBUG:
352379
break
353380
except Exception as e:
381+
previous_state = ((0, 0), (0, 0))
354382
print("Exception occured in main function: ", e)
355383
if restart_game_after_fail:
356384
print("restaring game")
357-
sys_time.sleep(7)
385+
#sys_time.sleep(7)
386+
if 2 ** game_count < 60 * 10:
387+
sys_time.sleep(2 ** game_count)
388+
else:
389+
sys_time.sleep(60 * 10)
358390
jump.restart_game()
359-
sys_time.sleep(7)
391+
sys_time.sleep(iteration_sleep_time*2)
360392
game_count += 1
361393
print("restared game, game_count: ", game_count)
362-
if 2 ** game_count < 60:
363-
sys_time.sleep(60 * (2 ** game_count))
364-
else:
365-
sys_time.sleep(60 * 60)
366394
else:
367395
raise

0 commit comments

Comments
 (0)