0

I'm trying to solve this problem from Google's Code Jam 2008:

The problem is called Train Timetable and you can find the full explanation here: Code Jam - Train Timetable

Note: I've decided to solve the problem with Node.js.

My code is the next:

function timeToMinutes(time) {
  const timeArray = time.split(":");
  const hours = parseInt(timeArray[0]);
  const minutes = parseInt(timeArray[1]);
  const hoursInMinutes = hours * 60;
  const total = hoursInMinutes + minutes;
  return total;
}

function timetableFiller(NAB, NBA, array) {
  let timetable = {
    departuresFromA: [],
    arrivalsToB: [],
    departuresFromB: [],
    arrivalsToA: [],
  };
  for (let i = 0; i < NAB + NBA; i++) {
    let tempArr = [];
    tempArr = array[i].split(" ");

    if (i < NAB) {
      timetable.departuresFromA.push(tempArr[0]);
      timetable.arrivalsToB.push(tempArr[1]);
    } else {
      timetable.departuresFromB.push(tempArr[0]);
      timetable.arrivalsToA.push(tempArr[1]);
    }
  }
  return timetable;
}

function timetableToMinutes(timetable) {
  let timetableMinutes = {
    departuresFromA: [],
    arrivalsToB: [],
    departuresFromB: [],
    arrivalsToA: [],
  };

  for (const property in timetable) {
    timetable[property].map((element) =>
      timetableMinutes[property].push(timeToMinutes(element))
    );
  }

  return timetableMinutes;
}

function trainsNeededCounter(arrivalsFromDestiny, departuresFromOrigin, tat) {
  let trainsNeeded = departuresFromOrigin.length;
  for (let i = 0; i < arrivalsFromDestiny.length; i++) {
    for (let j = 0; j < departuresFromOrigin.length; j++) {
      if (arrivalsFromDestiny[i] + tat <= departuresFromOrigin[j]) {
        trainsNeeded = trainsNeeded - 1;
        departuresFromOrigin.splice(j, 1);
      }
    }
  }
  return trainsNeeded;
}

function responseGenerator(inputA, inputB, caseNumber) {
  return `Case #${caseNumber}: ${inputA} ${inputB}`;
}

function problemSolution(input) {
  const numberOfCases = parseInt(input[0]);
  input.shift();
  let response = [];
  let caseNumber = 0;
  let NAB;
  let NBA;
  for (let i = 0; i < input.length; i = i + NAB + NBA + 2) {
    caseNumber = caseNumber + 1;
    const tat = parseInt(input[i]);
    const arrayNTrips = input[i + 1].split(" ");
    NAB = parseInt(arrayNTrips[0]);
    NBA = parseInt(arrayNTrips[1]);
    const arraySchedule = input.slice(i + 2, i + 2 + NAB + NBA);
    const timetable = timetableFiller(NAB, NBA, arraySchedule);
    const timetableMinutes = timetableToMinutes(timetable);

    const trainsNeededAB = trainsNeededCounter(
      timetableMinutes.arrivalsToA,
      timetableMinutes.departuresFromA,
      tat
    );
    const trainsNeededBA = trainsNeededCounter(
      timetableMinutes.arrivalsToB,
      timetableMinutes.departuresFromB,
      tat
    );
    response.push(
      responseGenerator(trainsNeededAB, trainsNeededBA, caseNumber)
    );
  }

  return response;
}

function readInput() {
  const readline = require("readline");
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false,
  });
  let problem = [];
  rl.on("line", (line) => {
    problem.push(line);
  }).on("close", () => {
    const solution = problemSolution(problem);
    solution.map((response) => console.log(response));
  });
}

readInput();

How to replicate the issue

  1. You should login into Code Jam with your Google account.
  2. Paste into the code area on the right side and activate the Test run mode.
  3. As input you can copy paste the sample input provided in the problem and you can see that the output is exactly as the sample output.

I've tried with my own variations of the input and the responses seems correct but when I run the real attempt the platform says WA or Wrong Answer.

