This repository was archived by the owner on Dec 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paths.py
More file actions
118 lines (110 loc) · 2.86 KB
/
s.py
File metadata and controls
118 lines (110 loc) · 2.86 KB
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
# Youtube: https://youtu.be/ZO83MHcgLMI
import argparse, math, sys, re, functools, operator, itertools, heapq
from collections import defaultdict, Counter, deque
#sys.setrecursionlimit(100000000)
#A = list(map(int, input().split()))
#T = int(input())
def read_lines(f):
while True:
line = f.readline()
if not line:
break
assert line[-1] == '\n'
yield line[:-1]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-1', '--one', action='store_true', help='Only part 1')
parser.add_argument('-2', '--two', action='store_true', help='Only part 2')
parser.add_argument('input_file', nargs='?')
args = parser.parse_args()
if args.input_file is not None:
f = open(args.input_file)
else:
f = sys.stdin
lines = list(read_lines(f))
if not args.two:
print(part_1(lines))
if not args.one:
print(part_2(lines))
def part_1(lines):
s = 0
X, Y = 101, 103
#X, Y = 11, 7
T = 100
c = Counter()
for i in lines:
matched = re.fullmatch('p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)', i)
px, py, vx, vy = map(int, matched.groups())
fx = (px + vx * T) % X
fy = (py + vy * T) % Y
# Check quadrant.
mx = (X - 1) // 2
my = (Y - 1) // 2
if fx == mx or fy == my:
continue
c[((mx < fx), (my < fy))] += 1
s = functools.reduce(operator.mul, c.values())
return s
def part_2_manual(lines):
s = 0
X, Y = 101, 103
T = 100
robots = []
for i in lines:
matched = re.fullmatch('p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)', i)
px, py, vx, vy = map(int, matched.groups())
robots.append((px, py, vx, vy))
for T in range(1000):
canvas = []
for y in range(Y):
canvas.append([])
for x in range(X):
canvas[-1].append(' ')
for px, py, vx, vy in robots:
fx = (px + vx * T) % X
fy = (py + vy * T) % Y
canvas[fy][fx] = '█'
for i in canvas:
print(''.join(i))
print('-' * X)
input(str(T))
# 33|, 87-, 134|, 190-, 235|, 293-, 336|
# 33 + 101 * x = 87 + 103 * y
s = 7709
return s
def part_2(lines):
s = None
X, Y = 101, 103
T = 100
robots = []
for i in lines:
matched = re.fullmatch('p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)', i)
px, py, vx, vy = map(int, matched.groups())
robots.append((px, py, vx, vy))
for T in range(X * Y):
canvas = []
for y in range(Y):
canvas.append([])
for x in range(X):
canvas[-1].append(0)
for px, py, vx, vy in robots:
fx = (px + vx * T) % X
fy = (py + vy * T) % Y
canvas[fy][fx] = 1
# CV
for y in range(Y):
for x in range(X):
if (canvas[(y + 0) % Y][(x + 0) % X] == 1 and
canvas[(y + 0) % Y][(x + 1) % X] == 1 and
canvas[(y + 1) % Y][(x + 0) % X] == 1 and
canvas[(y + 1) % Y][(x + 1) % X] == 0 and
canvas[(y + 0) % Y][(x + 2) % X] == 1 and
canvas[(y + 2) % Y][(x + 0) % X] == 1 and
canvas[(y + 0) % Y][(x + 3) % X] == 1 and
canvas[(y + 3) % Y][(x + 0) % X] == 1):
s = T
if s is not None:
break
return s
if __name__ == '__main__':
main()