-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path10.rb
executable file
·75 lines (62 loc) · 1.62 KB
/
10.rb
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
#!/usr/bin/env ruby
require 'pp'
#FILENAME = 'input.txt'
FILENAME = 'input_small.txt'
def is_star(stars, x, y)
stars.select { |s| s[:posx] == x && s[:posy] == y }.count > 0
end
def advance_stars(stars)
stars.each do |s|
s[:posx] += s[:velx]
s[:posy] += s[:vely]
end
stars
end
stars = []
File.readlines(FILENAME).each do |line|
posx, posy, velx, vely =
line.match(
/position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+),\s*(-?\d+)>/i
)
.captures
.map(&:to_i)
star = { posx: posx, posy: posy, velx: velx, vely: vely }
stars.push(star)
end
pp stars.select { |s| s[:posy] == 0 }
1.upto(100_000) do |time|
x_center = stars.map { |s| s[:posx] }.sum.fdiv(stars.size)
y_center = stars.map { |s| s[:posy] }.sum.fdiv(stars.size)
x_var = (stars.map { |s| (s[:posx] - x_center)**2 }.sum) / stars.count
y_var = (stars.map { |s| (s[:posy] - y_center)**2 }.sum) / stars.count
#show = (x_var + y_var) < 700
show = (x_var + y_var) < 50
if (show)
if show
puts "time #{time} center (#{x_center}, #{y_center}) var (#{x_var}, #{
y_var
})"
end
x_show_min = (x_center - 40).round
x_show_max = (x_center + 40).round
y_show_min = (y_center - 20).round
y_show_max = (y_center + 20).round
y_show_min.upto(y_show_max) do |y|
x_show_min.upto(x_show_max) do |x|
if (is_star(stars, x, y))
print '#'
else
print '.'
end
end
print "\n"
end
end
print "\n" if show
print time.to_s + "\n" if show
stars = advance_stars(stars)
if (time % 1_000 == 0)
#pp stars
end
end
#pp stars