Skip to content

Commit 5df6e93

Browse files
committed
Update things
1 parent 9023789 commit 5df6e93

12 files changed

+436
-0
lines changed

09-01.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
head = (0, 0)
2+
tail = (0, 0)
3+
visited = {(0, 0)}
4+
deltas = {
5+
'U': [0, 1],
6+
'D': [0, -1],
7+
'L': [-1, 0],
8+
'R': [1, 0]
9+
}
10+
def sign(n):
11+
if n > 0:
12+
return 1
13+
if n < 0:
14+
return -1
15+
return 0
16+
while True:
17+
inputval = input()
18+
if inputval == "": break
19+
dir, moves = inputval.split()
20+
moves = int(moves)
21+
for i in range(moves):
22+
head = (deltas[dir][0] + head[0], deltas[dir][1] + head[1])
23+
diff = (head[0] - tail[0], head[1] - tail[1])
24+
if abs(diff[0]) >= 2 or abs(diff[1]) >= 2:
25+
tail = (tail[0] + sign(diff[0]), tail[1] + sign(diff[1]))
26+
visited.add(tail)
27+
print(len(visited))

09-02.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
rope = []
2+
for i in range(10):
3+
rope.append((0, 0))
4+
visited = {(0, 0)}
5+
deltas = {
6+
'U': [0, 1],
7+
'D': [0, -1],
8+
'L': [-1, 0],
9+
'R': [1, 0]
10+
}
11+
def sign(n):
12+
if n > 0:
13+
return 1
14+
if n < 0:
15+
return -1
16+
return 0
17+
while True:
18+
inputval = input()
19+
if inputval == "": break
20+
dir, moves = inputval.split()
21+
moves = int(moves)
22+
for i in range(moves):
23+
rope[0] = (deltas[dir][0] + rope[0][0], deltas[dir][1] + rope[0][1])
24+
for j in range(9):
25+
diff = (rope[j][0] - rope[j + 1][0], rope[j][1] - rope[j + 1][1])
26+
if abs(diff[0]) >= 2 or abs(diff[1]) >= 2:
27+
rope[j + 1] = (rope[j + 1][0] + sign(diff[0]), rope[j + 1][1] + sign(diff[1]))
28+
visited.add(rope[-1])
29+
print(len(visited))

10-01.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
register = 1
2+
score = 0
3+
cycle_num = 0
4+
cycles = [220, 180, 140, 100, 60, 20]
5+
while True:
6+
inputval = input()
7+
if inputval == '': break
8+
inputvals = inputval.split()
9+
if inputvals[0] == 'noop':
10+
cycle_num += 1
11+
if cycles and cycle_num >= cycles[-1]:
12+
score += cycles[-1] * register
13+
print(cycles[-1] * register)
14+
cycles.pop()
15+
else:
16+
cycle_num += 2
17+
if cycles and cycle_num >= cycles[-1]:
18+
score += cycles[-1] * register
19+
print(cycles[-1] * register)
20+
cycles.pop()
21+
register += int(inputvals[1])
22+
print(score)

10-02.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
register = 1
2+
cycle_num = 0
3+
cycles = 40
4+
crtOutput = ""
5+
def outputPixel():
6+
global crtOutput
7+
if abs(register - cycle_num) <= 1:
8+
crtOutput += '#'
9+
else:
10+
crtOutput += '.'
11+
while True:
12+
inputval = input()
13+
if inputval == '': break
14+
inputvals = inputval.split()
15+
if inputvals[0] == 'noop':
16+
outputPixel()
17+
cycle_num += 1
18+
if cycles and cycle_num >= cycles:
19+
cycle_num = 0
20+
crtOutput += '\n'
21+
else:
22+
for i in range(2):
23+
outputPixel()
24+
cycle_num += 1
25+
if cycles and cycle_num >= cycles:
26+
cycle_num = 0
27+
crtOutput += '\n'
28+
register += int(inputvals[1])
29+
print(crtOutput)

