Skip to content

Instantly share code, notes, and snippets.

@PhiLhoSoft
Last active October 17, 2019 14:12
Show Gist options
  • Select an option

  • Save PhiLhoSoft/3a82ca0d9e9c462955f893ea9455481c to your computer and use it in GitHub Desktop.

Select an option

Save PhiLhoSoft/3a82ca0d9e9c462955f893ea9455481c to your computer and use it in GitHub Desktop.

Revisions

  1. PhiLhoSoft revised this gist Oct 17, 2019. 1 changed file with 99 additions and 0 deletions.
    99 changes: 99 additions & 0 deletions TOSA-MDF-2018-18-3 - Barrage fluvial.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    /*******
    * Read input from STDIN
    * Use: console.log() to output your result.
    * Use: console.error() to output debug information into STDERR
    * ***/
    const fs = require('fs');
    const readline = require('readline');

    const input = [];

    const readStream = fs.createReadStream('/_Dev/Concours/sample-BarrageFluvial/input1.txt', { autoClose: true });
    const readline_object = readline.createInterface({
    input: readStream, //process.stdin,
    output: process.stdout
    });
    // console.log(readline_object);
    readline_object
    .on("line", (value) => { //Read input values
    input.push(value);
    })
    .on("close", ContestResponse);

    // ContestResponse();

    let mapSize;

    function ContestResponse() {
    //implement your code here using input array
    mapSize = parseInt(input.shift(), 10);
    const map = input.map((line) => {
    const values = line.split('');
    return values.map((point) => point === '.' ? 0 : 1);
    });
    console.log(parseMap(map));
    }

    const deplacements = [[-1, 0], [1, 0], [0, -1], [0, 1]];

    function explore(map, point, result) {
    let found = false;
    const toExplore = [];
    do {
    found = false;
    for (let d of deplacements) {
    const px = point.x + d[0];
    const py = point.y + d[1];
    if (px < 0 || px >= mapSize || py < 0 || py >= mapSize) continue;
    if (map[py][px] === 1) {
    const neigh = { x: px, y: py };
    toExplore.push(neigh);
    result.push(neigh);
    found = true;
    map[py][px] = -1;
    }
    }
    toExplore.forEach((p) => explore(map, p, result));
    } while (found);
    }

    function parseMap(map) {
    const leftBank = [];
    const rightBank = [];
    for (x = 0; x < mapSize - 1; x++) {
    if (map[0][x] === 0 && map[0][x + 1] === 1) {
    rightBank.push({ x: x + 1, y: 0 });
    break;
    }
    }
    for (y = 0; y < mapSize - 1; y++) {
    if (map[y][0] === 0 && map[y + 1][0] === 1) {
    leftBank.push({ x: 0, y: y + 1 });
    break;
    }
    }
    const pr = rightBank[0];
    explore(map, pr, rightBank);
    const pl = leftBank[0];
    explore(map, pl, leftBank);

    let dist = mapSize * mapSize;
    const res = rightBank.reduce(
    (accR, rp) => {
    accR = leftBank.reduce(
    (accL, lp) => {
    const dx = lp.x - rp.x;
    const dy = lp.y - rp.y;
    const d = Math.hypot(dx, dy);
    // console.error('D', d, lp, rp);
    if (d < accL) {
    accL = d;
    }
    return accL;
    }, accR
    );
    return accR;
    }, dist
    )
    return Math.ceil(res);
    }
  2. PhiLhoSoft created this gist Oct 16, 2019.
    72 changes: 72 additions & 0 deletions TOSA-MDF-2018-12-1 - Intérêts débiteurs.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    /*******
    * Read input from STDIN
    * Use: console.log() to output your result.
    * Use: console.error() to output debug information into STDERR
    * ***/

    var input = [];

    // readline_object.on("line", (value) => { //Read input values
    // input.push(value);
    // })
    // //Call ContestResponse when all inputs are read
    // readline_object.on("close", ContestResponse);


    function ContestResponse() {
    //implement your code here using input array
    input = input.map((v) => parseInt(v, 10));
    // console.log('Input is', input);
    const days = input.shift();
    const accountStart = input.shift();
    console.error('D S N', days, accountStart, input.length);
    const oldI = oldMethod(days, accountStart, input);
    const newI = newMethod(days, accountStart, input);
    console.error('N O R', newI, oldI, newI - oldI)
    console.log(newI - oldI)
    return newI - oldI;
    }

    function oldMethod(days, accountStart, input) {
    let account = accountStart;
    let interestRate = 0.1;
    let interests = 0;
    let negativeDays = 0;
    for (let n = 0; n < days; n++) {
    const move = input[n];
    account += move;
    if (account < 0) {
    negativeDays++;
    } else {
    negativeDays = 0;
    }
    if (account < 0 && negativeDays > 2) {
    interests += -account * interestRate;
    }
    }
    return interests;
    }

    function newMethod(days, accountStart, input) {
    let account = accountStart;
    let interestRate = 0.2;
    let interests = 0;
    let negativeDays = 0;
    for (let n = 0; n < days; n++) {
    const move = input[n];
    account += move;
    if (account < 0) {
    negativeDays++;
    } else {
    negativeDays = 0;
    interestRate = 0.2;
    }
    if (negativeDays > 3) {
    interestRate = 0.3;
    }
    if (account < 0) {
    interests += -account * interestRate;
    }
    }
    return interests;
    }
    96 changes: 96 additions & 0 deletions TOSA-MDF-2018-12-2 - Saut à la perche.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    /*******
    * Read input from STDIN
    * Use: console.log() to output your result.
    * Use: console.error() to output debug information into STDERR
    * ***/
    const fs = require('fs');
    const readline = require('readline');

    // var input = [
    // '7',
    // 'aaaaaa 4.25 S',
    // 'bbbbbb 4.25 E',
    // 'bbbbbb 4.25 S',
    // 'bbbbbb 4.30 E',
    // 'aaaaaa 4.30 E',
    // 'aaaaaa 4.30 E',
    // 'aaaaaa 4.30 E',
    // ];

    // var input = [
    // '7',
    // 'aaaaaa 4.25 S',
    // 'bbbbbb 4.25 E',
    // 'aaaaaa 4.27 E',
    // 'bbbbbb 4.30 S',
    // 'aaaaaa 4.30 S',
    // 'bbbbbb 4.35 E',
    // 'aaaaaa 4.38 E',
    // ];
    const input = [];

    const readStream = fs.createReadStream('/_Dev/Concours/sample-SautPerche/input1.txt', { autoClose: true });
    const readline_object = readline.createInterface({
    input: readStream, //process.stdin,
    output: process.stdout
    });
    // console.log(readline_object);
    readline_object
    .on("line", (value) => { //Read input values
    input.push(value);
    })
    .on("close", ContestResponse);

    // ContestResponse();

    function ContestResponse() {
    //implement your code here using input array
    const tryNb = parseInt(input.shift(), 10);
    const tries = input.map((line) => {
    const values = line.split(' ');
    return { name: values[0].trim(), height: parseFloat(values[1]), result: values[2].trim() }
    });
    // console.error('Nb T', tryNb, tries);
    console.log(parseResults(tryNb, tries));
    }

    function parseResults(tryNb, tries) {
    const contestants = new Map();
    let height = 0;
    for (let n = 0; n < tryNb; n++) {
    const tr = tries[n];
    const contestant = contestants.get(tr.name) || { name: tr.name, totalFails: 0, height: 0, heightFailNb: 0 };
    if (tr.result === 'E') {
    contestants.set(tr.name, { ...contestant, totalFails: contestant.totalFails + 1, heightFailNb: contestant.heightFailNb + 1 });
    } else if (tr.height >= height) {
    height = tr.height;
    contestants.set(tr.name, { ...contestant, height: tr.height, heightFailNb: 0 });
    }
    }
    console.error('C', contestants);
    let contestant = {};
    let isKo = false;
    for (let c of contestants.values()) {
    if (c.height === height) {
    console.error('C', c, contestant)
    if (contestant.name) { // Previous winner
    if (contestant.heightFailNb > c.heightFailNb) {
    contestant = c;
    isKo = false;
    } else if (contestant.heightFailNb === c.heightFailNb) {
    if (contestant.totalFails > c.totalFails) {
    contestant = c;
    isKo = false;
    } else if (contestant.totalFails === c.totalFails) {
    isKo = true;
    }
    }
    } else {
    contestant = c;
    isKo = false;
    }
    }
    }
    console.error('CR', contestant);
    return isKo ? 'KO' : contestant.name;
    }