r/adventofcode • u/nneonneo • Dec 20 '19
Upping the Ante Intcode Primality Generator
I had some fun writing some Intcode tooling over the last few days - as a reverse engineer it's been rather fun taking apart the Intcode programs day by day (although I don't usually base my solutions on doing so).
Well, I figured it was time to build something slightly nontrivial in Intcode, so here's a fully functional prime-number generator in Intcode. It uses the Intcode "standard" ABI so it should be pretty straightforward to understand for anyone who's looked at other code. I've got a WIP assembler which has syntax too awful for publication, so I'll probably clean that up and post it after Christmas.
109,120,109,2,21101,2,0,-1,21201,-1,0,1,21101,19,0,0,1105,1,34,1206,1,24,204,-1,22101,1,-1,-1,1105,1,8,109,-2,99,109,4,21207,-3,2,-1,1205,-1,86,21101,2,0,-2,22207,-2,-3,-1,1206,-1,79,21201,-3,0,1,21201,-2,0,2,21101,69,0,0,1105,1,95,1206,1,86,21201,-2,1,-2,1105,1,47,21101,1,0,-3,1105,1,90,21101,0,0,-3,109,-4,2105,1,0,109,4,22207,-3,-2,-1,1205,-1,115,21202,-2,-1,-1,22201,-3,-1,-3,1105,1,97,109,-4,2105,1,0This program takes no input, and generates prime numbers in sequence using output instructions. Benchmark: my very unoptimized IntCode interpreter gets to ~5000 within about 10 seconds; I'm certain folks here can do much better.
(Bonus: per https://codegolf.meta.stackexchange.com/questions/2028/what-are-programming-languages/2073, this program qualifies Intcode as a "real programming language" for CodeGolf.SE purposes. I look forward to the first code golf competition in Intcode).
View all comments
Show parent comment
4
u/[deleted] Dec 20 '19
This uses Eratosthenes' prime sieve. It is possible to expand it automatically when you hit the boundary and still be much faster than trial division, but I'm not going to code that in intcode...
Here's the annotated source code, by the way:
It's converted into pure intcode with
echo (cut -c4- < primes.intcode | sed 's/ #.*//') | sed 's/ //g' | sed 's/,$//'.