-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-01.html
144 lines (127 loc) · 3.1 KB
/
day-01.html
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<html>
<head>
<title>Day 01</title>
</head>
<body>
<h1>Day 01</h1>
<script type="module">
import { DAY_01 } from "./constants/inputs.js";
const inputs = [...DAY_01];
// === PART ONE ===
// Method #1
const measurements = [];
inputs.forEach((input, index) => {
if (!inputs[index - 1]) {
return null;
}
if (input > inputs[index - 1]) {
measurements.push("increase");
} else {
measurements.push("decrease");
}
});
const countIncreases = measurements.filter((m) => m === "increase"); // 1752
const countDecreases = measurements.filter((m) => m === "decrease"); // 247
// Method #2
const increases = inputs.reduce((count, input, index) => {
if (input > inputs[index - 1]) {
count++;
return count;
}
return count;
}, 0);
console.log(`Count of increases: ${increases}`);
// === PART 2 ===
const smallInputs = [
100, 101, 105, 106, 103, 104, 106, 108, 112, 123, 125, 149, 158, 147,
150, 153, 156, 166, 171, 172, 174, 186, 193, 198, 203, 217, 220, 235,
239, 242, 245, 243, 237, 238, 246, 247, 238, 242, 254, 256, 263,
];
const letters = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
];
/*
100 A
101 A B
105 A B C
106 B C D
103 E C D
104 E F D
106 E F G
108 F G H
112 I G H
123 I J H
125 I J K
149 J K L
158 M K L
147 M N L
150 M N O
153 N O P
156 Q O P
166 Q R P
171 Q R S
172 R S T
174 U S T
186 U V T
193 U V W
198 V W X
203 Y W X
217 Y Z X
220 Y Z A
235 Z A B
239 C A B
242 C D
245 C D E
243 D E F
*/
// A -- 100 101 105 = 306 N
// B -- 101 105 106 = 312 D
// C -- 105 106 103 = 314 D
// D -- 106 103 104 = 313 I
// E -- 103 104 106 = 313 N
// F -- 104 106 108 = 318 D
// G -- 106 108 112 = 326 D
// H -- 108 112 123 = 343 D
// I -- 112 123 125 =
// J -- 123 125 149 =
// K -- 125 149 158 =
// L --
// M --
// N --
const p2Measurements = [];
inputs.forEach((input, index) => {
if (index < 3) return;
let sumCurrent = 0;
let sumPrev = 0;
for (let i = index; i > index - 3; i--) {
sumCurrent = sumCurrent + inputs[i];
}
for (let j = index - 1; j > index - 4; j--) {
sumPrev = sumPrev + inputs[j];
}
if (sumCurrent > sumPrev) {
p2Measurements.push("increase");
} else {
p2Measurements.push("decrease");
}
});
const p2CountIncreases = p2Measurements.filter((m) => m === "increase");
const p2CountDecreases = p2Measurements.filter((m) => m === "decrease");
console.log("p2CountIncreases", p2CountIncreases.length);
console.log("p2CountDecreases", p2CountDecreases.length);
</script>
</body>
</html>