Skip to content

Commit

Permalink
Added 2010 Solutions
Browse files Browse the repository at this point in the history
Wow, Turing, Python, AND Java! Cool!
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent 3f7bace commit ac9b7af
Show file tree
Hide file tree
Showing 13 changed files with 1,262 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 2010/Junior/J1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
% J1: 2010: What is n, Daddy?
%
% big if (or a formula if you prefer)
%

var n : int

get n

% it's just a big if.
if n = 1 then
put 1
elsif n = 2 then
put 2
elsif n = 3 then
put 2
elsif n = 4 then
put 3
elsif n = 5 then
put 3
elsif n = 6 then
put 3
elsif n = 7 then
put 2
elsif n = 8 then
put 2
elsif n = 9 then
put 1
elsif n = 10 then
put 1
end if

% alternative to the big if: calculate answer
put 3 - abs (5 - n) div 2
58 changes: 58 additions & 0 deletions 2010/Junior/J2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
% J2: 2010: Up and Down
%
% loops and decisions and keeping track of things.
%
% Process each person separately.
% Track both the distance and steps for each.
% Quit JUST before the last set
% and then add (or subtract) the remaining steps.
%

var a, b, c, d, s : int
var nikkySteps, nikkyDistance : int
var byronSteps, byronDistance : int
var next, sgn : int

get a, b, c, d, s

nikkySteps := 0
nikkyDistance := 0
next := a
sgn := 1
loop
exit when nikkySteps + next >= s
nikkySteps := nikkySteps + next
nikkyDistance := nikkyDistance + sgn * next
if sgn = 1 then
next := b
else
next := a
end if
sgn := sgn * -1
end loop
nikkyDistance := nikkyDistance + sgn * (s - nikkySteps)

byronSteps := 0
byronDistance := 0
next := c
sgn := 1
loop
exit when byronSteps + next >= s
byronSteps := byronSteps + next
byronDistance := byronDistance + sgn * next
if sgn = 1 then
next := d
else
next := c
end if
sgn := sgn * -1
end loop
byronDistance := byronDistance + sgn * (s - byronSteps)

if nikkyDistance > byronDistance then
put "Nikky"
elsif nikkyDistance < byronDistance then
put "Byron"
else
put "Tied"
end if
81 changes: 81 additions & 0 deletions 2010/Junior/J3.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
% J3: 2010: Punchy
%
% straight forward loop with if's
%

var a, b : int
var instruction : int
var var1, var2 : string (1)
var n : int

a := 0
b := 0

loop
get instruction
exit when instruction = 7
if instruction = 1 then
get var1
get n
if var1 = "A" then
a := n
else
b := n
end if
elsif instruction = 2 then
get var1
if var1 = "A" then
put a
else
put b
end if
else % it's 3 4 5 6 (+ * - /)
get var1
get var2
if var1 = "A" then
if var2 = "A" then
if instruction = 3 then
a := a + a
elsif instruction = 4 then
a := a * a
elsif instruction = 5 then
a := a - a
else
a := a div a
end if
else
if instruction = 3 then
a := a + b
elsif instruction = 4 then
a := a * b
elsif instruction = 5 then
a := a - b
else
a := a div b
end if
end if
else
if var2 = "A" then
if instruction = 3 then
b := b + a
elsif instruction = 4 then
b := b * a
elsif instruction = 5 then
b := b - a
else
b := b div a
end if
else
if instruction = 3 then
b := b + b
elsif instruction = 4 then
b := b * b
elsif instruction = 5 then
b := b - b
else
b := b div b
end if
end if
end if
end if
end loop
54 changes: 54 additions & 0 deletions 2010/Junior/J4.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
% J4: 2010: Global Warming
%
% while not totally obvious from the given examples
% you need to find a repeated parttern, STARTING FROM THE FIRST
% position in the difference array.
%
% To find that pattern length, try pattern lengths of 1, 2, 3, ..
% if its a true pattern, then
% diff(a) and diff(a+pattern) (for all a >=1) should be equal.



var n, a, b : int
var diff : array 1 .. 19 of int
var pattern : int

loop
get n
exit when n = 0

% handle the case of size 1
if n = 1 then
get a
put 0
else

% normal case, get differences
get a
for i : 1 .. n - 1
get b
diff (i) := b - a
a := b
end for

% try a pattern length of 1, 2,3... until one works.
pattern := 1
loop
a := 1
loop
exit when a + pattern > n - 1 or diff (a) not= diff (a + pattern)
a := a + 1
end loop

% you've found the correct pattern if a is at the end.
% ie. you've tried them all and diif(a) = diff (a+pattern)
exit when a + pattern > n - 1

% okay, try the next pattern length
pattern := pattern + 1
end loop
put pattern
end if
end loop

