-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.cr
91 lines (71 loc) · 1.48 KB
/
14.cr
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
WIDTH = 101
HEIGHT = 103
def p1
content = File.read("input")
lines = content.split('\n')
# tl, tr, bl, br
quads = [0, 0, 0, 0] of Int32
lines.each do |line|
if m = line.match(/p=(\d+),(\d+) v=(-?\d+),(-?\d+)/)
px = m[1].to_i32
py = m[2].to_i32
vx = m[3].to_i32
vy = m[4].to_i32
iters = 0
while iters < 100
iters += 1
px += vx
py += vy
px %= WIDTH
py %= HEIGHT
end
if px < WIDTH // 2
if py < HEIGHT // 2
quads[0] += 1
elsif py > HEIGHT // 2
quads[2] += 1
end
elsif px > WIDTH // 2
if py < HEIGHT // 2
quads[1] += 1
elsif py > HEIGHT // 2
quads[3] += 1
end
end
end
end
sum = quads[0] * quads[1] * quads[2] * quads[3]
puts sum
end
def p2
content = File.read("input")
lines = content.split('\n')
bots = Deque(Array(Int32)).new
lines.each do |line|
if m = line.match(/p=(\d+),(\d+) v=(-?\d+),(-?\d+)/)
px = m[1].to_i32
py = m[2].to_i32
vx = m[3].to_i32
vy = m[4].to_i32
bots.push([px, py, vx, vy])
end
end
count = 0
while count < 1000000
count += 1
botset = Set(Int32).new
bots.each do |bot|
bot[0] += bot[2]
bot[1] += bot[3]
bot[0] %= WIDTH
bot[1] %= HEIGHT
botset.add(bot[0] * 100000 + bot[1])
end
if botset.size == bots.size
puts count
break
end
end
end
p1
p2