11-01.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Monkey:
2+
def __init__(self, items = None, transition = None, test = None, ifTrue = None, ifFalse = None) -> None:
3+
self.items = items
4+
self.transition = transition
5+
self.test = test
6+
self.ifTrue = ifTrue
7+
self.ifFalse = ifFalse
8+
9+
monkeys = []
10+
11+
def make_lambda(op, val):
12+
if op == "+":
13+
return lambda old: old + (old if val == "old" else int(val))
14+
else:
15+
return lambda old: old * (old if val == "old" else int(val))
16+
17+
while True:
18+
inputval = input()
19+
if inputval == "": break
20+
monkey = Monkey()
21+
monkey.items = list(map(lambda x: int(x), input()[18:].split(", ")))
22+
newFunc = input()[23:].split()
23+
monkey.transition = make_lambda(*newFunc)
24+
monkey.test = int(input()[21:])
25+
monkey.ifTrue = int(input()[29:])
26+
monkey.ifFalse = int(input()[30:])
27+
monkeys.append(monkey)
28+
input()
29+
30+
inspections = [0] * len(monkeys)
31+
32+
for i in range(20):
33+
print(f"Round {i+1}:")
34+
for j in range(len(monkeys)):
35+
print(f" Monkey {j}:")
36+
monkey = monkeys[j]
37+
for item in monkey.items:
38+
print(f" Item {item}:", end='')
39+
item = monkey.transition(item)
40+
print(f" New score={item};", end='')
41+
item = item // 3
42+
print(f" New score={item};", end='')
43+
inspections[j] += 1
44+
if item % monkey.test == 0:
45+
monkeys[monkey.ifTrue].items.append(item)
46+
print(f" True, to monkey {monkey.ifTrue}")
47+
else:
48+
monkeys[monkey.ifFalse].items.append(item)
49+
print(f" False, to monkey {monkey.ifFalse}")
50+
monkey.items.clear()
51+
print(inspections)
52+
inspections.sort()
53+
print(inspections[-1] * inspections[-2])

11-02.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Monkey:
2+
def __init__(self, items = None, transition = None, test = None, ifTrue = None, ifFalse = None) -> None:
3+
self.items = items
4+
self.transition = transition
5+
self.test = test
6+
self.ifTrue = ifTrue
7+
self.ifFalse = ifFalse
8+
9+
monkeys = []
10+
11+
def make_lambda(op, val):
12+
if op == "+":
13+
return lambda old: old + (old if val == "old" else int(val))
14+
else:
15+
return lambda old: old * (old if val == "old" else int(val))
16+
17+
while True:
18+
inputval = input()
19+
if inputval == "": break
20+
monkey = Monkey()
21+
monkey.items = list(map(lambda x: int(x), input()[18:].split(", ")))
22+
newFunc = input()[23:].split()
23+
monkey.transition = make_lambda(*newFunc)
24+
monkey.test = int(input()[21:])
25+
monkey.ifTrue = int(input()[29:])
26+
monkey.ifFalse = int(input()[30:])
27+
monkeys.append(monkey)
28+
input()
29+
30+
inspections = [0] * len(monkeys)
31+
32+
lcmVal = 1
33+
34+
# Manually check that the monkeys have prime test values
35+
for monkey in monkeys:
36+
lcmVal = lcmVal * monkey.test
37+
38+
for i in range(10000):
39+
for j in range(len(monkeys)):
40+
monkey = monkeys[j]
41+
for item in monkey.items:
42+
item = monkey.transition(item) % lcmVal
43+
inspections[j] += 1
44+
if item % monkey.test == 0:
45+
monkeys[monkey.ifTrue].items.append(item)
46+
else:
47+
monkeys[monkey.ifFalse].items.append(item)
48+
monkey.items.clear()
49+
print(inspections)
50+
inspections.sort()
51+
print(inspections[-1] * inspections[-2])

12-01.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from collections import deque
2+
3+
height_map = []
4+
min_dist = []
5+
bfs_queue = deque()
6+
target_location = None
7+
while True:
8+
inputval = input()
9+
if inputval == "": break
10+
height_map.append([])
11+
min_dist.append([])
12+
for i in range(len(inputval)):
13+
if inputval[i] == 'S':
14+
height_map[-1].append(1)
15+
min_dist[-1].append(0)
16+
bfs_queue.append((len(height_map) - 1, i))
17+
elif inputval[i] == 'E':
18+
height_map[-1].append(26)
19+
min_dist[-1].append(-1)
20+
target_location = (len(height_map) - 1, i)
21+
else:
22+
height_map[-1].append(ord(inputval[i]) - ord('a') + 1)
23+
min_dist[-1].append(-1)
24+
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
25+
while min_dist[target_location[0]][target_location[1]] == -1:
26+
row, col = bfs_queue.popleft()
27+
for rowDelta, colDelta in dirs:
28+
newRow, newCol = row + rowDelta, col + colDelta
29+
if newRow >= 0 and newRow < len(height_map) and newCol >= 0 and newCol < len(height_map[newRow]):
30+
if min_dist[newRow][newCol] == -1 and height_map[newRow][newCol] - height_map[row][col] <= 1:
31+
min_dist[newRow][newCol] = min_dist[row][col] + 1
32+
bfs_queue.append((newRow, newCol))
33+
print(min_dist[target_location[0]][target_location[1]])