Thank you so much for your help!

Javier
  • 1

1 Answers1

0

I made a video about this recently. You should check it out. I think you can understand the logic flow from it. We are both doing the same thing basically. https://youtu.be/_Cp51vMDZAs -check this out

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void solve(int t)
{
int NA, NB;
float T;
cin >> T >> NA >> NB;
cin.ignore();
vector<string> ASchedule, BSchedule;
if (NA > 0)
    for (int i = 0; i < NA; i++)
    {
        string s;
        getline(cin, s);
        ASchedule.push_back(s);
    }
if (NB > 0)
    for (int i = 0; i < NB; i++)
    {
        string s;
        getline(cin, s);
        BSchedule.push_back(s);
    }
int alength, blength;
alength = (int)ASchedule.size();
blength = (int)BSchedule.size();
if (alength == 0 || blength == 0)
{
    cout << "Case #" << t << ": " << alength << " " << blength << endl;
    return;
}
float TT = T / 10;
string val, value;
int d;
float ADH, ADM, AAH, AAM, BDH, BDM, BAH, BAM;
vector<float> AD, AA, BD, BA;
for (int i = 0; i < alength; i++)
{
    val = ASchedule[i];
    ADH = stof(val.substr(0, 2));
    AAH = stof(val.substr(6, 2));
    ADM = stof(val.substr(3, 2));
    AAM = stof(val.substr(9, 2));
    if (val.at(9) == '0')
    {
        AAM /= 10;
        AAM += TT;
        AAM *= 10;
    }
    else
        AAM += T;
    if (AAM > 59)
    {
        d = -1;
        while (AAM != 59)
        {
            AAM -= 1;
            d++;
        }
        AAH++;
        AAM = 0;
        AAM += d;
    }
    // if (ADH > 23)
    //     ADH = 0;
    // if (AAH > 23)
    //     AAH = 0;
    ADM /= 100;
    ADH += ADM;
    AAM /= 100;
    AAH += AAM;
    AD.push_back(ADH);
    AA.push_back(AAH);
}
for (int j = 0; j < blength; j++)
{
    value = BSchedule[j];
    BDH = stof(value.substr(0, 2));
    BDM = stof(value.substr(3, 2));
    BAH = stof(value.substr(6, 2));
    BAM = stof(value.substr(9, 2));
    if (value.at(9) == '0')
    {
        BAM /= 10;
        BAM += TT;
        BAM *= 10;
    }
    else
        BAM += T;

    if (BAM > 59)
    {
        d = -1;
        while (BAM != 59)
        {
            BAM -= 1;
            d++;
        }
        BAH++;
        BAM = 0;
        BAM += d;
    }
    // if (BDH > 23)
    //     BDH = 0;
    // if (BAH > 23)
    //     BAH = 0;
    BDM /= 100;
    BDH += BDM;
    BAM /= 100;
    BAH += BAM;
    BA.push_back(BAH);
    BD.push_back(BDH);
}
int no1 = alength, no2 = blength;
sort(BD.begin(), BD.end());
sort(BA.begin(), BA.end());
sort(AA.begin(), AA.end());
sort(AD.begin(), AD.end());
for (int i = 0; i < alength; i++)
    for (int j = 0; j < blength; j++)
        if (AD[i] >= BA[j])
        {
            no1--;
            BA[j] = 50;
            break;
        }
for (int i = 0; i < blength; i++)
    for (int j = 0; j < alength; j++)
        if (AA[j] <= BD[i])
        {
            no2--;
            AA[j] = 50;
            break;
        }
cout << "Case #" << t << ": " << no1 << " " << no2 << endl;
}
int main()
{
int N;
cin >> N;
cin.ignore();
for (int t = 1; t <= N; t++)
    solve(t);
}
  • Could you share your solution with me? I will try to translate from C++ to Node.js to figure out if the problem is my solution or the Code Jam platform. – Javier Apr 20 '21 at 00:32