From 50a33169691669d6ebd3d5d8e3b570c32b50e8e6 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 6 Apr 2017 15:55:57 +0200 Subject: [PATCH] Adding easy puzzles --- puzzles/easy/ascii-art.cpp | 53 +++++++++++ puzzles/easy/chuck-norris.cpp | 69 ++++++++++++++ puzzles/easy/defibrillators.cpp | 110 +++++++++++++++++++++++ puzzles/easy/horse-racing-duals.cpp | 100 +++++++++++++++++++++ puzzles/easy/horse-racing-duals.sh | 12 +++ puzzles/easy/mars-lander-episode-1.cpp | 58 ++++++++++++ puzzles/easy/mime-type.cpp | 67 ++++++++++++++ puzzles/easy/onboarding.clj | 18 ++++ puzzles/easy/onboarding.cpp | 32 +++++++ puzzles/easy/power-of-thor-episode-1.clj | 33 +++++++ puzzles/easy/power-of-thor-episode-1.cpp | 49 ++++++++++ puzzles/easy/temperatures.cpp | 47 ++++++++++ puzzles/easy/the-descent.c | 35 ++++++++ puzzles/easy/the-descent.clj | 24 +++++ puzzles/easy/the-descent.cpp | 35 ++++++++ puzzles/easy/the-descent.java | 35 ++++++++ puzzles/easy/the-descent.lua | 24 +++++ puzzles/easy/the-descent.php | 32 +++++++ puzzles/easy/the-descent.sh | 16 ++++ 19 files changed, 849 insertions(+) create mode 100644 puzzles/easy/ascii-art.cpp create mode 100644 puzzles/easy/chuck-norris.cpp create mode 100644 puzzles/easy/defibrillators.cpp create mode 100644 puzzles/easy/horse-racing-duals.cpp create mode 100644 puzzles/easy/horse-racing-duals.sh create mode 100644 puzzles/easy/mars-lander-episode-1.cpp create mode 100644 puzzles/easy/mime-type.cpp create mode 100644 puzzles/easy/onboarding.clj create mode 100644 puzzles/easy/onboarding.cpp create mode 100644 puzzles/easy/power-of-thor-episode-1.clj create mode 100644 puzzles/easy/power-of-thor-episode-1.cpp create mode 100644 puzzles/easy/temperatures.cpp create mode 100644 puzzles/easy/the-descent.c create mode 100644 puzzles/easy/the-descent.clj create mode 100644 puzzles/easy/the-descent.cpp create mode 100644 puzzles/easy/the-descent.java create mode 100644 puzzles/easy/the-descent.lua create mode 100644 puzzles/easy/the-descent.php create mode 100644 puzzles/easy/the-descent.sh diff --git a/puzzles/easy/ascii-art.cpp b/puzzles/easy/ascii-art.cpp new file mode 100644 index 0000000..7aad2da --- /dev/null +++ b/puzzles/easy/ascii-art.cpp @@ -0,0 +1,53 @@ +// https://www.codingame.com/training/easy/ascii-art +// Xavier Morel - 2014-10-16 + +#include +#include +#include +#include + +using namespace std; + +vector letters; + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + int L; + cin >> L; cin.ignore(); + int H; + cin >> H; cin.ignore(); + string T; + getline(cin, T); + + + for (int i = 0; i < H; i++) { + string ROW; + getline(cin, ROW); + letters.push_back(ROW); + } + + for (char &c : T) { + // Handling char. + if (c >= 'a' && c <= 'z') { + c -= 'a'; + } else if (c >= 'A' && c <= 'Z') { + c -= 'A'; + } else { + c = 26; // ? + } + } + + // Write an action using cout. DON'T FORGET THE "<< endl" + // To debug: cerr << "Debug messages..." << endl; + + for (int i = 0; i < H; i++) { + for (char c : T) { + cout << letters[i].substr((c*L), L); + } + cout << endl; + } +} diff --git a/puzzles/easy/chuck-norris.cpp b/puzzles/easy/chuck-norris.cpp new file mode 100644 index 0000000..21dddf0 --- /dev/null +++ b/puzzles/easy/chuck-norris.cpp @@ -0,0 +1,69 @@ +// https://www.codingame.com/ide/puzzle/chuck-norris +// Xavier Morel - 2014-10-16 + +#include +#include +#include +#include + +using namespace std; + +void print_bits(bool bit, int len) +{ + static bool first_char = true; + + if (!first_char) { + cout << ' '; + } + + cout << '0'; + if (!bit) { + cout << '0'; + } + cout << ' '; + + for (int i = 0; i < len; i++) { + cout << '0'; + } + + first_char = false; +} + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + string MESSAGE; + getline(cin, MESSAGE); + + // Write an action using cout. DON'T FORGET THE "<< endl" + // To debug: cerr << "Debug messages..." << endl; + + bool bit; + int len = 0; + + for (char c : MESSAGE) { + // cerr << "Letter " << c << endl; + for (int w = 7; w > 0; w--) { + bool cur_bit = (c & 1 << (w - 1)); + if (len == 0 || bit == cur_bit) { + bit = cur_bit; + len++; + } else if (bit != cur_bit) { + // cerr << "printing " << len << " bits " << (bit ? '1' : '0') << endl; + print_bits(bit, len); + bit = cur_bit; + len = 1; + } + + // cerr << "weight " << w << ": " << (bit ? '1' : '0') << endl; + } + // cerr << "printing " << len << " bits " << (bit ? '1' : '0') << endl; + } + + print_bits(bit, len); + + cout << endl; +} diff --git a/puzzles/easy/defibrillators.cpp b/puzzles/easy/defibrillators.cpp new file mode 100644 index 0000000..471e4bf --- /dev/null +++ b/puzzles/easy/defibrillators.cpp @@ -0,0 +1,110 @@ +// https://www.codingame.com/ide/puzzle/defibrillators +// Xavier Morel - 2016-03-07 + +#include +#include +#include +#include + +#define M_PI (3.14159265358979323846) + +using namespace std; + +typedef struct { + double lon; + double lat; +} Coords; + +typedef struct { + int id; + string name; + string address; + string phone; // ? + Coords coords; +} Defib; + +void fillDefib(string s, Defib *out) +{ + size_t current; + size_t next = -1; + string tmp; + + current = next + 1; + next = s.find_first_of(";", current); + out->id = stoi(s.substr(current, next - current)); + + current = next + 1; + next = s.find_first_of(";", current); + out->name = s.substr(current, next - current); + + current = next + 1; + next = s.find_first_of(";", current); + out->address = s.substr(current, next - current); + + current = next + 1; + next = s.find_first_of(";", current); + out->phone = s.substr(current, next - current); + + current = next + 1; + next = s.find_first_of(";", current); + tmp = s.substr(current, next - current); + replace(tmp.begin(), tmp.end(), ',', '.'); + out->coords.lon = stod(tmp); + + current = next + 1; + next = s.find_first_of(";", current); + tmp = s.substr(current, next - current); + replace(tmp.begin(), tmp.end(), ',', '.'); + out->coords.lat = stod(tmp); +} + +double calcDist(Coords from, Coords to) +{ + double fLon = from.lon * M_PI / 180; + double fLat = from.lat * M_PI / 180; + double tLon = to.lon * M_PI / 180; + double tLat = to.lat * M_PI / 180; + + double x = (tLon - fLon) * cos((tLat + fLat) / 2); + double y = (tLat - fLat); + double res = (sqrt((x*x) + (y*y)) * 6371); + + return res; +} + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + string tmp; + Coords userCoords; + + cin >> tmp; cin.ignore(); + replace(tmp.begin(), tmp.end(), ',', '.'); + userCoords.lon = stod(tmp); + cin >> tmp; cin.ignore(); + replace(tmp.begin(), tmp.end(), ',', '.'); + userCoords.lat = stod(tmp); + + int N; + cin >> N; cin.ignore(); + + int nearest = -1; + double nearest_dist = 0; + + Defib defibs[N]; + for (int i = 0; i < N; i++) { + string DEFIB; + getline(cin, DEFIB); + fillDefib(DEFIB, &defibs[i]); + double dist = calcDist(userCoords, defibs[i].coords); + if (nearest < 0 || nearest_dist > dist) { + nearest = i; + nearest_dist = dist; + } + } + + cout << defibs[nearest].name << endl; +} diff --git a/puzzles/easy/horse-racing-duals.cpp b/puzzles/easy/horse-racing-duals.cpp new file mode 100644 index 0000000..7da1666 --- /dev/null +++ b/puzzles/easy/horse-racing-duals.cpp @@ -0,0 +1,100 @@ +// https://www.codingame.com/ide/puzzle/horse-racing-duals +// Xavier Morel - 2016-03-12 + +#include +#include +#include +#include + +using namespace std; + +bool two_equals = false; + +typedef struct elem_s { + int pow; + struct elem_s *p; + struct elem_s *n; +} elem; + +elem *create_elem(int pw) +{ + elem *cur = new elem(); + cur->p = 0; + cur->n = 0; + cur->pow = pw; + return cur; +} + +elem *add_elem(elem *first, int pw) +{ + if (!first) { + return create_elem(pw); + } + + elem *prev; + for (elem *tmp = first; tmp; tmp = tmp->n) { + if (pw == tmp->pow) { + two_equals = true; + return first; + } else if (pw < tmp->pow) { + elem *cur = create_elem(pw); + if (tmp->p) { + tmp->p->n = cur; + } else { + first = cur; + } + cur->p = tmp->p; + cur->n = tmp; + tmp->p = cur; + return first; + } + prev = tmp; + } + + elem *cur = create_elem(pw); + prev->n = cur; + cur->p = prev; + + return first; +} + +int main() +{ + int N; + cin >> N; cin.ignore(); + + elem *first = 0; + elem *cur; + + for (int i = 0; i < N; i++) { + int pw; + cin >> pw; cin.ignore(); + first = add_elem(first, pw); + } + + if (two_equals) { + cout << "0" << endl; + } else { + int least = -1; + elem *prev = 0; + for (elem *tmp = first; tmp; tmp = tmp->n) { + if (prev) { + delete prev; + } + if (tmp->n) { + int diff = tmp->n->pow - tmp->pow; + if (least == -1 || least > diff) { + least = diff; + } + } + prev = tmp; + } + if (prev) { + delete prev; + } + if (least < 0) { + least = 0; + } + cout << least << endl; + } +} diff --git a/puzzles/easy/horse-racing-duals.sh b/puzzles/easy/horse-racing-duals.sh new file mode 100644 index 0000000..425b5be --- /dev/null +++ b/puzzles/easy/horse-racing-duals.sh @@ -0,0 +1,12 @@ +# https://www.codingame.com/ide/puzzle/horse-racing-duals +# Xavier Morel - 2016-03-12 + +read N +for ((i=0; i0)); then + horses+=":" + fi + horses+=$Pi +done +echo $horses | tr ":" "\n" | sort -n | awk 'NR>1{print $1-p} {p=$1}' | sort -n | head -n 1 diff --git a/puzzles/easy/mars-lander-episode-1.cpp b/puzzles/easy/mars-lander-episode-1.cpp new file mode 100644 index 0000000..7661a5d --- /dev/null +++ b/puzzles/easy/mars-lander-episode-1.cpp @@ -0,0 +1,58 @@ +// https://www.codingame.com/ide/puzzle/mars-lander-episode-1 +// Xavier Morel - 2016-03-12 + +#include +#include +#include +#include + +using namespace std; + +typedef struct { + int x; + int y; +} Coords; + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + int N; // the number of points used to draw the surface of Mars. + cin >> N; cin.ignore(); + int landing_start = 0; + int landing_end = 0; + int landing_y; + + for (int i = 0; i < N; i++) { + int LAND_X; // X coordinate of a surface point. (0 to 6999) + int LAND_Y; // Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars. + + cin >> LAND_X >> LAND_Y; cin.ignore(); + if ((i == 0 || landing_y != LAND_Y) && (landing_end - landing_start < 1000)) { + landing_start = LAND_X; + landing_y = LAND_Y; + } else if (landing_y == LAND_Y) { + landing_end = LAND_X; + } + } + + // game loop + while (1) { + Coords pos; + int HS; // the horizontal speed (in m/s), can be negative. + int VS; // the vertical speed (in m/s), can be negative. + int F; // the quantity of remaining fuel in liters. + int R; // the rotation angle in degrees (-90 to 90). + int P; // the thrust power (0 to 4). + cin >> pos.x >> pos.y >> HS >> VS >> F >> R >> P; cin.ignore(); + + int power = 0; + if (VS < -39) { + power = 4; + } + + cout << "0 " << power << endl; // R P. R is the desired rotation angle. P is the desired thrust power. + } +} diff --git a/puzzles/easy/mime-type.cpp b/puzzles/easy/mime-type.cpp new file mode 100644 index 0000000..30da503 --- /dev/null +++ b/puzzles/easy/mime-type.cpp @@ -0,0 +1,67 @@ +// https://www.codingame.com/ide/puzzle/mime-type +// Xavier Morel - 2014-10-16 + +#include +#include +#include +#include +#include + +using namespace std; + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + int N; // Number of elements which make up the association table. + cin >> N; cin.ignore(); + int Q; // Number Q of file names to be analyzed. + cin >> Q; cin.ignore(); + + map mimes; + + for (int i = 0; i < N; i++) { + string EXT; // file extension + string MT; // MIME type. + cin >> EXT >> MT; cin.ignore(); + + for (char &c : EXT) { + if (c >= 'A' && c <= 'Z') { + c -= 'A' - 'a'; + } + } + mimes[EXT] = MT; + } + for (int i = 0; i < Q; i++) { + string FNAME; // One file name per line. + getline(cin, FNAME); + + string ext; + if (FNAME.find_last_of('.') != string::npos) { + ext = FNAME.substr(FNAME.find_last_of('.') + 1); + + for (char &c : ext) { + if (c >= 'A' && c <= 'Z') { + c -= 'A' - 'a'; + } + } + } else { + ext = ""; + } + + cerr << "File " << FNAME << " extension " << ext << endl; + + if (mimes.count(ext)) { + cout << mimes[ext] << endl; + } else { + cout << "UNKNOWN" << endl; + } + } + + // Write an action using cout. DON'T FORGET THE "<< endl" + // To debug: cerr << "Debug messages..." << endl; + + // cout << "UNKNOWN" << endl; // For each of the Q filenames, display on a line the corresponding MIME type. If there is no corresponding type, then display UNKNOWN. +} diff --git a/puzzles/easy/onboarding.clj b/puzzles/easy/onboarding.clj new file mode 100644 index 0000000..afe5b07 --- /dev/null +++ b/puzzles/easy/onboarding.clj @@ -0,0 +1,18 @@ +; https://www.codingame.com/ide/puzzle/onboarding +; Xavier Morel - 2016-03-04 + +(ns Player + (:gen-class)) + +(defn -main [& args] + (while true + (let [enemy1 (read) dist1 (read) enemy2 (read) dist2 (read)] + ; enemy1: name of enemy 1 + ; dist1: distance to enemy 1 + ; enemy2: name of enemy 2 + ; dist2: distance to enemy 2 + + ; Enter the code here + + (if (< dist1 dist2) (println enemy1) (println enemy2)) + ))) diff --git a/puzzles/easy/onboarding.cpp b/puzzles/easy/onboarding.cpp new file mode 100644 index 0000000..83eda6b --- /dev/null +++ b/puzzles/easy/onboarding.cpp @@ -0,0 +1,32 @@ +// https://www.codingame.com/ide/puzzle/onboarding +// Xavier Morel - 2016-03-04 + +#include + +using namespace std; + +int main() +{ + + // game loop + while (1) { + string enemy1; // name of enemy 1 + cin >> enemy1; cin.ignore(); + int dist1; // distance to enemy 1 + cin >> dist1; cin.ignore(); + string enemy2; // name of enemy 2 + cin >> enemy2; cin.ignore(); + int dist2; // distance to enemy 2 + cin >> dist2; cin.ignore(); + + // Write an action using cout. DON'T FORGET THE "<< endl" + + if (dist1 < dist2) { + cout << enemy1 << endl; + } else { + cout << enemy2 << endl; + } + // Enter the code here + + } +} diff --git a/puzzles/easy/power-of-thor-episode-1.clj b/puzzles/easy/power-of-thor-episode-1.clj new file mode 100644 index 0000000..bf3f5a5 --- /dev/null +++ b/puzzles/easy/power-of-thor-episode-1.clj @@ -0,0 +1,33 @@ +;; https://www.codingame.com/ide/puzzle/power-of-thor-episode-1 +;; Xavier Morel - 2016-03-12 + +(ns Player + (:gen-class)) + +; Auto-generated code below aims at helping you parse +; the standard input according to the problem statement. +; --- +; Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders. + +(defn -main [& args] + (let [lightX (read) lightY (read) initialTX (read) initialTY (read)] + ; lightX: the X position of the light of power + ; lightY: the Y position of the light of power + ; initialTX: Thor's starting X position + ; initialTY: Thor's starting Y position + (def thorX initialTX) + (def thorY initialTY) + (while true + (let [remainingTurns (read)] + (cond + (< lightY thorY) (do (print "N") (def thorY (- thorY 1))) + (> lightY thorY) (do (print "S") (def thorY (+ thorY 1)))) + (cond + (< lightX thorX) (do (print "W") (def thorX (- thorX 1))) + (> lightX thorX) (do (print "E") (def thorX (+ thorX 1))) + ) + (println "") + ; (binding [*out* *err*] + ; (println "Debug messages...")) + + )))) diff --git a/puzzles/easy/power-of-thor-episode-1.cpp b/puzzles/easy/power-of-thor-episode-1.cpp new file mode 100644 index 0000000..fd48a0a --- /dev/null +++ b/puzzles/easy/power-of-thor-episode-1.cpp @@ -0,0 +1,49 @@ +// https://www.codingame.com/ide/puzzle/power-of-thor-episode-1 +// Xavier Morel - 2014-10-16 + +#include +#include +#include +#include + +using namespace std; + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + int LX; // the X position of the light of power + int LY; // the Y position of the light of power + int TX; // Thor's starting X position + int TY; // Thor's starting Y position + cin >> LX >> LY >> TX >> TY; cin.ignore(); + + // game loop + while (1) { + int E; // The level of Thor's remaining energy, representing the number of moves he can still make. + cin >> E; cin.ignore(); + + // Write an action using cout. DON'T FORGET THE "<< endl" + // To debug: cerr << "Debug messages..." << endl; + + if (TY < LY) { + cout << "S"; + TY++; + } else if (TY > LY) { + cout << "N"; + TY--; + } + + if (TX > LX) { + cout << "W"; + TX++; + } else if (TX < LX) { + cout << "E"; + TX--; + } + + cout << endl; // A single line providing the move to be made: N NE E SE S SW W or NW + } +} diff --git a/puzzles/easy/temperatures.cpp b/puzzles/easy/temperatures.cpp new file mode 100644 index 0000000..d4de7af --- /dev/null +++ b/puzzles/easy/temperatures.cpp @@ -0,0 +1,47 @@ +// https://www.codingame.com/ide/puzzle/temperatures +// Xavier Morel - 2014-10-16 + +#include +#include +#include +#include + +using namespace std; + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + int N; // the number of temperatures to analyse + cin >> N; cin.ignore(); + + + + // Write an action using cout. DON'T FORGET THE "<< endl" + // To debug: cerr << "Debug messages..." << endl; + + if (N == 0) { + cout << "0"; + } else { + int minTemp; + for (int i = 0; i < N; i++) { + int temp; + cin >> temp; + + if (i == 0) { + minTemp = temp; + } else if ( + ((temp * temp) < (minTemp * minTemp)) || ( + (temp * temp) == (minTemp * minTemp) && minTemp < temp + ) + ) { + minTemp = temp; + } + } + + cout << minTemp; + } + cout << endl; +} diff --git a/puzzles/easy/the-descent.c b/puzzles/easy/the-descent.c new file mode 100644 index 0000000..713ba80 --- /dev/null +++ b/puzzles/easy/the-descent.c @@ -0,0 +1,35 @@ +// https://www.codingame.com/ide/puzzle/the-descent +// Xavier Morel - 2016-03-12 + +#include +#include +#include + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + + // game loop + while (1) { + int maxRange = 0; + int maxValue = 0; + for (int i = 0; i < 8; i++) { + int mountainH; // represents the height of one mountain, from 9 to 0. + scanf("%d", &mountainH); + if (mountainH > maxValue) { + maxValue = mountainH; + maxRange = i; + } + } + + // Write an action using printf(). DON'T FORGET THE TRAILING \n + // To debug: fprintf(stderr, "Debug messages...\n"); + + printf("%d\n", maxRange); // The number of the mountain to fire on. + } + + return 0; +} diff --git a/puzzles/easy/the-descent.clj b/puzzles/easy/the-descent.clj new file mode 100644 index 0000000..492e8dd --- /dev/null +++ b/puzzles/easy/the-descent.clj @@ -0,0 +1,24 @@ +;; https://www.codingame.com/ide/puzzle/the-descent +;; Xavier Morel - 2016-03-12 + +(ns Player + (:gen-class)) + ; Auto-generated code below aims at helping you parse + ; the standard input according to the problem statement. + +(defn -main [& args] + (while true + (def maxValue 0) + (def maxRange 0) + (loop [i 8] + (when (> i 0) + (let [mountainH (read)] + (when (> mountainH maxValue) + (def maxValue mountainH) + (def maxRange (- 8 i)) + ) + (recur (dec i))))) + ; (binding [*out* *err*] + ; (println "Debug messages...")) + ; The number of the mountain to fire on. + (println maxRange))) diff --git a/puzzles/easy/the-descent.cpp b/puzzles/easy/the-descent.cpp new file mode 100644 index 0000000..29e428f --- /dev/null +++ b/puzzles/easy/the-descent.cpp @@ -0,0 +1,35 @@ +// https://www.codingame.com/ide/puzzle/the-descent +// Xavier Morel - 2016-03-12 + +#include +#include +#include +#include + +using namespace std; + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +int main() +{ + + // game loop + while (1) { + int v = 0, r = 0; + for (int i = 0; i < 8; i++) { + int mountainH; // represents the height of one mountain, from 9 to 0. + cin >> mountainH; cin.ignore(); + if (mountainH > v) { + r = i; + v = mountainH; + } + } + + // Write an action using cout. DON'T FORGET THE "<< endl" + // To debug: cerr << "Debug messages..." << endl; + + cout << r << endl; // The number of the mountain to fire on. + } +} diff --git a/puzzles/easy/the-descent.java b/puzzles/easy/the-descent.java new file mode 100644 index 0000000..47f6eff --- /dev/null +++ b/puzzles/easy/the-descent.java @@ -0,0 +1,35 @@ +// https://www.codingame.com/ide/puzzle/the-descent +// Xavier Morel - 2016-03-12 + +import java.util.*; +import java.io.*; +import java.math.*; + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + **/ +class Player { + + public static void main(String args[]) { + Scanner in = new Scanner(System.in); + + // game loop + while (true) { + int maxValue = 0; + int maxRange = 0; + for (int i = 0; i < 8; i++) { + int mountainH = in.nextInt(); // represents the height of one mountain, from 9 to 0. + if (mountainH > maxValue) { + maxValue = mountainH; + maxRange = i; + } + } + + // Write an action using System.out.println() + // To debug: System.err.println("Debug messages..."); + + System.out.println(maxRange); // The number of the mountain to fire on. + } + } +} diff --git a/puzzles/easy/the-descent.lua b/puzzles/easy/the-descent.lua new file mode 100644 index 0000000..afc3f0f --- /dev/null +++ b/puzzles/easy/the-descent.lua @@ -0,0 +1,24 @@ +-- https://www.codingame.com/ide/puzzle/the-descent +-- Xavier Morel - 2016-03-12 + +-- Auto-generated code below aims at helping you parse +-- the standard input according to the problem statement. + + +-- game loop +while true do + v=0 + r=0 + for i=0,8-1 do + mountainH = tonumber(io.read()) -- represents the height of one mountain, from 9 to 0. + if mountainH > v then + v = mountainH + r = i + end + end + + -- Write an action using print() + -- To debug: io.stderr:write("Debug message\n") + + print(r) -- The number of the mountain to fire on. +end diff --git a/puzzles/easy/the-descent.php b/puzzles/easy/the-descent.php new file mode 100644 index 0000000..052666e --- /dev/null +++ b/puzzles/easy/the-descent.php @@ -0,0 +1,32 @@ + $maxVal) { + $maxVal = $mountainH; + $maxRange = $i; + } + } + + // Write an action using echo(). DON'T FORGET THE TRAILING \n + // To debug (equivalent to var_dump): error_log(var_export($var, true)); + + echo "$maxRange\n"; // The number of the mountain to fire on. +} diff --git a/puzzles/easy/the-descent.sh b/puzzles/easy/the-descent.sh new file mode 100644 index 0000000..53b7dbc --- /dev/null +++ b/puzzles/easy/the-descent.sh @@ -0,0 +1,16 @@ +# https://www.codingame.com/ide/puzzle/the-descent +# Xavier Morel - 2016-03-12 + +while true; do + mountains="" + for (( i=0; i<8; i++ )); do + # mountainH: represents the height of one mountain, from 9 to 0. + if ((i>0)); then + mountains+=";" + fi + read mountainH + mountains+=$mountainH":"$i + done + + echo $mountains | tr ";" "\n" | sort -n | tail -n 1 | cut -d ":" -f 2 +done