-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.py
70 lines (64 loc) · 2.15 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
66
67
68
69
70
def parsestacks():
with open("Day5input.txt", "r") as file:
stacks = [[], [], [], [], [], [], [], [], []]
parsing = True
for line in file:
if line == "":
break
if parsing == True:
insertion = False
for i in range(len(line)):
char = line[i]
if char == "[":
insertion = True
continue
if insertion == True:
stacks[(i - 1) // 4].append(char)
insertion = False
for i in range(len(stacks)):
stacks[i] = stacks[i][::-1]
return stacks
def part1(stacks):
with open("Day5input.txt", "r") as file:
ready = False
for line in file:
if not ready:
if not line.strip():
ready = True
continue
_, n, _, From, _, to = line.split(" ")
for i in range(int(n)):
temp = stacks[int(From) - 1].pop()
if temp != []:
stacks[int(to) - 1].append(temp)
result = ""
for i in range(len(stacks)):
if len(stacks[i]) > 0:
result = result + stacks[i].pop()
return result
def part2(stacks):
with open("Day5input.txt", "r") as file:
ready = False
for line in file:
if not ready:
if not line.strip():
ready = True
continue
_, n, _, From, _, to = line.split(" ")
elms = []
for i in range(int(n)):
temp = stacks[int(From) - 1].pop()
elms.append(temp)
for i in range(int(n)):
temp = elms.pop()
if temp != []:
stacks[int(to) - 1].append(temp)
result = ""
for i in range(len(stacks)):
if len(stacks[i]) > 0:
result = result + stacks[i].pop()
return result
stacks = parsestacks()
print("part1",part1(stacks))
stacks = parsestacks()
print("part2",part2(stacks))