-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path11.rb
executable file
·92 lines (82 loc) · 2.04 KB
/
11.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env ruby
require 'pp'
#FILENAME = 'input.txt'
FILENAME = 'input_small.txt'
#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{ |x| x.to_i }
# star = {posx: posx, posy: posy, velx: velx, vely: vely}
# stars.push(star)
#end
class Grid
def self.calc(x, y, serial)
@grid_calculations ||=
Hash.new do |h, key|
x = key[0]
y = key[1]
serial = key[2]
rack_id = x + 10
power = ((rack_id * y) + serial) * rack_id
hundreds = ((power / 100) % 10).floor
h[key] = hundreds - 5
end
@grid_calculations[[x, y, serial]]
end
end
raise 'fail1' unless Grid.calc(3, 5, 8) == 4
raise 'fail2' unless Grid.calc(122, 79, 57) == -5
raise 'fail3' unless Grid.calc(217, 196, 39) == 0
raise 'fail4' unless Grid.calc(101, 153, 71) == 4
raise 'fail5' unless Grid.calc(3, 5, 8) == 4
raise 'fail6' unless Grid.calc(122, 79, 57) == -5
raise 'fail7' unless Grid.calc(217, 196, 39) == 0
raise 'fail8' unless Grid.calc(101, 153, 71) == 4
puts 'Passed tests'
puts 'Part 1'
max_val = 0
max_x = -1
max_y = -1
serial = 1_308
(1).upto(300 - 3) do |y|
(1).upto(300 - 3) do |x|
val = 0
(0).upto(2) do |yadd|
(0).upto(2) { |xadd| val += Grid.calc(x + xadd, y + yadd, serial) }
end
if (max_val < val)
max_val = val
max_x = x
max_y = y
puts "New Max #{max_val} (#{max_x}, #{max_y})"
end
end
end
puts 'Part 2'
max_val = 0
max_x = -1
max_y = -1
serial = 1_308
#size = 3
(1).upto(300) do |size|
(1).upto(300 - size) do |y|
(1).upto(300 - size) do |x|
val = 0
(0).upto(size - 1) do |yadd|
(0).upto(size - 1) do |xadd|
val += Grid.calc(x + xadd, y + yadd, serial)
end
end
if (max_val < val)
max_val = val
max_x = x
max_y = y
max_size = size
puts "New Max #{max_val} (#{max_x}, #{max_y}, #{size})"
end
end
end
end
## Failed Guesses
# 235,86,13