-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.py
65 lines (50 loc) · 1.26 KB
/
day5.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
import queue
test = [
"ZN",
"MCD",
"P"
]
init = [
"CZNBMWQV",
"HZRWCB",
"FQRJ",
"ZSWHFNMT",
"GFWLNQP",
"LPW",
"VBDRGCQJ",
"ZQNBW",
"HLFCGTJ",
]
def initQueue(letters):
q = queue.LifoQueue()
for l in letters:
q.put(l)
return q
def extract(l):
parts = l.split()
return int(parts[3]), int(parts[5]), int(parts[1])
def task1(lines, bricks):
for l in lines:
src, dest, quantity = extract(l)
for i in range(quantity):
bricks[dest - 1].put(bricks[src - 1].get())
return [b.get() for b in bricks]
def task2(lines, bricks):
for l in lines:
src, dest, quantity = extract(l)
if quantity == 1:
bricks[dest - 1].put(bricks[src - 1].get())
else:
helpQ = queue.LifoQueue()
for i in range(quantity):
helpQ.put(bricks[src - 1].get())
for i in range(quantity):
bricks[dest - 1].put(helpQ.get())
return [b.get() for b in bricks]
if __name__ == '__main__':
f = open('./day5_input.txt', 'r')
lines = f.readlines()
bricks = [initQueue(i) for i in init]
print(task1(lines, bricks))
bricks = [initQueue(i) for i in init]
print(task2(lines, bricks))