days 11-16

This commit is contained in:
Tim Stallard 2017-12-16 12:12:56 +00:00
parent 492bdb29fd
commit d5fbba3277
Signed by: Tim
GPG Key ID: E18E975C6A734F07
16 changed files with 2313 additions and 0 deletions

1
day11/input.txt Normal file

File diff suppressed because one or more lines are too long

30
day11/part1.js Normal file
View File

@ -0,0 +1,30 @@
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
var dirs = input.split(",");
var replacements = [
[["n", "s"], ""],
[["ne", "sw"], ""],
[["nw", "se"], ""],
[["ne", "nw"], "n"],
[["se", "sw"], "s"],
[["ne", "s"], "se"],
[["se", "n"], "ne"],
[["sw", "n"], "nw"],
[["nw", "s"], "sw"],
];
var oldlength = 0;
while(dirs.length != oldlength){
oldlength = dirs.length;
for(var replacement of replacements){
if(dirs.indexOf(replacement[0][0]) > -1 && dirs.indexOf(replacement[0][1]) > -1){
dirs.splice(dirs.indexOf(replacement[0][0]), 1);
dirs.splice(dirs.indexOf(replacement[0][1]), 1);
if(replacement[1]){
dirs.push(replacement[1]);
}
}
}
}
console.log(dirs.length);

41
day11/part2.js Normal file
View File

@ -0,0 +1,41 @@
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
var dirs = input.split(",");
function simplify(dirs){
var replacements = [
[["n", "s"], ""],
[["ne", "sw"], ""],
[["nw", "se"], ""],
[["ne", "nw"], "n"],
[["se", "sw"], "s"],
[["ne", "s"], "se"],
[["se", "n"], "ne"],
[["sw", "n"], "nw"],
[["nw", "s"], "sw"],
];
var oldlength = 0;
while(dirs.length != oldlength){
oldlength = dirs.length;
for(var replacement of replacements){
if(dirs.indexOf(replacement[0][0]) > -1 && dirs.indexOf(replacement[0][1]) > -1){
dirs.splice(dirs.indexOf(replacement[0][0]), 1);
dirs.splice(dirs.indexOf(replacement[0][1]), 1);
if(replacement[1]){
dirs.push(replacement[1]);
}
}
}
}
return dirs;
}
var maxlen = 0;
for(var i = 0; i < dirs.length; i++){
var sub = dirs.slice(0, i);
var len = simplify(sub).length;
if(len > maxlen){
maxlen = len;
}
console.log(i/dirs.length);
}
console.log(maxlen);

2000
day12/input.txt Normal file

File diff suppressed because it is too large Load Diff

11
day12/part1.js Normal file
View File

@ -0,0 +1,11 @@
var input = require("fs").readFileSync("input.txt").toString();
var links = input.split("\n").filter((a)=>(a)).map((a)=>(a.split(" <-> ")[1].split(", ").map((b)=>(parseInt(b)))));
var attached = [];
function add(x){
if(attached.indexOf(x) == -1){
attached.push(x);
links[x].forEach((a)=>(add(a)));
}
}
add(0);
console.log(attached.length);

19
day12/part2.js Normal file
View File

@ -0,0 +1,19 @@
var input = require("fs").readFileSync("input.txt").toString();
var links = input.split("\n").filter((a)=>(a)).map((a)=>(a.split(" <-> ")[1].split(", ").map((b)=>(parseInt(b)))));
var allvisited = [];
function getconnected(x, items){
if(items.indexOf(x) == -1){
items.push(x);
links[x].forEach((a)=>(getconnected(a, items)));
}
return items;
}
var i = 0;
while(allvisited.length != Object.keys(links).length){
var start = Object.keys(links).map((a)=>(parseInt(a))).filter((a)=>(allvisited.indexOf(a) == -1))[0];
console.log(i, start);
var newgroup = getconnected(start, []);
allvisited = allvisited.concat(newgroup);
i++
}
console.log(i);

43
day13/input.txt Normal file
View File

@ -0,0 +1,43 @@
0: 4
1: 2
2: 3
4: 5
6: 6
8: 4
10: 8
12: 6
14: 6
16: 8
18: 8
20: 6
22: 8
24: 9
26: 8
28: 8
30: 12
32: 12
34: 10
36: 12
38: 12
40: 10
42: 12
44: 12
46: 12
48: 12
50: 12
52: 14
54: 14
56: 12
58: 14
60: 14
62: 14
64: 17
66: 14
70: 14
72: 14
74: 14
76: 14
78: 18
82: 14
88: 18
90: 14

4
day13/part1.js Normal file
View File

@ -0,0 +1,4 @@
var input = require("fs").readFileSync("input.txt").toString();
var depths = input.split("\n").filter((a)=>(a)).map((a)=>(a.split(": ").map((b)=>(parseInt(b))))).reduce((dep, a)=>{dep[a[0]] = a[1]; return dep;}, []);
var severity = depths.map((a, i)=>(a*i)).filter((a, i)=>((i % (a/i*2 - 2)) == 0)).reduce((a, b)=>(a+b));
console.log(severity);

8
day13/part2.js Normal file
View File

@ -0,0 +1,8 @@
var input = require("fs").readFileSync("input.txt").toString();
var depths = input.split("\n").filter((a)=>(a)).map((a)=>(a.split(": ").map((b)=>(parseInt(b))))).reduce((dep, a)=>{dep[a[0]] = a[1]; return dep;}, []);
var caught = (del)=>(depths.filter((a, i)=>(((i+del) % (a*2 - 2)) == 0)).length > 0);
for(var i = 0; i < 10000000; i++){
if(caught(i) == false){
console.log(i);
}
}

