Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 0k
- z sn # n = input size
- 25 sm # m = window size
- [ LRx q ] sX # Return macro, call cleanup and quit
- [ q ] sQ # Exit loop without cleanup
- # Loop: Read in input file
- [ Lis. LLs. ] SR # cleanup for loop
- [
- z 0 =X # process stack
- li 1- d si :a # pop into a @ --i
- lLx
- ] SL
- ln Si lLx
- # Loop: Initialize window
- [ LLs. s. ] SR # cleanup for loop
- [
- dd ;a r :w # w @ index = a @ index, index on top
- 1- d _1 =X # index--, break if -1
- lLx
- ] SL
- lm 1- lLx
- # Part 1
- [
- # Locals:
- [
- LCs. LSs. LLs. LAs.
- Lps. Lqs.
- ] SR
- 0 Sp # ptr for Shift window
- lm Sq # next_ptr for Shift window
- # Shift window
- [
- lq ;a lp :w # w @ ptr = a @ next_ptr
- lp 1+ lm% sp # ptr++ mod window size
- lq 1+ sq # next_ptr++
- ] SS
- # Check window
- [
- # Locals:
- [
- LTs. LIs. LJs.
- Lis. Ljs.
- ] SR
- [ 1 LRx 4Q ] ST
- 0 Si 1 Sj
- # inner loop
- [
- li;w lj;w + lq;a =T # found sum, return True
- lj 1+ d sj lm =Q # j++, break if j == window size
- lJx
- ] SJ
- # outer loop
- [
- li 1+ sj # j = i + 1
- lJx # call inner j loop
- li 1+ d si lm 1- =Q # i++, break if i == window size - 1
- lIx
- ] SI
- lIx
- 0 LRx # return False
- ] SC
- # Print Answer
- [
- [Part 1: ]n lq;a p # Print answer
- LRx q
- ] SA
- # Foreach Window
- [
- lCx 0 =A # Print and exit when check fails
- lSx lq ln 1+ =X # shift window, break at end
- lLx # loop
- ] SL
- lLx
- ] S1
- l1x
- # Part 2 (passing in part 1)
- [
- # Locals:
- [
- LWs. L+s. L-s.
- Lts. Lss. # target value, and running sum
- Lis. Ljs. # shifing window, i to j
- LKs. LLs. LHs.
- Lks. Lls. Lhs. # variables for finding min/low, max/high
- ] SR
- St # t = passed in top of stack
- 0 Si 0 Sj 0;a Ss # i, j = 0, s = a @ 0
- [
- lj 1+ d sj ;a ls + ss # sum += a @ (++j)
- ] S+
- [
- li d ;a ls r- ss 1+ si # sum -= a @ (i++)
- ] S-
- # Sliding window loop
- [
- ls lt =Q # break if sum == target
- ls lt >+ # too low, add next element
- ls lt <- # too high, remove first element
- lWx
- ] SW
- lWx
- [ d sl ] SL # update min/low
- [ d sh ] SH # update max/high
- # Loop for finding min/low and max/high
- [
- lk ;a
- d ll >L
- d lh <H
- lk 1+ d sk lj =Q
- lKx
- ] SK
- # init: low = target, high = 0, k = i
- lt Sl
- 0 Sh
- li Sk
- lKx
- [Part 2: ]n ll lh + ps.
- LRx
- ] S2
- l2x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement