YAPC::EU 2018 Glasgow Update!

This commit is contained in:
Ingy döt Net 2018-08-17 15:15:24 +01:00
parent 22f33d4004
commit 4e2d22a71d
1170 changed files with 15042 additions and 3047 deletions

View file

@ -0,0 +1,73 @@
--This little ditty converts the pyramid to rules quite nicely, however I will concede
--that solving those two rules (18=2x+z and 73=5x+6z) and specifically converting them
--into xrule(35=7x) and zrule(56=7z) is somewhat amateurish - suggestions welcome.
sequence pyramid = {
{151},
{"",""},
{40,"",""},
{"","","",""},
{"x",11,"y",4,"z"}}
sequence rules = {}
-- each cell in the pyramid is either an integer final value or an equation.
-- initially the equations are strings, we substitute all with triplets of
-- the form {k,x,z} ie k+l*x+m*z, and known values < last row become rules.
for r=5 to 1 by -1 do
for c=1 to length(pyramid[r]) do
object prc = pyramid[r][c], equ
if prc="x" then prc = {0,1,0} -- ie one x
elsif prc="y" then prc = {0,1,1} -- ie one x plus one z
elsif prc="z" then prc = {0,0,1} -- ie one z
else
if prc="" or r<=4 then
-- examples: x+11 is {0,1,0}+{11,0,0} -> {11,1,0},
-- 11+y is {11,0,0}+{0,1,1} -> {11,1,1},
-- 40=""+"" is {40,0,0}={22,2,1} ==> {18,2,1}
equ = sq_add(pyramid[r+1][c],pyramid[r+1][c+1])
end if
if prc="" then prc = equ
else prc = {prc,0,0}
if r<=4 then
equ[1] = prc[1]-equ[1]
rules = append(rules,equ)
end if
end if
end if
pyramid[r][c] = prc
end for
end for
ppOpt({pp_Nest,1,pp_StrFmt,1})
?"equations"
pp(pyramid)
?"rules"
pp(rules)
puts(1,"=====\n")
if length(rules)!=2 then ?9/0 end if -- more work needed!?
-- admittedly this bit is rather amateurish, and maybe problem-specific:
sequence xrule = sq_sub(sq_mul(rules[1],rules[2][3]),sq_mul(rules[2],rules[1][3])),
zrule = sq_sub(sq_mul(rules[2],rules[1][2]),sq_mul(rules[1],rules[2][2]))
?{"xrule",xrule}
?{"zrule",zrule}
integer x = xrule[1]/xrule[2],
z = zrule[1]/zrule[3],
y = x+z
printf(1,"x = %d, y=%d, z=%d\n",{x,y,z})
-- finally evaluate all the equations and print it.
for r=1 to length(pyramid) do
for c=1 to length(pyramid[r]) do
integer {k, l, m} = pyramid[r][c]
pyramid[r][c] = k+l*x+m*z
end for
end for
pp(pyramid)

View file

@ -40,4 +40,5 @@
(+ 11 @Y @F)
(+ @Y 4 @G)
(+ 4 @Z @H)
(+ @X @Z @Y) )
(+ @X @Z @Y)
T )

View file

@ -1,18 +1,22 @@
/*REXX program solves a (Pascal's) "Pyramid of Numbers" puzzle given four values. */
/*┌───────────────────────────────────────────────────────────┐
answer
mid /
\ /
\ 151
\ ααα ααα
40 ααα ααα
ααα ααα ααα ααα
x 11 y 4 z
/ \
/ \
/ \
Find: x y z b d
*/
/*╔══════════════════════════════════════════════════╗
answer
mid /
\ /
\ 151
\ ααα ααα
40 ααα ααα
ααα ααα ααα ααα
x 11 y 4 z
/ \
/ \
/ \
Find: x y z b d
*/
do #=2; _=sourceLine(#) /* [↓] this DO loop shows (above) box.*/
if pos('#',_)\==0 then leave /*only display up to the above line. */
say sourceLine(#) /*display one line of the above box. */
end /*#*/ /* [↑] this is one cheap way for doc. */
parse arg b d mid answer . /*obtain optional variables from the CL*/
if b=='' | b=="," then b= 11 /*Not specified? Then use the default.*/
if d=='' | d=="," then d= 4 /* " " " " " " */
@ -21,13 +25,13 @@ if answer='' | answer=="," then answer= 151 /* " " " " "
pad= left('', 15) /*used for inserting spaces in output. */
big= answer - 4*b - 4*d /*calculate big number less constants*/
middle= mid - 2*b /* " middle " " " */
do x =-big to big
do y=-big to big
if x+y\==middle then iterate /*40 = x+2B+Y ──or── 40-2*11 = x+y */
do z=-big to big
if z \== y - x then iterate /*z has to equal y-x (y=x+z) */
if x+y*6+z == big then say pad 'x = ' x pad "y = " y pad 'z = ' z
end /*z*/
end /*y*/
end /*x*/ /*stick a fork in it, we're all done. */
say
do x =-big to big
do y=-big to big
if x+y\==middle then iterate /*40 = x+2B+Y ──or── 40-2*11 = x+y */
do z=-big to big
if z \== y - x then iterate /*z has to equal y-x (y=x+z) */
if x+y*6+z == big then say pad 'x = ' x pad "y = " y pad 'z = ' z
end /*z*/
end /*y*/
end /*x*/ /*stick a fork in it, we're all done. */