-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
59 lines (49 loc) · 1.17 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
require 'json'
require_relative '../common/intcode'
DATA = File.read('data.txt').scan(/\d/).map(&:to_i)
OFFSET = DATA[0...7].join.to_i
def make_pattern n, len
[0, 1, 0, -1]
.map{ |v| [v] * n }
.flatten
.cycle
.take(len + 1)
.drop(1)
end
def phase arr
arr.size.times.map do |n|
arr.zip(make_pattern n + 1, arr.size)
.map{ |a| a.reduce(&:*) }
.sum
.to_s[-1]
end.join.scan(/\d/).map(&:to_i)
end
def do_n_phases arr, n
n.times{ |n| arr = phase arr }
arr
end
def partial_phase arr, offset
raise :nope unless offset > arr.size / 2
r = []
sum = 0
arr[offset..-1].reverse.each do |n|
sum += n
sum %= 10
r.push sum
end
Array.new(offset) + r.reverse
end
def do_n_partial_phases arr, offset, n
n.times do |n|
arr = partial_phase arr, offset
end
arr
end
def part2
r = do_n_partial_phases DATA*10000, offset, 100
r[offset...(offset+8)].join
end
PART1 = do_n_phases(DATA, 100)[0...8].join
PART2 = do_n_partial_phases(DATA * 10_000, OFFSET, 100).drop(OFFSET)[0...8].join
puts 'Part 1: %s' % PART1
puts 'Part 2: %s' % PART2