about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--daily/303easy/README.md58
-rw-r--r--daily/303easy/ricochet.pas35
2 files changed, 93 insertions, 0 deletions
diff --git a/daily/303easy/README.md b/daily/303easy/README.md
new file mode 100644
index 0000000..b970342
--- /dev/null
+++ b/daily/303easy/README.md
@@ -0,0 +1,58 @@
+# [[2017-02-21] Challenge #303 [Easy] Ricochet](https://www.reddit.com/r/dailyprogrammer/comments/5vb1wf/20170221_challenge_303_easy_ricochet/)
+
+## Description
+
+Start with a grid _h_ units high by _w_ units wide. Set a point particle in
+motion from the upper-left corner of the grid, 45 degrees from the horizontal,
+so that it crosses from one corner of each unit square to the other. When the
+particle reaches the bounds of the grid, it ricochets and continues until it
+reaches another corner. 
+
+Given the size of the grid (_h_ and _w_), and the velocity (_v_) of the
+particle in unit squares per second, determine _C_: the corner where the
+particle will stop, _b_: how many times the particle ricocheted off the bounds
+of the grid, and _t_: the time it took for the particle to reach _C_.
+
+## Constraints
+
+The particle always starts from the upper-left corner of the grid (and will
+therefore always end up in one of the other corners).
+
+Since we'll be working with unit squares, _h_ and _w_ are always integers.
+
+## Formal Inputs & Outputs
+
+### Input description
+
+The input will be an arbitrary number of lines containing _h_, _w_, and _v_,
+each separated by spaces:
+
+     8 3 1
+     15 4 2
+
+### Output description
+
+For each line of input, your program should output a line containing _C_, _b_,
+and _t_, where _C_ can be UR, LR, or LL depending on where the particle ends
+up:
+
+     LL 9 24
+     UR 17 30
+
+## Bonus
+
+Instead of a particle, determine the behavior of a rectangle _m_ units high by
+_n_ units wide. Input should be as follows: _h_ _w_ _m_ _n_ _v_. So for a 10 by
+7 grid with a 3 by 2 rectangle, the input would be:
+
+     10 7 3 2 1
+
+The output format is the same:
+
+     LR 10 35
+
+# Finally
+
+Have a good challenge idea like /u/sceleris927 did?
+
+Consider submitting it to /r/dailyprogrammer_ideas
diff --git a/daily/303easy/ricochet.pas b/daily/303easy/ricochet.pas
new file mode 100644
index 0000000..5ebc179
--- /dev/null
+++ b/daily/303easy/ricochet.pas
@@ -0,0 +1,35 @@
+var
+  h, w, v, lcm: int64;
+
+
+function gcd(a, b: int64): int64;
+  var
+    c: int64;
+
+  begin
+    while b > 0 do
+      begin
+        c := b;
+        b := a mod b;
+        a := c
+      end;
+    gcd := a
+  end;
+
+
+begin
+  repeat
+    readln(h, w, v);
+    lcm := h * w div gcd(h, w);
+    if lcm div h mod 2 = 0 then
+      write('U')
+    else
+      write('L');
+    if lcm div w mod 2 = 0 then
+      write('L ')
+    else
+      write('R ');
+    write(lcm div h + lcm div w - 2);
+    writeln(' ', lcm div v)
+  until eof(input)
+end.