-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.js
106 lines (93 loc) · 2.69 KB
/
day12.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { read, readEx, time } from './util.js';
import Vec from './util/Vec.js';
await import('./util/bad-but-great.js');
await readEx();
await read();
let data;
const smthsub = (s) => {
let numqs = BigInt(s.split``.filter(c => c === '?').length);
const out = [];
console.log('num', 2n ** numqs);
let numIndex = (i, n) => (i >> n) & 1n;
for (let i = 0n; i < 2n ** numqs; ++i) {
let k = 0n;
let str = s.split``;
for (let j = BigInt(s.length); j-- > 0;) {
str[j] = str[j] === '?' ? numIndex(i, k++) ? '#' : '.' : str[j];
}
out.push(str.join``);
}
return out;
}
// 22:36
const partOne = () => {
let lines = data.lines();
const counts = (s) => s.split(/\.+/g).map(s => s.length).truthy();
const arreq = (a, b) => a.length === b.length && a.every((v, i) => b[i] === v);
let sum = 0;
for (const line of lines) {
let [a, b] = line.split(' ');
b = b.split(',').nums();
//console.log('a =', a);
//console.log('b =', b);
let poss = smthsub(a);
let c = 0;
for (const p of poss) {
if (arreq(counts(p), b)) {
//console.log(' p =', p);
++c;
}
}
//console.log('c =', c);
//console.log();
sum += c;
}
console.log({sum});
};
const count = (memo = {}, str, amounts, i, j, cur) => {
const key = [i, j, cur].join();
if (key in memo) return memo[key];
if (i === str.length) {
return +(j === amounts.length && cur === 0 || j === amounts.length - 1 && amounts[j] === cur);
}
let total = 0;
let c = str[i];
if (c !== '#') {
if (cur === 0) {
total += count(memo, str, amounts, i + 1, j, 0);
} else if (cur > 0 && j < amounts.length && amounts[j] === cur) {
total += count(memo, str, amounts, i + 1, j + 1, 0);
}
}
if (c !== '.') {
total += count(memo, str, amounts, i + 1, j, cur + 1);
}
return memo[key] = total;
};
// >2h
const partTwo = () => {
let lines = data.lines();
let sum = 0;
for (const line of lines) {
let [a, b] = line.split(' ');
a = [a].repeat(5).join('?');
b = b.split(',').repeat(5).nums();
let c = count({}, a, b, 0, 0, 0);
console.log(a);
console.log('=>', c);
console.log();
sum += c;
}
console.log({sum});
};
if (process.argv[2]) {
console.log('--- --- Running Sample Data --- ---');
data = await readEx(); // Sample Data
//time(partOne);
time(partTwo);
} else {
console.log('--- --- Running Real Data --- ---');
data = await read(); // Real Data
//time(partOne);
time(partTwo);
}