ShareMyScreen/AdventOfCode/2022/21/MonkeyMath

From J Wiki
Jump to navigation Jump to search

The Problem << >>

A really easy one. Make each line an assignment to a verb and evaluate it at the end. I'll add a rank ("_) to the last word to make sure they are all verbs, and use string replacement to J syntax.

   require 'strings'
   0: ([: ". '"_' ,~ ]);._2 ('/';'%';':';'=:') stringreplace LF ,~ wd 'clippaste'
0
   root''
152
 

The 0 was just to eat up the blank lines that are the result when (". y) produces a verb result.

Part 2 is just as easy. I redefine humn as ] so that the expression it appears in is not constant.

  humn =: ]
   root
pppw + sjmn
   pppw''

   sjmn''
150

pppw contains the ] and evaluates to empty on an empty argument, while sjmn has all scalar constant verbs and evaluates to a constant. I want to know what to put in for the ] to get the same constant value out of pppw. In other words, I want to apply the inverse of pppw.

   pppw^:_1 sjmn''
301

I'm glad I was using J to solve this.