-
Notifications
You must be signed in to change notification settings - Fork 81
/
part1.py
77 lines (70 loc) · 1.93 KB
/
part1.py
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
#!/usr/bin/env python3
# find nethack seed by start inventory
# searching RNG around specified time
# not required for getting flag
import argparse
import os
import pty
import sys
import time
import string
import datetime
nethack = '\'/usr/local/games/nethack\''
filename1 = 'part1.log'
targetin1 = 'part1_in.txt'
seedfile = 'time.txt'
seed_start = int(datetime.datetime(2021,8,31,22,18,55).timestamp())
seed2try = 0
while True:
seed = seed_start + seed2try
fseed = open(seedfile, 'w')
fseed.write(str(seed))
fseed.close()
print('Try seed ', str(seed))
(pid, fd) = pty.fork()
if pid == 0:
exec('os.system('+ nethack + ')')
exit()
else:
time.sleep(0.1)
fout = open(filename1, 'w')
os.write(fd, "nahml i #quit\nyq".encode())
try:
nout = os.read(fd, 1024)
while nout != None:
nout = nout.rstrip(b'\033')
fout.write(nout.decode('ascii'))
nout = os.read(fd, 1024)
except OSError:
print('nethack ended successfully.')
fout.close()
ftofind = open(targetin1, 'r')
flog1 = open(filename1, 'r')
strall = flog1.read().encode('ascii')
strtofind = ftofind.readlines()
ftofind.close()
flog1.close()
# print(strall)
hit = 1
for i in strtofind:
# print(i)
if strall.find(i[:-1].encode('ascii')) != -1:
print('Found ' + i[:-1])
else:
print('NOT ' + i[:-1])
hit = 0
if (hit == 1):
print('Hit: seed for this run is ' + str(seed))
exit()
else:
print('Pass, next')
if seed2try >= 0:
seed2try = - seed2try - 1
else:
seed2try = - seed2try
if seed2try >= 200:
print('+- 200 seconds tried but no luck')
exit()
# input()
# open ./part1_in.txt and if all lines appears in part1.log,
# we get a hit