aboutsummaryrefslogtreecommitdiff
path: root/day23/part2
blob: 28cabf84da5d5840988688c8af980fdca5099683 (plain)
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
#!/usr/bin/env ruby

require 'prime'

def valof(reg_file, reg_or_int)
  if reg_or_int >= 'a' && reg_or_int <= 'z' then
    return reg_file[reg_or_int] || 0
  end
  return reg_or_int.to_i
end

input = $stdin.readlines.map(&:chomp)

reg = {'a' => 1}
pc = 0

loop do
  break if pc > 7

  istr = input[pc].split
  case istr[0]
  when 'set'
    reg[istr[1]] = valof(reg, istr[2])
  when 'sub'
    reg[istr[1]] = valof(reg, istr[1]) - valof(reg, istr[2])
  when 'mul'
    reg[istr[1]] = valof(reg, istr[1]) * valof(reg, istr[2])
  end
  pc += 1
end

n = reg['b']
stop = reg['c']
diff = -input[-2].split.last.to_i
h = 0

# What the horrible program is doing is counting the number of
# composite numbers there are starting with #{b} and incrementing
# by #{diff} until and including #{stop}.

loop do
  break if n > stop
  h += 1 unless Prime::prime?(n)
  n += diff
end

puts h