Day 14: Parabolic Reflector Dish
Part one done. What a joy to have such an easy puzzle after those other challenging ones!
Okay now I've looked at part two… and it says to tilt the dish in four directions for 1,000,000,000 cycles. After the previous challenges I'm wondering how this problem could be condensed rather than brute forcing it, but this seems difficult. Maybe the answer is to again find a point that each rock reaches a loop, find the lowest common multiple so that all rocks complete their loop, and divide the cycle total by that number to find at what part of the loop the rocks would be in at that cycle point. Well, I think that must be it! Or close to it. It also seems difficult to program… I think I will leave it for now.
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
const inputToUse = realInput;
const dish = [];
const lines = inputToUse.split(/\n/);
lines.forEach(function(line){
const chars = line.trim().split('');
dish.push(chars);
});
console.log('dish',dish);
dish.forEach(function(row,i){
row.forEach(function(node,j){
// if the node contains a rolling stone
if( node === 'O' ) {
var shifting = true;
var stonePos = [i,j];
while( shifting ) {
console.log('stonePos',stonePos);
const nodeAbovePosY = stonePos[0] - 1;
if( nodeAbovePosY >= 0 ) {
const valueAtNodeAbove = dish[nodeAbovePosY][stonePos[1]];
console.log('valueAtNodeAbove',valueAtNodeAbove);
if( valueAtNodeAbove === '.' ) {
dish[stonePos[0]][stonePos[1]] = '.'; // old pos is now empty
dish[nodeAbovePosY][stonePos[1]] = 'O'; // rolling stone moves to pos above
stonePos = [nodeAbovePosY, stonePos[1]];
} else {
shifting = false;
}
} else {
shifting = false;
}
}
}
});
});
console.log('dish after shifting',dish);
// calculate weights
const dishHeight = dish.length;
var totalWeight = 0;
dish.forEach(function(row,i){
var stoneCount = 0;
row.forEach(function(char){
if( char === 'O' ) {
stoneCount = stoneCount + 1;
}
});
var singleStoneWeight = dishHeight - i;
const stonesWeight = singleStoneWeight * stoneCount;
totalWeight = totalWeight + stonesWeight;
});
console.log('totalWeight',totalWeight);