-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rb
executable file
·68 lines (55 loc) · 1.31 KB
/
main.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
#!/usr/bin/ruby
SERIAL = File
.read('data.txt')
.to_i
def calc_cell x, y, serial
rack = x + 10
power_lvl = rack * y
power_lvl += serial
power_lvl *= rack
power_lvl = (power_lvl/100).to_i % 10
power_lvl - 5
end
GRID = (0..300).map do |y|
(0..300).map{ |x| calc_cell x, y, SERIAL }
end
def calc_grid_p1 x, y
(0...3).to_a
.product((0...3).to_a)
.map{ |ox, oy| calc_cell(x+ox, y+oy, SERIAL) }
.sum
.yield_self do |s|
{
sum: s,
x: x,
y: y,
}
end
end
def calc_grid_p2 x, y
max_size = 300-[x,y].max
prev = 0
r = (1..max_size).map do |max|
added = (0...max).map do |oz|
GRID[y+max-1][x+oz] +
GRID[y+oz][x+max-1]
end.sum
added -= GRID[y+max-1][x+max-1]
prev += added
{
sum: prev,
size: max,
}
end.sort_by{ |x| x[:sum] }.last
r.merge(x: x, y: y)
end
PART1 = (1..300).to_a.product((1..300).to_a)
.map{ |x, y| calc_grid_p1 x, y }
.sort_by{ |r| r[:sum] }
.last
PART2 = (1...300).to_a.product((1...300).to_a)
.map{ |x, y| calc_grid_p2 x, y }
.sort_by{ |r| r[:sum] }
.last
puts 'Part 1: %{x},%{y}' % PART1
puts 'Part 2: %{x},%{y},%{size}' % PART2