~justusw/apljkmeetup

89cbc8b5a65146e59d02f35afdfbd3f99c3b9fa8 — Justus Perlwitz 1 year, 11 months ago 7a23228
2022-11-22: Implement lilypad
4 files changed, 107 insertions(+), 0 deletions(-)

A 2022-11-22/lilypad.ijs
A 2022-11-22/lilypad_expected.txt
A 2022-11-22/lilypad_in.txt
A lib/dict.ijs
A 2022-11-22/lilypad.ijs => 2022-11-22/lilypad.ijs +60 -0
@@ 0,0 1,60 @@
load 'lib/dict.ijs'
NB. https://codegolf.stackexchange.com/questions/111081/lily-pad-jumping
NB. format is
NB. t{test case num} for test cases
NB. t{test case num}e for expected results
in_p =: '2022-11-22/lilypad_in.txt'
in =: ". L: 0 'b' freads in_p
exp_p =: '2022-11-22/lilypad_expected.txt'
exp =: 'b' freads exp_p

NB. You can assume that your list is non-empty, and that all integers are
NB. greater than 0.

NB. some helpers
at =: [: > {

NB. format is r(esult) ; p(osition) ; d(irection) (0 left, 1 right) ; m(oves)
start =: d_init ; 0  ; [: +`-"0 ]
r =: 0 at ]
p =: 1 at ]
m =: 2 at ]
m_left =: 0 < [: # m

adv =: monad define
m_c =. {. m y
p_n =. (p y) + m_c
((p_n , | m_c) d_set (r y)) ; p_n ; }. m y
)

pond_run =: [: adv^:m_left^:_ [: start ]


pond_format =: monad define
min =. 1 <. (<./ d_keys rs =. r y)
zeroed =. ((d_keys rs) - min) ,. d_values rs
max =. >./ d_keys zeroed
holes_filled =. zeroed
for_ijk. i. max do.
    if. -. ijk d_has zeroed do.
        holes_filled =. (ijk , 0) d_set holes_filled
    end.
end.
sorted =. (/: d_keys holes_filled) { holes_filled
map =. monad define 'if. y -: 0 do. ''_'' else. ": y end. '
; map L:0 <"0 d_values sorted
)

pond =: [: pond_format [: pond_run ]

pond_test =: monad define
in =. > y { in
exp =. > y { exp
a =. pond in
exp -: a
)

test_all =: monad define
assert. pond_test"0 i. # y
)
test_all in

A 2022-11-22/lilypad_expected.txt => 2022-11-22/lilypad_expected.txt +8 -0
@@ 0,0 1,8 @@
8_2__2__6
____5
2_2
3124
_3125
8_2__2__6
___12__3__10____41__1216
_______________________________________________________________________________________________4___1002_2

A 2022-11-22/lilypad_in.txt => 2022-11-22/lilypad_in.txt +8 -0
@@ 0,0 1,8 @@
2 3 6 8 2
5
2 2
4 3 2 1
5 3 2 1
2 3 6 8 2
10 3 12 4 1 12 16
100 4 7 2 2

A lib/dict.ijs => lib/dict.ijs +31 -0
@@ 0,0 1,31 @@
d_init =: 0 2 $ 0
d_from =: [: ,: ]
d_empty =: 0 = [: # ]
d_keys =: 0 {"1 ]
d_values =: 1 {"1 ]
d_len =: [: # ]
d_index =: d_keys i. [
d_has =: d_len > d_index
d_get =: [: : (d_index { d_values)^:d_has
d_delat =: [: : ([: < [: < [: < [) { ]
d_del =: [: : (d_index d_delat ])^:d_has
d_set =: [: : ((0 { [) d_del ]) , [

NB. unit tests
d_test =: 4 2 $ i. 100
test =: monad define
assert. 1 = d_empty d_init
assert. (0 2 4 6) -: d_keys d_test
assert. (1 3 5 7) -: d_values d_test
assert. 4 = d_len d_test
assert. 1 = 2 d_index d_test
assert. 3 = 2 d_get d_test
assert. d_test -: 7 d_get d_test
assert. (0 2 6 ,. 1 3 7) -: 2 d_delat d_test
assert. (0 2 6 ,. 1 3 7) -: 4 d_del d_test
assert. (0 2 4 6 ,. 1 3 5 7) -: 5 d_del d_test
assert. (,: 1 0) -: ((1 0) d_set d_init)
assert. 5 = d_len (10 0 d_set d_test)
assert. 4 = d_len (6 0 d_set d_test)
)
test 0