-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.ts
106 lines (87 loc) · 3.06 KB
/
day06.ts
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 fs from 'fs';
const DAY = '06';
function calculateDistance(buttonHold: number, totalTime: number): number {
const travelTime = totalTime - buttonHold;
const speed = buttonHold;
return speed * travelTime;
}
async function part1(fileName: string) {
const input = fs.readFileSync(`input/${fileName}`);
const rows = input.toString().split('\r\n');
const times = rows[0].split(/\s+/).slice(1);
const distances = rows[1].split(/\s+/).slice(1);
const records: { time: number; distance: number }[] = [];
for (let i = 0; i < times.length; i++) {
records.push({ time: parseInt(times[i]), distance: parseInt(distances[i]) });
}
let winningWays = 1;
for (const record of records) {
let counter = 0;
const midPoint = Math.floor(record.time / 2);
const midPointDistance = calculateDistance(midPoint, record.time);
// If mid point's distance is better than the record, start finding more strategies from there
if (midPointDistance > record.distance) {
counter++;
let betterStrategy1 = true;
let betterStrategy2 = true;
let buttonHoldTime1 = midPoint;
let buttonHoldTime2 = midPoint;
while (betterStrategy1 || betterStrategy2) {
buttonHoldTime1++;
if (betterStrategy1 && calculateDistance(buttonHoldTime1, record.time) > record.distance) {
betterStrategy1 = true;
counter++;
} else {
betterStrategy1 = false;
}
buttonHoldTime2--;
if (betterStrategy2 && calculateDistance(buttonHoldTime2, record.time) > record.distance) {
betterStrategy2 = true;
counter++;
} else {
betterStrategy2 = false;
}
}
}
winningWays *= counter;
}
console.log(winningWays);
}
async function part2(fileName: string) {
const input = fs.readFileSync(`input/${fileName}`);
const rows = input.toString().split('\r\n');
const time = parseInt(rows[0].replace(/\s+/g, '').split(':')[1]);
const distance = parseInt(rows[1].replace(/\s+/g, '').split(':')[1]);
let winningWays = 0;
const midPoint = Math.floor(time / 2);
const midPointDistance = calculateDistance(midPoint, time);
// If mid point's distance is better than the record, start finding more strategies from there
if (midPointDistance > distance) {
winningWays++;
let betterStrategy1 = true;
let betterStrategy2 = true;
let buttonHoldTime1 = midPoint;
let buttonHoldTime2 = midPoint;
while (betterStrategy1 || betterStrategy2) {
buttonHoldTime1++;
if (betterStrategy1 && calculateDistance(buttonHoldTime1, time) > distance) {
betterStrategy1 = true;
winningWays++;
} else {
betterStrategy1 = false;
}
buttonHoldTime2--;
if (betterStrategy2 && calculateDistance(buttonHoldTime2, time) > distance) {
betterStrategy2 = true;
winningWays++;
} else {
betterStrategy2 = false;
}
}
}
console.log(winningWays);
}
// part1(`day${DAY}_example.txt`);
// part1(`day${DAY}.txt`);
// part2(`day${DAY}_example.txt`);
part2(`day${DAY}.txt`);