function day1part1(data) { | |
let sum = 0; | |
for (let i = 0; i < data.length; i++) { | |
sum += data[i]; | |
} | |
return sum; | |
} | |
function day1part2(data) { | |
const frequencies = []; | |
let sum = 0; | |
while (true) { | |
for (let i = 0; i < data.length; i++) { | |
const num = data[i]; | |
sum += num; | |
if (frequencies.includes(sum)) { | |
return sum; | |
} | |
frequencies.push(sum); | |
} | |
} | |
} |
DAY TWO
//Part 1 | |
var count = 0; | |
var count2 = 0; | |
function getFrequency(string) { | |
var freq = {}; | |
for (var i = 0; i < string.length; i++) { | |
var character = string.charAt(i); | |
if (freq[character]) { | |
freq[character]++; | |
} else { | |
freq[character] = 1; | |
} | |
} | |
if (Object.keys(freq).some(i => freq[i] === 2)) count++ | |
if (Object.keys(freq).some(j => freq[j] === 3)) count2++ | |
} | |
for (var i = 0; i < input.length; i++) { | |
getFrequency(input[i]); | |
} | |
console.log(count) | |
console.log(count2) | |
console.log(count * count2) | |
//Part 2 | |
var freq = {}; | |
for (let i = 0; i < input.length; i++) { | |
for (let j = 0; j < input.length; j++) { | |
var subChar = input[j].slice(i + 1); | |
console.log(subChar) | |
if (freq.includes(subChar)) { | |
return subChar; | |
} | |
freq.push(subChar); | |
} | |
} |
//part 1 | |
var c = {}; | |
var o = []; | |
for (var inp of input) { | |
var string = inp.split(/(\d+) @ (\d+),(\d+)\: (\d+)x(\d+)/); | |
var i = parseInt(string[1]) | |
var l = parseInt(string[2]) | |
var t = parseInt(string[3]) | |
var w = parseInt(string[4]) | |
var h = parseInt(string[5]) | |
for (var j = t; j < t + h; j++) { | |
for (var k = l; k < l + w; k++) { | |
var key = k + "," + j ; | |
if (!c[key]) { | |
c[key] = []; | |
} | |
c[key] = c[key].concat(i); | |
if (c[key].length >= 2) { | |
o.push(key) | |
} | |
} | |
} | |
} | |
console.log(o) | |
//part 2 | |
var c = {}; | |
var ov = []; | |
var o = []; | |
var id = []; | |
for (var inp of input) { | |
var string = inp.split(/(\d+) @ (\d+),(\d+)\: (\d+)x(\d+)/); | |
var i = parseInt(string[1]) | |
var l = parseInt(string[2]) | |
var t = parseInt(string[3]) | |
var w = parseInt(string[4]) | |
var h = parseInt(string[5]) | |
id.push(i) | |
for (var y = t; y < t + h; y++) { | |
for (var x = l; x < l + w; x++) { | |
var key = x + "," + y; | |
if (!c[key]) { | |
c[key] = []; | |
} | |
c[key] = c[key].concat(i); | |
if (c[key].length >= 2) { | |
for (var i of id) { | |
ov.push(i); | |
} | |
} | |
} | |
} | |
} | |
for (const i of id) { | |
if (!ov.includes(i)) { | |
console.log(i); | |
} | |
} |
Σχόλια
Δημοσίευση σχολίου