-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.py
141 lines (111 loc) · 5.15 KB
/
day05.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
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
'''
--- Day 5: Supply Stacks ---
Note: The input was quite complicated to parse. Especially
the crates. So I manually stored the crates in the form of
dictionary.
This problem basically applying the steps given in the input
and then arrange the crates. Eventually after applying all the
steps you see which crates are on the top of the docks.
In second part if do not need to move multiple crates one by one
hence it changes the configuration. In the end we have to pick the
top crates but this time the answer will be different as the
configuration is also different.
Sample Input Explanation:
Initialy you have the setup of crates. You can see that we have three places
where we can put crates 1, 2, and 3. After the crates configuration you can see
that you have the instructions. That you have to follow one by one.
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
'''
filename = '/Users/ali/Documents/programming-with-python-fork/aoc/day05.txt'
instructionList = [[int(line.split()[1]), int(line.split()[3]), int(line.split()[5])] for line in open(filename)]
def moveItPartOne(cratesPos, nOfCrates, initialPos, finalPos):
'''
Input: This takes 4 parapmeters.
createpos: this is the current positions of the crates which is the
dictionary and it is created from the input.
noOfCrates: this stores the number of crates that we hvae to move.
initialPos: this the position of the starting.
finalpos: This is the final position of the crates.
Output: Returns the updated crates positions dictionary.
'''
# For loop to take one crate at a time.
for _ in range(nOfCrates):
# remove the crate from the orignal psition.
popped = cratesPos[initialPos].pop(0)
# update the create position in the dictionary
cratesPos[finalPos] = [popped] + cratesPos[finalPos]
return cratesPos
def moveItPart2(cratesPos, nOfCrates, initialPos, finalPos):
'''
Input: This takes 4 parapmeters.
createpos: this is the current positions of the crates which is the
dictionary and it is created from the input.
noOfCrates: this stores the number of crates that we hvae to move.
initialPos: this the position of the starting.
finalpos: This is the final position of the crates.
Output: Returns the updated crates positions dictionary.
'''
# Unlike the other function for part one
# this does the job in one go
# it takes the whole stack of crates
# removes from original position
# then updates the dictionary of crates pos
popped = cratesPos[initialPos][:nOfCrates]
cratesPos[initialPos] = cratesPos[initialPos][nOfCrates:]
cratesPos[finalPos] = popped + cratesPos[finalPos]
return cratesPos
def getResult(cratesPos):
"""
Input: This function simply takes the crate position dictionary
Output: Returns the printable string which are all the top crates in the
dictionary of create positons.
"""
return ''.join([cratesPos[i][0] for i in cratesPos.keys()])
def applyInstructions(instructionList, cratesPos, part):
'''
Input: This takes theree parameters.
instructionsList: Which is the list generated from the input.
createPos: The initial position fo the crates
part: this is a intger 1 for part one and 2 for part 2.
basically it controls which movement function will be used.
As this function can be used for solving both parts of day 5.
Output: Returns the final positon of the crates in the form of a dictionary.
'''
for instruction in instructionList:
nOfCrates = instruction[0]
initialPos = instruction[1]
finalPos = instruction[2]
if part == 2:
cratesPos = moveItPart2(cratesPos, nOfCrates, initialPos, finalPos)
else:
cratesPos = moveItPartOne(cratesPos, nOfCrates, initialPos, finalPos)
return cratesPos
cratesPos = {1: ['S', 'P', 'H', 'V', 'F', 'G'],
2: ['M', 'Z', 'D', 'V', 'B', 'F', 'J', 'G'],
3: ['N', 'J', 'L', 'M', 'G'],
4: ['P', 'W', 'D', 'V', 'Z', 'G', 'N'],
5: ['B', 'C', 'R', 'V'],
6: ['Z', 'L', 'W', 'P', 'M', 'S', 'R', 'V'],
7: ['P', 'H', 'T'],
8: ['V', 'Z', 'H', 'C', 'N', 'S', 'R', 'Q'],
9: ['J', 'Q', 'V', 'P', 'G', 'L', 'F']}
finalCratesPosPart1 = applyInstructions(instructionList, cratesPos, 1)
print('Part1: ', getResult(finalCratesPosPart1))
cratesPos = {1: ['S', 'P', 'H', 'V', 'F', 'G'],
2: ['M', 'Z', 'D', 'V', 'B', 'F', 'J', 'G'],
3: ['N', 'J', 'L', 'M', 'G'],
4: ['P', 'W', 'D', 'V', 'Z', 'G', 'N'],
5: ['B', 'C', 'R', 'V'],
6: ['Z', 'L', 'W', 'P', 'M', 'S', 'R', 'V'],
7: ['P', 'H', 'T'],
8: ['V', 'Z', 'H', 'C', 'N', 'S', 'R', 'Q'],
9: ['J', 'Q', 'V', 'P', 'G', 'L', 'F']}
finalCratesPosPart2 = applyInstructions(instructionList, cratesPos, 2)
print('Part2: ',getResult(finalCratesPosPart2))