11 KiB
11 KiB
In [1]:
from notebook_preamble import D, J, V, define
from joy.library import SimpleFunctionWrapper
from joy.utils.stack import list_to_stack
@SimpleFunctionWrapper
def incr_at(stack):
'''Given a index and a sequence of integers, increment the integer at the index.
E.g.:
3 [0 1 2 3 4 5] incr_at
-----------------------------
[0 1 2 4 4 5]
'''
sequence, (i, stack) = stack
mem = []
while i >= 0:
term, sequence = sequence
mem.append(term)
i -= 1
mem[-1] += 1
return list_to_stack(mem, sequence), stack
D['incr_at'] = incr_atIn [2]:
J('3 [0 1 2 3 4 5] incr_at')[0 1 2 4 4 5]
In [3]:
from joy.library import DefinitionWrapper
DefinitionWrapper.add_definitions('''
get_value == [roll< at] nullary
incr_value == [[popd incr_at] unary] dip
add_value == [+] cons dipd
incr_step_count == [++] dip
AoC2017.5.0 == get_value incr_value add_value incr_step_count
''', D)In [4]:
define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec')In [5]:
J('0 0 [0 3 0 1 -3] F')5
In [6]:
DefinitionWrapper.add_definitions('''
init-index-and-step-count == 0 0 roll<
prepare-predicate == dup size [>=] cons [popop] swoncat
AoC2017.5.preamble == init-index-and-step-count prepare-predicate
AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec
''', D)In [7]:
J('[0 3 0 1 -3] AoC2017.5')5