Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Command: tr -d ':' <input | dc -fdc-p2.dc
- [ 0* ] sZ
- [ lt+ ] sT
- # exit recursion: idx=0 acc -> acc or 0
- [
- + dlt!=Z # zero top of stack it acc != target
- q
- ] sX
- # recursion: stack frame: acc idx; return acc or 0
- [
- d lt<X # prune if acc > targ
- r 1-d 0=X # idx--, check base case, stack: idx acc
- d3Rd # acc acc idx idx
- 3R d;l 3R+ # list(idx)+acc idx acc idx
- lRx # res acc idx
- _3R # tuck res
- d3Rd # idx idx acc acc res+
- d;l 4R*
- lRx # res* idx acc res+
- _3R # tuck res
- d;l
- dZ10r^ 4R*+ # concat acc||val
- lRx # res|| res* res+
- ++ # "or" results with +
- ] sR
- 0
- ?
- [
- 0
- [
- 1+
- d3R r:l # store list (backwards n to 1)
- z3<I
- ] dsIx # stack: n=size-of-list target sum
- rst # target = input(0); stack: n sum
- d;l # acc = list(n) (first item); stack: acc i=n sum
- lRx # res sum
- 0d 3R>T # res = (res > 0) ? targ : 0
- + # sum += res
- ? z1<M
- ] dsMx
- [Part 2: ]n p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement