Added day 9
This commit is contained in:
parent
6e3de19949
commit
9bfed12c61
1
day09/input.txt
Normal file
1
day09/input.txt
Normal file
File diff suppressed because one or more lines are too long
25
day09/part1.js
Normal file
25
day09/part1.js
Normal file
@ -0,0 +1,25 @@
|
||||
var input = require("fs").readFileSync("input.txt").toString().replace(/\n/g, "").replace(/\r/g, "");
|
||||
var parts = [];
|
||||
while(input.length > 0){
|
||||
if(input[0] == "("){
|
||||
var len = input.match(/(\(([0-9]+)x([0-9]+)\)).*/);
|
||||
parts.push(input.slice(0, len[1].length + parseInt(len[2])));
|
||||
input = input.slice(len[1].length + parseInt(len[2]));
|
||||
}
|
||||
else{
|
||||
parts.push(input[0]);
|
||||
input = input.slice(1);
|
||||
}
|
||||
}
|
||||
|
||||
parts = parts.map(function(part){
|
||||
if(part[0] == "("){
|
||||
var len = part.match(/(\(([0-9]+)x([0-9]+)\))(.*)/);
|
||||
return parseInt(len[2]) * parseInt(len[3]);
|
||||
}
|
||||
else{
|
||||
return part.length
|
||||
}
|
||||
});
|
||||
|
||||
console.log(parts.reduce((a, b)=>(a + b)));
|
28
day09/part2.js
Normal file
28
day09/part2.js
Normal file
@ -0,0 +1,28 @@
|
||||
var input = require("fs").readFileSync("input.txt").toString().replace(/\n/g, "").replace(/\r/g, "");
|
||||
function lenStr(str){
|
||||
var parts = [];
|
||||
while(str.length > 0){
|
||||
if(str[0] == "("){
|
||||
var len = str.match(/(\(([0-9]+)x([0-9]+)\)).*/);
|
||||
parts.push(str.slice(0, len[1].length + parseInt(len[2])));
|
||||
str = str.slice(len[1].length + parseInt(len[2]));
|
||||
}
|
||||
else{
|
||||
parts.push(str[0]);
|
||||
str = str.slice(1);
|
||||
}
|
||||
}
|
||||
|
||||
parts = parts.map(function(part){
|
||||
if(part[0] == "("){
|
||||
var len = part.match(/(\(([0-9]+)x([0-9]+)\))(.*)/);
|
||||
return parseInt(len[3]) * lenStr(len[4]);
|
||||
}
|
||||
else{
|
||||
return part.length;
|
||||
}
|
||||
});
|
||||
return parts.reduce((a, b)=>(a + b));
|
||||
}
|
||||
|
||||
console.log(lenStr(input));
|
Loading…
Reference in New Issue
Block a user