-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
51 lines (43 loc) · 901 Bytes
/
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
require 'io/console'
require_relative '../common/intcode'
INST = File.read('data.txt').split(',').map(&:to_i)
def get_arr_xy arr, x, y
return nil if x < 0 or y < 0
arr.fetch(y, [])[x]
end
def sum_arr a, b
a.zip(b).map{ |s| s.reduce(:+) }
end
def check_xy x, y
c = Cpu.new INST
c.write_input x
c.write_input y
c.run
c.read_output
end
def part1
(0...50).map do |y|
(0...50).map do |x|
check_xy x, y
end.sum
end.sum
end
def check_if_fits x, y
right = check_xy(x + 99, y)
bottom = check_xy(x, y + 99)
right == 1 and bottom == 1
end
def part2
x, y = 1, 1
loop do
x += 1
d = (1..10000).bsearch do |d|
check_xy(x, y+d) == 0
end
y += d - 1
break if check_if_fits(x, y-99)
end
"%d%04d" % [x, y-99]
end
puts 'Part 1: %s' % part1
puts 'Part 2: %s' % part2