1 min read

APL Hacking: Project Euler Daily (#17)

This one I did the old fashioned way at first, with a loop and a lot of cases and branching. Either with branch arrows or with Control Structures, it felt wrong. In the end, I realized that I could condense things down a bit if I removed most of the casing and made the casing implicit in the expressions. That makes for only a single loop and a few lines that are easier to read.

On the other hand, I still think that I am missing something. I feel like the solution is right there in the front, but I just haven't grasped it or spent the time on it.

Problem #17:

∇R←PESEVENTEEN;D;I;W;WT;⎕IO
⎕IO←0

W←'' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight'
W←W,'nine' 'ten' 'eleven' 'twelve' 'thirteen' 'fourteen'
W←W,'fifteen' 'sixteen' 'seventeen' 'eighteen' 'nineteen'

WT←'' 'ten' 'twenty' 'thirty' 'forty' 'fifty' 'sixty'
WT←WT,'seventy' 'eighty' 'ninety'

I←1 ⋄ R←0

NEXT:
R←R+(⍴⊃W[D])+⍴(0≠D)/'thousand'⊣D←10|⌊I÷1000
R←R+(⍴⊃W[D])+⍴(0≠D)/'hundred'⊣D←10|⌊I÷100
R←R+⍴(~(0=100|I)∨0=⌊I÷100)/'and'
R←R+⊃(((⍴⊃WT[10|⌊I÷10])+⍴⊃W[10|I])(⍴⊃W[19⌊D]))[20>D]⊣D←100|I
→(I=1000)/0 ⋄ I←I+1 ⋄ →NEXT