-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rb
72 lines (62 loc) · 1.73 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
68
69
70
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 part1
c = Cpu.new INST
c.run
lines = c.read_all_out.map(&:chr).join.split("\n")
(0...lines[0].size).map do |x|
(0...lines.size).select do |y|
[[-1,0], [0, 1], [1, 0], [0, -1], [0, 0]].all? do |a|
pt = sum_arr [x, y], a
'#' == get_arr_xy(lines, pt.first, pt.last)
end
end.map do |y|
x * y
end.sum
end.sum
end
def part2
c = Cpu.new [2] + INST[1..-1]
c.run
c.write_input_array 'A,B,A,B,C,C,B,C,B,A'.each_char.map(&:ord)
c.write_input 10
c.write_input_array 'R,12,L,8,R,12'.each_char.map(&:ord)
c.write_input 10
c.write_input_array 'R,8,R,6,R,6,R,8'.each_char.map(&:ord)
c.write_input 10
c.write_input_array 'R,8,L,8,R,8,R,4,R,4'.each_char.map(&:ord)
c.write_input 10
c.read_all_out
c.run
c.write_input 'y'.ord
c.write_input 10
while not c.finished
c.execute_one
if c.has_output
r = c.read_output
return r if r > 255
# if r == 10
# gets
# else
# print r.chr
# end
end
end
end
PART1 = part1
PART2 = part2
puts 'Part 1: %s' % PART1
puts 'Part 2: %s' % PART2
# R,12,L,8,R,12,R,8,R,6,R,6,R,8,R,12,L,8,R,12,R,8,R,6,R,6,R,8,R,8,L,8,R,8,R,4,R,4,R,8,L,8,R,8,R,4,R,4,R,8,R,6,R,6,R,8,R,8,L,8,R,8,R,4,R,4,R,8,R,6,R,6,R,8,R,12,L,8,R,12
# A,B,A,B,C,C,B,C,B,A
# A = R,12,L,8,R,12
# B = R,8,R,6,R,6,R,8
# C = R,8,L,8,R,8,R,4,R,4