32
day14/part1.js Normal file
View File

@ -0,0 +1,32 @@
function hash(input){
var list = [];
for(var i = 0; i < 256; i++){
list.push(i);
}
var pos = 0;
var lengths = input.split("").filter((a)=>(a)).map((a)=>(a.charCodeAt(0))).concat([17, 31, 73, 47, 23]);
var skip = 0;
for(var x = 0; x < 64; x++){
for(var length of lengths){
for(var i = 0; i < (length/2); i++){
var a = list[(pos+i)%list.length];
var b = list[(pos+length-1-i)%list.length];
list[(pos+i)%list.length] = b;
list[(pos+length-1-i)%list.length] = a;
}
pos += length + skip;
pos = pos % list.length;
skip += 1;
}
}
var hash = list.reduce((arr, a, i)=>{arr[Math.floor(i/16)] = arr[Math.floor(i/16)] ^ a; return arr;}, []);
return hash.map((a)=>(a.toString(2).split("").map((b)=>(parseInt(b))).reduce((a,b)=>(a+b)))).reduce((a, b)=>(a+b));
}
var total = 0;
for(var i = 0; i < 128; i++){
total += hash("xlqgujun-" + i);
}
console.log(total);

61
day14/part2.js Normal file
View File

@ -0,0 +1,61 @@
function hash(input){
var list = [];
for(var i = 0; i < 256; i++){
list.push(i);
}
var pos = 0;
var lengths = input.split("").filter((a)=>(a)).map((a)=>(a.charCodeAt(0))).concat([17, 31, 73, 47, 23]);
var skip = 0;
for(var x = 0; x < 64; x++){
for(var length of lengths){
for(var i = 0; i < (length/2); i++){
var a = list[(pos+i)%list.length];
var b = list[(pos+length-1-i)%list.length];
list[(pos+i)%list.length] = b;
list[(pos+length-1-i)%list.length] = a;
}
pos += length + skip;
pos = pos % list.length;
skip += 1;
}
}
var hash = list.reduce((arr, a, i)=>{arr[Math.floor(i/16)] = arr[Math.floor(i/16)] ^ a; return arr;}, []);
return hash.map((a)=>(("00000000" + a.toString(2)).slice(-8).split("").map((b)=>(parseInt(b))))).reduce((a, b)=>(a.concat(b)));
}
var grid = [];
for(var i = 0; i < 128; i++){
grid.push(hash("xlqgujun-" + i));
}
var seen = [];
function calc(x, y){
return (x + (128 * y));
}
function scanNeighbours(x, y){
if(grid[x]){
if(grid[x][y]){
if(seen.indexOf(calc(x,y)) == -1){
seen.push(calc(x,y));
scanNeighbours(x+1,y);
scanNeighbours(x-1,y);
scanNeighbours(x,y+1);
scanNeighbours(x,y-1);
}
}
}
}
var total = 0;
for(var x = 0; x < 128; x++){
for(var y = 0; y < 128; y++){
if(grid[x][y]){
if(seen.indexOf(calc(x,y)) == -1){
total++;
scanNeighbours(x,y);
}
}
}
}
console.log(total);

13
day15/part1.js Normal file
View File

@ -0,0 +1,13 @@
var a = 591;
var b = 393;
var total = 0;
for(var i = 0; i <= 40000000; i++){
a = (a*16807)%2147483647;
b = (b*48271)%2147483647;
if(a.toString(2).slice(-16) == b.toString(2).slice(-16)){
total++;
console.log(i);
console.log(a, b);
}
}
console.log(total);

19
day15/part2.js Normal file
View File

@ -0,0 +1,19 @@
var a = 591;
var b = 393;
var total = 0;
for(var i = 0; i <= 5000000; i++){
do{
a = (a*16807)%2147483647;
}
while(a%4 != 0)
do{
b = (b*48271)%2147483647;
}
while(b%8 != 0)
if(a.toString(2).slice(-16) == b.toString(2).slice(-16)){
total++;
console.log(i);
console.log(a, b);
}
}
console.log(total);

1
day16/input.txt Normal file

File diff suppressed because one or more lines are too long

11
day16/part1.js Normal file
View File

@ -0,0 +1,11 @@
var items = "abcdefghijklmnop".split("");
s = (i) => {items = items.splice(-i).concat(items);}
x = (a,b) => {[items[a], items[b]] = [items[b], items[a]]}
p = (a,b) => {x(items.indexOf(a), items.indexOf(b))}
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
var input = "," + input + ",";
var expr = input.replace(/,/g, ");").replace(/;([sxp])/g, ";$1(").replace(/([a-z])\/([a-z])/g, "'$1'/'$2'").replace(/\//g, ",");
expr = expr.substring(2);
eval(expr);
console.log(items.join(""));

19
day16/part2.js Normal file
View File

@ -0,0 +1,19 @@
var items = "abcdefghijklmnop".split("");
s = (i) => {items = items.splice(-i).concat(items);}
x = (a,b) => {[items[a], items[b]] = [items[b], items[a]]}
p = (a,b) => {x(items.indexOf(a), items.indexOf(b))}
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
var input = "," + input + ",";
var expr = input.replace(/,/g, ");").replace(/;([sxp])/g, ";$1(").replace(/([a-z])\/([a-z])/g, "'$1'/'$2'").replace(/\//g, ",");
expr = expr.substring(2);
var state = "";
var states = [];
while(states.indexOf(state) == -1){
states.push(state);
eval(expr);
state = items.join("");
}
states = states.filter((a)=>(a));
var num = 1000000000;
console.log(states[(num-1)%states.length]);