Adding easy puzzles

This commit is contained in:
Xavier Morel
2017-04-06 15:55:57 +02:00
parent 2df5dcf1e3
commit 50a3316969
19 changed files with 849 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
// https://www.codingame.com/training/easy/ascii-art
// Xavier Morel - 2014-10-16
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> 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;
}
}

View File

@@ -0,0 +1,69 @@
// https://www.codingame.com/ide/puzzle/chuck-norris
// Xavier Morel - 2014-10-16
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
}

View File

@@ -0,0 +1,110 @@
// https://www.codingame.com/ide/puzzle/defibrillators
// Xavier Morel - 2016-03-07
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#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;
}

View File

@@ -0,0 +1,100 @@
// https://www.codingame.com/ide/puzzle/horse-racing-duals
// Xavier Morel - 2016-03-12
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
}
}

View File

@@ -0,0 +1,12 @@
# https://www.codingame.com/ide/puzzle/horse-racing-duals
# Xavier Morel - 2016-03-12
read N
for ((i=0; i<N; i++)); do
read Pi
if ((i>0)); 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

View File

@@ -0,0 +1,58 @@
// https://www.codingame.com/ide/puzzle/mars-lander-episode-1
// Xavier Morel - 2016-03-12
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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.
}
}

View File

@@ -0,0 +1,67 @@
// https://www.codingame.com/ide/puzzle/mime-type
// Xavier Morel - 2014-10-16
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
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<string, string> 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.
}

View File

@@ -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))
)))

View File

@@ -0,0 +1,32 @@
// https://www.codingame.com/ide/puzzle/onboarding
// Xavier Morel - 2016-03-04
#include <iostream>
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
}
}

View File

@@ -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..."))
))))

View File

@@ -0,0 +1,49 @@
// https://www.codingame.com/ide/puzzle/power-of-thor-episode-1
// Xavier Morel - 2014-10-16
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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
}
}

View File

@@ -0,0 +1,47 @@
// https://www.codingame.com/ide/puzzle/temperatures
// Xavier Morel - 2014-10-16
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
}

View File

@@ -0,0 +1,35 @@
// https://www.codingame.com/ide/puzzle/the-descent
// Xavier Morel - 2016-03-12
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* 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;
}

View File

@@ -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)))

View File

@@ -0,0 +1,35 @@
// https://www.codingame.com/ide/puzzle/the-descent
// Xavier Morel - 2016-03-12
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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.
}
}

View File

@@ -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.
}
}
}

View File

@@ -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

View File

@@ -0,0 +1,32 @@
<?php
// 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) {
$maxVal = 0;
$maxRange = 0;
for ($i = 0; $i < 8; $i++) {
fscanf(
STDIN,
"%d",
$mountainH // represents the height of one mountain, from 9 to 0.
);
if ($mountainH > $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.
}

View File

@@ -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