aboutsummaryrefslogtreecommitdiff
path: root/day23/part2
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2018-11-20 15:45:18 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2018-11-20 15:45:18 +0000
commit360688b02454c6ce74b95ac0a896eaf14d73a220 (patch)
tree60d1e5d311c597569b12d7b736ba7c6d8b08ad2f /day23/part2
parent2e8936e46ecf4a6784caacbf1b10ee67f0757aa5 (diff)
[day23] Drew a graph to determine what the program doesHEADmaster
Diffstat (limited to 'day23/part2')
-rwxr-xr-xday23/part247
1 files changed, 47 insertions, 0 deletions
diff --git a/day23/part2 b/day23/part2
new file mode 100755
index 0000000..28cabf8
--- /dev/null
+++ b/day23/part2
@@ -0,0 +1,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