12-02.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from collections import deque
2+
3+
height_map = []
4+
min_dist = []
5+
bfs_queue = deque()
6+
target_location = None
7+
while True:
8+
inputval = input()
9+
if inputval == "": break
10+
height_map.append([])
11+
min_dist.append([])
12+
for i in range(len(inputval)):
13+
if inputval[i] == 'S' or inputval[i] == 'a':
14+
height_map[-1].append(1)
15+
min_dist[-1].append(0)
16+
bfs_queue.append((len(height_map) - 1, i))
17+
elif inputval[i] == 'E':
18+
height_map[-1].append(26)
19+
min_dist[-1].append(-1)
20+
target_location = (len(height_map) - 1, i)
21+
else:
22+
height_map[-1].append(ord(inputval[i]) - ord('a') + 1)
23+
min_dist[-1].append(-1)
24+
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
25+
while min_dist[target_location[0]][target_location[1]] == -1:
26+
row, col = bfs_queue.popleft()
27+
for rowDelta, colDelta in dirs:
28+
newRow, newCol = row + rowDelta, col + colDelta
29+
if newRow >= 0 and newRow < len(height_map) and newCol >= 0 and newCol < len(height_map[newRow]):
30+
if min_dist[newRow][newCol] == -1 and height_map[newRow][newCol] - height_map[row][col] <= 1:
31+
min_dist[newRow][newCol] = min_dist[row][col] + 1
32+
bfs_queue.append((newRow, newCol))
33+
print(min_dist[target_location[0]][target_location[1]])

13-01.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
left_signals = []
2+
right_signals = []
3+
while True:
4+
inputval = input()
5+
if inputval == "": break
6+
left_signals.append(eval(inputval))
7+
right_signals.append(eval(input()))
8+
input()
9+
score = 0
10+
def compare_values(a, b):
11+
if type(a) is int:
12+
if type(b) is int:
13+
return a - b
14+
else:
15+
return compare_values([a], b)
16+
else:
17+
if type(b) is int:
18+
return compare_values(a, [b])
19+
else:
20+
for i in range(min(len(a), len(b))):
21+
val = compare_values(a[i], b[i])
22+
if val != 0:
23+
return val
24+
return len(a) - len(b)
25+
for i in range(len(left_signals)):
26+
if compare_values(left_signals[i], right_signals[i]) < 0:
27+
score += i + 1
28+
print(score)

13-02.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import functools
2+
signals = [[[2]], [[6]]]
3+
while True:
4+
inputval = input()
5+
if inputval == "": break
6+
signals.append(eval(inputval))
7+
signals.append(eval(input()))
8+
input()
9+
def compare_values(a, b):
10+
if type(a) is int:
11+
if type(b) is int:
12+
return a - b
13+
else:
14+
return compare_values([a], b)
15+
else:
16+
if type(b) is int:
17+
return compare_values(a, [b])
18+
else:
19+
for i in range(min(len(a), len(b))):
20+
val = compare_values(a[i], b[i])
21+
if val != 0:
22+
return val
23+
return len(a) - len(b)
24+
signals.sort(key=functools.cmp_to_key(compare_values))
25+
26+
score = 1
27+
28+
for i in range(len(signals)):
29+
if compare_values(signals[i], [[2]]) == 0 or compare_values(signals[i], [[6]]) == 0:
30+
score = score * (i + 1)
31+
print(score)

14-01.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
rocks = []
2+
while True:
3+
inputval = input()
4+
if inputval == "": break
5+
rocks.append([tuple(int(x) for x in oneCoord.split(",")) for oneCoord in inputval.split(" -> ")])
6+
min_x = min(min(coord[0] for coord in line) for line in rocks)
7+
max_x = max(max(coord[0] for coord in line) for line in rocks)
8+
max_y = max(max(coord[1] for coord in line) for line in rocks)
9+
10+
print(min_x, max_x, max_y)
11+
12+
# 0 for air, 1 for stone, 2 for sand
13+
caveMap = [[0] * (max_y + 1) for i in range(min_x, max_x + 1)]
14+
15+
for line in rocks:
16+
for start, end in zip(line[:-1], line[1:]):
17+
if start[0] < end[0]:
18+
for i in range(start[0], end[0] + 1):
19+
caveMap[i - min_x][start[1]] = 1
20+
else:
21+
for i in range(end[0], start[0] + 1):
22+
caveMap[i - min_x][start[1]] = 1
23+
if start[1] < end[1]:
24+
for i in range(start[1], end[1] + 1):
25+
caveMap[start[0] - min_x][i] = 1
26+
else:
27+
for i in range(end[1], start[1] + 1):
28+
caveMap[start[0] - min_x][i] = 1
29+
30+
score = 0
31+
while True:
32+
sandX, sandY = 500, 0
33+
while sandY < max_y and sandX > min_x and sandX < max_x:
34+
if caveMap[sandX - min_x][sandY + 1] == 0:
35+
sandY += 1
36+
elif caveMap[sandX - min_x - 1][sandY + 1] == 0:
37+
sandY += 1
38+
sandX += -1
39+
elif caveMap[sandX - min_x + 1][sandY + 1] == 0:
40+
sandY += 1
41+
sandX += 1
42+
else:
43+
caveMap[sandX - min_x][sandY] = 2
44+
score += 1
45+
break
46+
if not (sandY < max_y and sandX > min_x and sandX < max_x):
47+
# Reached the edge. Break
48+
break
49+
print(score)

0 commit comments

Comments
 (0)