Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
66
Task/Pascals-triangle-Puzzle/Nim/pascals-triangle-puzzle.nim
Normal file
66
Task/Pascals-triangle-Puzzle/Nim/pascals-triangle-puzzle.nim
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import math, strutils
|
||||
|
||||
var B_X, B_Y, B_Z : int = 0
|
||||
|
||||
type
|
||||
Block_Value = object
|
||||
Known : int
|
||||
X, Y, Z : int
|
||||
|
||||
let
|
||||
X: Block_Value = Block_Value(Known:0, X:1, Y:0, Z:0)
|
||||
Y: Block_Value = Block_Value(Known:0, X:0, Y:1, Z:0)
|
||||
Z: Block_Value = Block_Value(Known:0, X:0, Y:0, Z:1)
|
||||
|
||||
proc Add (L : var Block_Value, R : Block_Value) =
|
||||
# Symbolically adds one block to another
|
||||
L.Known = L.Known + R.Known
|
||||
L.X = L.X + R.X - R.Z # Z is excluded as n(Y - X - Z) = 0
|
||||
L.Y = L.Y + R.Y + R.Z
|
||||
|
||||
proc Add (L: var Block_Value, R: int) =
|
||||
# Symbolically adds a value to the block
|
||||
L.Known = L.Known + R
|
||||
|
||||
proc Image (N : Block_Value): string =
|
||||
# The block value, when X,Y,Z are known
|
||||
result = $(N.Known + N.X * B_X + N.Y * B_Y + N.Z * B_Z)
|
||||
|
||||
proc Solve_2x2 (A11: int, A12:int, B1:int, A21:int, A22:int, B2: int) =
|
||||
# Don't care about things, supposing an integer solution exists
|
||||
if A22 == 0:
|
||||
B_X = toInt(B2 / A21)
|
||||
B_Y = toInt((B1 - (A11*B_X)) / A12)
|
||||
else:
|
||||
B_X = toInt((B1*A22 - B2*A12) / (A11*A22 - A21*A12))
|
||||
B_Y = toInt((B1 - A11*B_X) / A12)
|
||||
B_Z = B_Y - B_X
|
||||
|
||||
var B : array [1..5, array[1..5, Block_Value]] # The lower triangle contains blocks
|
||||
|
||||
# The bottom blocks
|
||||
Add(B[5][1],X)
|
||||
Add(B[5][2],11)
|
||||
Add(B[5][3],Y)
|
||||
Add(B[5][4],4)
|
||||
Add(B[5][5],Z)
|
||||
|
||||
# Upward run
|
||||
for Row in countdown(4,1):
|
||||
for Column in 1 .. Row:
|
||||
Add (B[Row][Column], B[Row + 1][Column])
|
||||
Add (B[Row][Column], B[Row + 1][Column + 1])
|
||||
|
||||
# Now have known blocks 40=[3][1], 151=[1][1] and Y=X+Z to determine X,Y,Z
|
||||
Solve_2x2( B[1][1].X,
|
||||
B[1][1].Y,
|
||||
151 - B[1][1].Known,
|
||||
B[3][1].X,
|
||||
B[3][1].Y,
|
||||
40 - B[3][1].Known)
|
||||
|
||||
#Print the results
|
||||
for Row in 1..5:
|
||||
writeln(stdout,"")
|
||||
for Column in 1..Row:
|
||||
write(stdout, Image(B[Row][Column]), " ")
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
# set up triangle
|
||||
var rows = 5;
|
||||
var tri = rows.of {|i| i.of { Hash(x => 0, z => 0, v => 0, rhs => nil) } }
|
||||
tri[0][0]{:rhs} = 151;
|
||||
tri[2][0]{:rhs} = 40;
|
||||
tri[4][0]{:x} = 1;
|
||||
tri[4][1]{:v} = 11;
|
||||
tri[4][2]{:x} = 1;
|
||||
tri[4][2]{:z} = 1;
|
||||
tri[4][3]{:v} = 4;
|
||||
tri[4][4]{:z} = 1;
|
||||
|
||||
# aggregate from bottom to top
|
||||
for row in (tri.end -> downto(1)) {
|
||||
for col in (tri[row-1].range) {
|
||||
[:x, :z, :v].each { |key|
|
||||
tri[row-1][col]{key} = (tri[row][col]{key} + tri[row][col+1]{key})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# find equations
|
||||
var eqn = gather {
|
||||
for r in tri {
|
||||
for c in r {
|
||||
take([c{:x}, c{:z}, c{:rhs} - c{:v}]) if defined(c{:rhs})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# print equations
|
||||
say "Equations:";
|
||||
say " x + z = y";
|
||||
for x,z,y in eqn { say "#{x}x + #{z}z = #{y}" }
|
||||
|
||||
# solve
|
||||
var f = (eqn[0][1] / eqn[1][1]);
|
||||
for i in (0..2) { eqn[0][i] -= (f * eqn[1][i]) }
|
||||
f = (eqn[1][0] / eqn[0][0]);
|
||||
for i in (0..2) { eqn[1][i] -= (f * eqn[0][i]) }
|
||||
|
||||
# print solution
|
||||
say "Solution:";
|
||||
var x = (eqn[0][2] / eqn[0][0]);
|
||||
var z = (eqn[1][2] / eqn[1][1]);
|
||||
var y = (x + z);
|
||||
say "x=#{x}, y=#{y}, z=#{z}";
|
||||
Loading…
Add table
Add a link
Reference in a new issue