44 changes: 44 additions & 0 deletions 2010/Junior/J5_1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
% J5 2010; Knight Hop
%
% Due to Peter Sun and Wonjohn Choi
% of St Francis Xavier SS
%
% straight forward recursive approach
%

var board : array 1 .. 8, 1 .. 8 of int

procedure knightMove (x : int, y : int, steps : int)
if x > 0 and x <= 8 and y > 0 and y <= 8 and steps < board (x, y) then
board (x, y) := steps
knightMove (x - 1, y + 2, steps + 1)
knightMove (x - 1, y - 2, steps + 1)
knightMove (x + 1, y + 2, steps + 1)
knightMove (x + 1, y - 2, steps + 1)
knightMove (x - 2, y + 1, steps + 1)
knightMove (x - 2, y - 1, steps + 1)
knightMove (x + 2, y + 1, steps + 1)
knightMove (x + 2, y - 1, steps + 1)
end if
end knightMove


var xStart, yStart : int
var xEnd, yEnd : int

% get input
get xStart, yStart, xEnd, yEnd

% initialize the board (99999 = haven't got there yet)
for i : 1 .. 8
for j : 1 .. 8
board (i, j) := 99999
end for
end for

% move everywhere possible
knightMove (xStart, yStart, 0)

% answer is on the board
put board (xEnd, yEnd)

116 changes: 116 additions & 0 deletions 2010/Junior/J5_2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
% J5 2010; Knight Hop
%
% 2D array for the board
% 1D array of points to do the BFS
%

class Point
export var x, var y, setValue
var x, y : int

procedure setValue (a, b : int)
x := a
y := b
end setValue
end Point


var board : array 1 .. 8, 1 .. 8 of int

var p : array 1 .. 64 of ^Point
var q : array 1 .. 64 of ^Point
var psize, qsize : int

var x, y : int
var sx, sy : int
var a, b, step : int

% get input
get x, y, sx, sy

%initialize array of points (tracks latest moves)
new p (1)
p (1) -> setValue (x, y)
psize := 1

% initialize the board (99999 = haven't got there yet)
for i : 1 .. 8
for j : 1 .. 8
board (i, j) := 99999
end for
end for
board (x, y) := 0

%move EVERYWHERE possible
step := 1
loop
exit when psize = 0

% move to the next spots and remember where you moved to in q
qsize := 0
for i : 1 .. psize
a := p (i) -> x
b := p (i) -> y

% try all 8 possible moves
if a + 1 <= 8 and b + 2 <= 8 and step < board (a + 1, b + 2) then
board (a + 1, b + 2) := step
qsize := qsize + 1
new q (qsize)
q (qsize) -> setValue (a + 1, b + 2)
end if
if a + 2 <= 8 and b + 1 <= 8 and step < board (a + 2, b + 1) then
board (a + 2, b + 1) := step
qsize := qsize + 1
new q (qsize)
q (qsize) -> setValue (a + 2, b + 1)
end if
if a + 2 <= 8 and b - 1 >= 1 and step < board (a + 2, b - 1) then
board (a + 2, b - 1) := step
qsize := qsize + 1
new q (qsize)
q (qsize) -> setValue (a + 2, b - 1)
end if
if a + 1 <= 8 and b - 2 >= 1 and step < board (a + 1, b - 2) then
board (a + 1, b - 2) := step
qsize := qsize + 1
new q (qsize)
q (qsize) -> setValue (a + 1, b - 2)
end if
if a - 1 >= 1 and b - 2 >= 1 and step < board (a - 1, b - 2) then
board (a - 1, b - 2) := step
qsize := qsize + 1
new q (qsize)
q (qsize) -> setValue (a - 1, b - 2)
end if
if a - 2 >= 1 and b - 1 >= 1 and step < board (a - 2, b - 1) then
board (a - 2, b - 1) := step
qsize := qsize + 1
new q (qsize)
q (qsize) -> setValue (a - 2, b - 1)
end if
if a - 2 >= 1 and b + 1 <= 8 and step < board (a - 2, b + 1) then
board (a - 2, b + 1) := step
qsize := qsize + 1
new q (qsize)
q (qsize) -> setValue (a - 2, b + 1)
end if
if a - 1 >= 1 and b + 2 <= 8 and step < board (a - 1, b + 2) then
board (a - 1, b + 2) := step
qsize := qsize + 1
new q (qsize)
q (qsize) -> setValue (a - 1, b + 2)
end if
end for

% get set for the next round of moving
% (its one more step and set p = q)
step := step + 1
psize := qsize
for i : 1 .. psize
p (i) := q (i)
end for
end loop

% answer in on the board
put board (sx, sy)
Loading

0 comments on commit ac9b7af

Please sign in to comment.