Hi there!
I've used Javascript.
Here's my part one solution:
constfs=require('fs');// Read the inputletcontents=fs.readFileSync('frequenciesDiff.txt','utf8');// Parse to a int arrayletdiffs=contents.split("\n").map(item=>parseInt(item));// Reduceletresult=diffs.reduce((a,b)=>a+b);// Profit!console.log(result);
I just read the input from file, parsed to a int array and used the reduce function.
Talking about the second part, I've user a dictionary to store frequencies and retrieve them quickly (access is O(1)), cycling in a circular manner through the provided diffs:
constfs=require('fs');functionduplicateFinder(diffs){// Init an empty memo. We'll store here frequency values for easy retrievalletmemo={};letcurrentFrequency=0;leti=0;// Loop until currentFrequency is already in the memo.// When the loop breaks, it will be because currentFrequency got a value that had before. // This means it's the second time it appearswhile(!(currentFrequencyinmemo)){memo[currentFrequency]=currentFrequency.toString();currentFrequency+=diffs[i];i=++i%diffs.length}returncurrentFrequency;}// Read the inputletcontents=fs.readFileSync('frequenciesDiff.txt','utf8');// Parse to a int arrayletdiffs=contents.split("\n").map(item=>parseInt(item));// Do the workletresult=duplicateFinder(diffs);// Profit!console.log(result);
I'm planning to do this in Go too, since I'd love to practice this language, but I haven't much time for now :(
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi there!
I've used Javascript.
Here's my part one solution:
I just read the input from file, parsed to a int array and used the
reduce
function.Talking about the second part, I've user a dictionary to store frequencies and retrieve them quickly (access is O(1)), cycling in a circular manner through the provided diffs:
I'm planning to do this in Go too, since I'd love to practice this language, but I haven't much time for now :(