D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
trianah
Full window
Github gist
Advent of Code 2018: day 2 (part 1)
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> d3.tsv('input.tsv', function(error, input) { if (error) throw error; console.log(calcChecksum(input)) function calcChecksum(data){ var sum1 = 0; var sum2 = 0; data.forEach(string => { sum1 = sum1 + calcRep(string.input, 2) sum2 = sum2 + calcRep(string.input, 3) }) return sum1 * sum2; } // returns 1 if string contains a //character that repeats exactly rep times function calcRep(string, rep){ var stringArr = string.split('') var pastCharsArr = [] console.log("String: " + string) for(var i = 0; i < stringArr.length - 1; i++){ var counter = 1; for(var j = i + 1; j < stringArr.length; j++){ if(!pastCharsArr.find(item => item === stringArr[i]) && stringArr[i] === stringArr[j]){ console.log(stringArr[j]) counter++; console.log(counter) } } if (counter === rep) { console.log("Found rep: " + rep) return 1 } pastCharsArr.push(stringArr[i]) } return 0; } }); </script> </body>
https://d3js.org/d3.v4.min.js