Advent of code 2016 day 12

Oforth examples. Feel free to post your own code.

Advent of code 2016 day 12

Postby sotolf » 12 Dec 2016 20:13

I did this day as well in oforth, I was misusing tvars quite a bit I think, but the code would be so clunky without them.

Advent of Code
--- Day 12: Leonardo's Monorail ---

You finally reach the top floor of this building: a garden with a slanted glass ceiling. Looks like there are no more stars to be had.

While sitting on a nearby bench amidst some tiger lilies, you manage to decrypt some of the files you extracted from the servers downstairs.

According to these documents, Easter Bunny HQ isn't just this building - it's a collection of buildings in the nearby area. They're all connected by a local monorail, and there's another building not far from here! Unfortunately, being night, the monorail is currently not operating.

You remotely connect to the monorail control systems and discover that the boot sequence expects a password. The password-checking logic (your puzzle input) is easy to extract, but the code it uses is strange: it's assembunny code designed for the new computer you just assembled. You'll have to execute the code and get the password.

The assembunny code you've extracted operates on four registers (a, b, c, and d) that start at 0 and can hold any integer. However, it seems to make use of only a few instructions:

cpy x y copies x (either an integer or the value of a register) into register y.
inc x increases the value of register x by one.
dec x decreases the value of register x by one.
jnz x y jumps to an instruction y away (positive means forward; negative means backward), but only if x is not zero.

The jnz instruction moves relative to itself: an offset of -1 would continue at the previous instruction, while an offset of 2 would skip over the next instruction.

For example:

cpy 41 a
inc a
inc a
dec a
jnz a 2
dec a

The above code would set register a to 41, increase its value by 2, decrease its value by 1, and then skip the last dec a (because a is not zero, so the jnz a 2 skips it), leaving register a at 42. When you move past the last instruction, the program halts.

After executing the assembunny code in your puzzle input, what value is left in register a?

Your puzzle answer was 318083.
--- Part Two ---

As you head down the fire escape to the monorail, you notice it didn't start; register c needs to be initialized to the position of the ignition key.

If you instead initialize register c to be 1, what value is now left in register a?

Your puzzle answer was 9227737.

Both parts of this puzzle are complete! They provide two gold stars: **


Code: Select all
\ registers
tvar: a
0 to a
tvar: b
0 to b
tvar: c
0 to c
tvar: d
0 to d

\ instruction pointer
tvar: ip
1 to ip

\ the program
tvar: program
"day12" File new dup open(File.READ) map( #yourself ) to program

: toBetween
  dup first swap second
  " to " swap + + ;

: ip++
  ip 1 + to ip ;

: cpy \ str --
  toBetween evaluate ip++ ;

: inc \ str --
  first dup " 1 + to " swap + + evaluate  ip++ ;

: dec \ str --
  first dup " 1 - to " swap + + evaluate  ip++ ;

: jnz \ str --
  map( #evaluate ) dup first if ip swap second + to ip else drop ip++ then ;

: parse \ str -- strstr
  words asArrayBuffer dup removeFirst ;

: run
  doWhile:
    [ ip program at
      parse evaluate
      ip program size <= ] ;
     
run "Register a: " . a .cr


This one actually took me less time than the python implementation that I did first, as always, oforth is fun :D
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Advent of code 2016 day 12

Postby Franck » 12 Dec 2016 21:37

Hi,

Cool !
Where can I find the input file "day12" with the instructions ?

Franck
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Re: Advent of code 2016 day 12

Postby sotolf » 13 Dec 2016 00:25

cpy 1 a
cpy 1 b
cpy 26 d
jnz c 2
jnz 1 5
cpy 7 c
inc d
dec c
jnz c -2
cpy a c
inc a
dec b
jnz b -2
cpy c b
dec d
jnz d -6
cpy 16 c
cpy 17 d
inc a
dec d
jnz d -2
dec c
jnz c -5

Here is the input that I got, there are some more on the advent of code reddit adventofcode.reddit.com :)
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Advent of code 2016 day 12

Postby Franck » 13 Dec 2016 05:57

Your code is cool !
And you do the right thing : create words which name are the instructions themselves.

Into #jnz, if you want to push values on the stack, you can use #apply instead of #map.

And #map will do thé open of thé file if it is closed.

One other way would be to be to let the Oforth interpreter do the job.
As instructions tokens are separated by space, the interpret can decode them the same way it decode Oforth code.

There is a word, #parse-name, that read the next name into the input source.
So the code could be :

Code: Select all
\ registers
tvar: a
0 to a
tvar: b
0 to b
tvar: c
1 to c
tvar: d
0 to d

\ instruction pointer
tvar: ip
1 to ip

: ip++
   ip 1+ to ip ;

: register
   parse-name Word find ;

: cpy
   parse-name asStringBuffer evaluate register put ip++ ;

: inc
   register dup at 1+ swap put ip++ ;

: dec
   register dup at 1- swap put ip++ ;

: jnz
   register at parse-name asInteger swap ifZero: [ drop 1 ]
   ip + to ip ;

: run
| program |
   "day12.txt" File new map( #yourself ) ->program
   while ( ip program size <= ) [ ip program at evaluate ] ;

run "Register a: " . a .cr


But your code is very cool !

Franck
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Re: Advent of code 2016 day 12

Postby sotolf » 13 Dec 2016 10:17

That's really cool, I was looking for something like #parse-name but I couldn't find it, I was trying to see how #tvar: and #import: was doing their magic, but they are probably using more internal code. I didn't get that.

That really lets you clean up the code a lot ;) That's also a very neat trick with #register :D

Like always I'm amazed over how neat the code can be, and that is good, because it gives me something to aspire to :)

Also storing the program in a local variable in the run function also makes it neater, one less pesky tvar to worry about ;)

so when I use #map I don't need open, that's neat :)

That code probably executes faster as well :)
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Advent of code 2016 day 12

Postby Franck » 13 Dec 2016 20:35

Yes, this code will be faster.

For #import: , this is not buit-in. You can find its definition into the "Package.of" file.

For #tvar: , it is built-in into the current version, but not into the next release, where the definition will be :

Code: Select all
: tvar:
   parse-name TVar new drop ;



Into the next release, you will be able to write :

Code: Select all
: reg:
   0 parse-name TVar new put ;

reg: a
reg: b
reg: c
reg: d


Franck
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Re: Advent of code 2016 day 12

Postby sotolf » 14 Dec 2016 12:56

That really looks like an improvement to the tvars :D

The nice thing about a forth is that it's so malleable that one can do stuff in so many ways, there is almost always possible to find a very elegant way of solving some task :D I just have to pick up some more experience with it, so that I can pick up these more elegant ways.

For #import: , this is not buit-in. You can find its definition into the "Package.of" file.

I was looking in prelude.of, stupid me :P
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53


Return to Oforth examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron