Skip to content

Commit 35c772e

Browse files
committed
Added day15 part 2
1 parent 3aa4342 commit 35c772e

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

day15/description.txt

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,51 @@ Here, you can see that the lowest (here, rightmost) 16 bits of the third value m
3636

3737
To get a significant sample, the judge would like to consider 40 million pairs. (In the example above, the judge would eventually find a total of 588 pairs that match in their lowest 16 bits.)
3838

39-
After 40 million pairs, what is the judge's final count?
39+
After 40 million pairs, what is the judge's final count?
40+
41+
--- Part Two ---
42+
43+
In the interest of trying to align a little better, the generators get more picky about the numbers they actually give to the judge.
44+
45+
They still generate values in the same way, but now they only hand a value to the judge when it meets their criteria:
46+
47+
Generator A looks for values that are multiples of 4.
48+
Generator B looks for values that are multiples of 8.
49+
Each generator functions completely independently: they both go through values entirely on their own, only occasionally handing an acceptable value to the judge, and otherwise working through the same sequence of values as before until they find one.
50+
51+
The judge still waits for each generator to provide it with a value before comparing them (using the same comparison method as before). It keeps track of the order it receives values; the first values from each generator are compared, then the second values from each generator, then the third values, and so on.
52+
53+
Using the example starting values given above, the generators now produce the following first five values each:
54+
55+
--Gen. A-- --Gen. B--
56+
1352636452 1233683848
57+
1992081072 862516352
58+
530830436 1159784568
59+
1980017072 1616057672
60+
740335192 412269392
61+
These values have the following corresponding binary values:
62+
63+
01010000100111111001100000100100
64+
01001001100010001000010110001000
65+
66+
01110110101111001011111010110000
67+
00110011011010001111010010000000
68+
69+
00011111101000111101010001100100
70+
01000101001000001110100001111000
71+
72+
01110110000001001010100110110000
73+
01100000010100110001010101001000
74+
75+
00101100001000001001111001011000
76+
00011000100100101011101101010000
77+
Unfortunately, even though this change makes more bits similar on average, none of these values' lowest 16 bits match. Now, it's not until the 1056th pair that the judge finds the first match:
78+
79+
--Gen. A-- --Gen. B--
80+
1023762912 896885216
81+
82+
00111101000001010110000111100000
83+
00110101011101010110000111100000
84+
This change makes the generators much slower, and the judge is getting impatient; it is now only willing to consider 5 million pairs. (Using the values from the example above, after five million pairs, the judge would eventually find a total of 309 pairs that match in their lowest 16 bits.)
85+
86+
After 5 million pairs, but using this new generator logic, what is the judge's final count?

day15/index.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ getInput()
55
.then((data) => {
66

77
const factorA = 16807;
8+
const multipleA = 4;
89
const factorB = 48271;
10+
const multipleB = 8;
911
const divisor = 2147483647;
10-
let length = 40000000;
11-
const pairs = {};
12-
let [[, nextValueA], [, nextValueB]] = data.map(row => row.split('with '));
12+
const [[, startValueA], [, startValueB]] = data.map(row => row.split('with '));
1313

1414
const toBinary = value => value.toString(2).padStart(32, '0').slice(16, 32);
1515

@@ -19,6 +19,11 @@ getInput()
1919
];
2020

2121
const part1 = () => {
22+
const pairs = {};
23+
let length = 40000000;
24+
let nextValueA = startValueA;
25+
let nextValueB = startValueB;
26+
2227
while (length) {
2328
[nextValueA, nextValueB] = generate(nextValueA, nextValueB);
2429

@@ -35,8 +40,32 @@ getInput()
3540
};
3641

3742
const part2 = () => {
43+
const pairs = {};
44+
const comparablesA = [];
45+
const comparablesB = [];
46+
let nextValueA = startValueA;
47+
let nextValueB = startValueB;
3848

49+
while (comparablesB.length <= 5000000) {
50+
[nextValueA, nextValueB] = generate(nextValueA, nextValueB);
51+
if (nextValueA % multipleA === 0) {
52+
comparablesA.push(nextValueA);
53+
}
54+
if (nextValueB % multipleB === 0) {
55+
comparablesB.push(nextValueB);
56+
}
57+
}
3958

59+
comparablesB.forEach((valueB, index) => {
60+
const binA = toBinary(comparablesA[index]);
61+
const binB = toBinary(valueB);
62+
if (binA === binB) {
63+
pairs[binA] = pairs[binA] ? pairs[binA] += 1 : 1;
64+
}
65+
});
66+
67+
const numberOfPairs = Object.values(pairs).reduce((sum, value) => sum + value, 0);
68+
console.log(numberOfPairs);
4069
};
4170

4271
part1();

0 commit comments

Comments
 (0)