Math Haters

Problem

Math-hater is short for a person who has given up on mathematics. Three math-haters are trying to take all math problems for the practice test. Math-haters are taken from question 1 to the last question as follows.

Haters Answers
Math-hater 1  1, 2, 3, 4, 5, 1, 2, 3, 4, 5, …
Math-hater 2  2, 1, 2, 3, 2, 4, 2 , 5, 2, 1, 2, 3, 2, 4, 2, 5, …
Math-hater 3  3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3 , 3, 1, 1, 2, 2, 4, 4, 5, 5, …

When an array answers with the correct answers from the 1st question to the last question are given in order, write a solution function to return the person who got the most questions in an array.

Constraint

  • The exam consists of up to 10,000 questions.
  • The correct answer to the question is one of 1, 2, 3, 4, 5.
  • If there are multiple people with the highest score, sort the returned values ​​in ascending order.

I/O example

answer return
[1, 2, 3, 4, 5] [1]
[1, 3, 2, 4, 2] [1,2,3]

I/O example explanation

  • Math-hater 1 got all the problems right.
  • Math-hater 2 is wrong in all matters.
  • Math-hater 3 is wrong in all matters.

So, the one who gets the most out of the question is Math-hater 1.

I/O example #2

Everyone got 2 questions at a time.

My Solution

function solution(answers) {
	let p1 = [1, 2, 3, 4, 5];
	let p2 = [2, 1, 2, 3, 2, 4, 2, 5];
	let p3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
	let mathHaters = [p1, p2, p3];
	let scores = [];

	mathHaters.forEach((hater) => {
		let loopCnt = parseInt(answers.length / hater.length);
		let remainCnt = answers.length % hater.length;
		let haterAnswers = [];
		let score = 0;

		if (loopCnt) {
			for (let i = 0; i < loopCnt; i++) {
				haterAnswers.push(...hater);
			}
		}

		if (remainCnt) {
			for (let j = 0; j < remainCnt; j++) {
				haterAnswers.push(hater[j]);
			}
		}

		for (let n in answers) {
			if (answers[n] === haterAnswers[n]) score++;
		}

		scores.push(score);
	});

	let maxScore = Math.max(...scores);
	let winners = [];

	scores.map((score, idx) => {
		if (score === maxScore) winners.push(idx + 1);
	});

	return winners;
}

Best Solution

function solution(answers) {
	var answer = [];
	var a1 = [1, 2, 3, 4, 5];
	var a2 = [2, 1, 2, 3, 2, 4, 2, 5];
	var a3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

	var a1c = answers.filter((a, i) => a === a1[i % a1.length]).length;
	var a2c = answers.filter((a, i) => a === a2[i % a2.length]).length;
	var a3c = answers.filter((a, i) => a === a3[i % a3.length]).length;
	var max = Math.max(a1c, a2c, a3c);

	if (a1c === max) {
		answer.push(1);
	}
	if (a2c === max) {
		answer.push(2);
	}
	if (a3c === max) {
		answer.push(3);
	}

	return answer;
}