Day 6: Wait For It
Well it seems I am well and truly obsessed. Part two today was so much easier than yesterday's XD. Basically the same as part one with some spaces removed.
This seems to have turned into a blog! I will try and do as many days as possible.
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
const inputToUse = realInput;
// ! Get race times and distance records from input
const inputLines = inputToUse.split(/\n/);
// console.log(inputLines);
// ! Get race times
const inputTimeLine = inputLines[0];
const inputTimeLineArr = inputTimeLine.split(':');
// console.log('inputTimeLineArr',inputTimeLineArr);
const timesStr = inputTimeLineArr[1].trim();
// console.log('timesStr',timesStr);
const timesArr = timesStr.split(/\s+/);
console.log('timesArr',timesArr);
// ! Get distance records
const inputDistanceLine = inputLines[1];
const inputDistanceLineArr = inputDistanceLine.split(':');
// console.log('inputTimeLineArr',inputTimeLineArr);
const distancesStr = inputDistanceLineArr[1].trim();
// console.log('timesStr',timesStr);
const distancesArr = distancesStr.split(/\s+/);
console.log('distancesArr',distancesArr);
// ! Check each race
var powerOfWinningStratCounts = 0;
timesArr.forEach(function(raceTime, i){
const distanceToBeat = distancesArr[i];
console.log(`for race ${i}, time to beat is ${distanceToBeat}`);
// check possible strategies
var winningStratCount = 0;
for( var j=0; j<raceTime; j++) {
const raceTimeNo = parseFloat(raceTime);
const chargeTime = j;
// console.log(`we charge for ${chargeTime}`);
const remainingTime = raceTime - chargeTime;
// console.log(`remaining time is ${remainingTime}`);
const speed = chargeTime;
const distanceDuringRace = speed * remainingTime;
// console.log('distanceFromCharge',distanceFromCharge);
// console.log(`for race ${i}, strat ${j} is to wait ${chargeTime} and achieve ${distanceDuringRace} distance`);
if( distanceDuringRace > distanceToBeat ) {
winningStratCount = winningStratCount + 1;
}
}
console.log(`for race ${i}, winningStratCount is ${winningStratCount}`);
if( powerOfWinningStratCounts > 0 ) {
powerOfWinningStratCounts = powerOfWinningStratCounts * winningStratCount;
} else {
powerOfWinningStratCounts = winningStratCount;
}
});
console.log(`powerOfWinningStratCounts:`,powerOfWinningStratCounts);
// part 2
const realRaceTime = timesStr.replace(/\s+/g,'');
console.log('realRaceTime',realRaceTime);
const realRaceDistance = distancesStr.replace(/\s+/g,'');
console.log('realRaceDistance',realRaceDistance);
var finalWinningStratCount = 0;
for( var j=0; j<realRaceTime; j++) {
const chargeTime = j;
// console.log(`we charge for ${chargeTime}`);
const remainingTime = realRaceTime - chargeTime;
// console.log(`remaining time is ${remainingTime}`);
const speed = chargeTime;
const distanceDuringRace = speed * remainingTime;
// console.log('distanceFromCharge',distanceFromCharge);
// console.log(`for race ${i}, strat ${j} is to wait ${chargeTime} and achieve ${distanceDuringRace} distance`);
if( distanceDuringRace > realRaceDistance ) {
finalWinningStratCount = finalWinningStratCount + 1;
}
}
console.log('finalWinningStratCount',finalWinningStratCount);