Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
aoc2017/day23.txt
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
106 lines (94 sloc)
3.7 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################### ANNOTATED ASSEMBLY #################### | |
set b 65 # b = 65 | |
set c b # c = 65 | |
jnz a 2 # if a { | |
jnz 1 5 # | |
A: mul b 100 # mul_count += 1 | |
sub b -100000 # b = 106500 | |
set c b # | |
sub c -17000 # c = 123500 | |
# } | |
# while true { | |
B: set f 1 # f = 1 | |
set d 2 # d = 2 | |
# do { | |
E: set e 2 # e = 2 | |
D: set g d # do { | |
mul g e # mul_count += 1 | |
sub g b # | |
jnz g 2 # if d * e == b { | |
set f 0 # f = 0 | |
# } | |
C: sub e -1 # e++ | |
set g e # | |
sub g b # | |
jnz g -8 # } while e != b | |
sub d -1 # d++ | |
set g d # | |
sub g b # | |
jnz g -13 # } while d != b | |
jnz f 2 # if f == 0 { | |
sub h -1 # h++ | |
# } | |
F: set g b # | |
sub g c # | |
jnz g 2 # if b == c { | |
jnz 1 3 # return | |
# } | |
G: sub b -17 # b += 17 | |
jnz 1 -23 # } | |
#################### ANNOTATED C(ISH) #################### | |
b = 65 # if A: | |
c = 65 # b = 106500 | |
if a { # c = b + 17000 | |
mul_count += 1 # mul_count += 1 | |
b = 106500 # else: | |
c = 123500 # b = c = 65 | |
} # | |
while true { # for b in xrange(b, c + 1, 17): | |
f = 1 # f = 1 | |
d = 2 # mul_count += (b - 2) ** 2 | |
do { # for d in xrange(2, b): | |
e = 2 # | |
do { # for e in xrange(2, b): | |
mul_count += 1 # | |
if d * e == b { # if d * e == b: | |
f = 0 # f = 0 | |
} # | |
e++ # | |
} while e != b # | |
d++ # | |
} while d != b # | |
if f == 0 { # if f == 0: | |
h++ # h += 1 | |
} # | |
if b == c { # | |
return # | |
} # | |
b += 17 # | |
} # | |
#################### ANNOTATED PYTHON(ISH) #################### | |
if A: | |
b = 106500 | |
c = b + 17000 | |
mul_count += 1 | |
else: | |
b = c = 65 | |
for b in xrange(b, c + 1, 17): # h = len(filter(is_not_prime, | |
f = 1 # xrange(b, c + 1, 17))) | |
mul_count += (b - 2) ** 2 # | |
for d in xrange(2, b): # | |
for e in xrange(2, b): # mul_count = sum([ | |
if d * e == b: # (i - 2) ** 2 for i in xrange(...)]) | |
f = 0 # | |
if f == 0: # | |
h += 1 # | |
#################### ANSWERS #################### | |
Part 1: we just have one iteration, and are looking for mul_count, which is | |
(65 - 2) ** 2 = 63 ** 2 = 3969 | |
Part 2: we are looking for the number of primes in the given range, | |
106500 to 123500 inclusive, going in steps of 17. 106500 is 17 * 6264 + 12, so | |
we are looking for any i from 6264 to 7264 inclusive such that 17i + 12 is | |
prime. The following mathematica command gives the answer: | |
In[1]:= Length[Select[17 * Range[6264, 7264] + 12, Not@*PrimeQ]] | |
Out[1]:= 917 |