Skip to content

Commit eafdcff

Browse files
committed
Solve day 01 challenge
1 parent 40b6b74 commit eafdcff

9 files changed

+7181
-2717
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
indent_size = 2
5+
insert_final_newline = true
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
9+
[*.js]
10+
max_line_length = 80
11+
indent_brace_style = 1TBS
12+
spaces_around_operators = true
13+
quote_type = single

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
.DS_Store

babel.config.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"env": {
3+
"test": {
4+
"plugins": ["@babel/plugin-transform-modules-commonjs"]
5+
}
6+
},
7+
"presets": [
8+
"@babel/preset-env"
9+
]
10+
}

day01/answer.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import fs from "fs"
2+
3+
const readInput = fs.readFileSync(__dirname + '/input.txt', 'utf-8');
4+
5+
const formattedInput = readInput.trim().split('\n');
6+
7+
8+
9+
export const countIncrement = (depths) => {
10+
let increment = 0
11+
12+
for (let i = 0; i < depths.length - 1; i++) {
13+
if (Number(depths[i]) < Number(depths[i + 1])) {
14+
increment += 1
15+
}
16+
}
17+
18+
return increment
19+
}
20+
21+
export const countGroupedIncrement = (depths) => {
22+
let groups = []
23+
24+
for (let i = 0; i < depths.length - 2; i++) {
25+
groups.push(depths.slice(i, i + 3))
26+
}
27+
28+
const reducer = (previousValue, currentValue) => Number(previousValue) + Number(currentValue);
29+
30+
const groupSum = groups.map(groupItem => groupItem.reduce(reducer, 0))
31+
32+
return countIncrement(groupSum)
33+
}
34+
35+
console.log("The number of increments is: ", countIncrement(formattedInput))
36+
console.log("The number of grouped increments is: ", countGroupedIncrement(formattedInput))

day01/challenge.txt

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
--- Day 1: Sonar Sweep ---
2+
3+
You're minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean!
4+
5+
Before you know it, you're inside a submarine the Elves keep ready for situations like this. It's covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there's a little meter that indicates the antenna's signal strength by displaying 0-50 stars.
6+
7+
Your instincts tell you that in order to save Christmas, you'll need to get all fifty stars by December 25th.
8+
9+
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
10+
11+
As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine.
12+
13+
For example, suppose you had the following report:
14+
15+
199
16+
200
17+
208
18+
210
19+
200
20+
207
21+
240
22+
269
23+
260
24+
263
25+
This report indicates that, scanning outward from the submarine, the sonar sweep found depths of 199, 200, 208, 210, and so on.
26+
27+
The first order of business is to figure out how quickly the depth increases, just so you know what you're dealing with - you never know if the keys will get carried into deeper water by an ocean current or a fish or something.
28+
29+
To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.) In the example above, the changes are as follows:
30+
31+
199 (N/A - no previous measurement)
32+
200 (increased)
33+
208 (increased)
34+
210 (increased)
35+
200 (decreased)
36+
207 (increased)
37+
240 (increased)
38+
269 (increased)
39+
260 (decreased)
40+
263 (increased)
41+
In this example, there are 7 measurements that are larger than the previous measurement.
42+
43+
How many measurements are larger than the previous measurement?
44+
45+
Answer:
46+
47+
The first half of this puzzle is complete! It provides one gold star: *
48+
49+
--- Part Two ---
50+
51+
Considering every single measurement isn't as useful as you expected: there's just too much noise in the data.
52+
53+
Instead, consider sums of a three-measurement sliding window. Again considering the above example:
54+
55+
199 A
56+
200 A B
57+
208 A B C
58+
210 B C D
59+
200 E C D
60+
207 E F D
61+
240 E F G
62+
269 F G H
63+
260 G H
64+
263 H
65+
Start by comparing the first and second three-measurement windows. The measurements in the first window are marked A (199, 200, 208); their sum is 199 + 200 + 208 = 607. The second window is marked B (200, 208, 210); its sum is 618. The sum of measurements in the second window is larger than the sum of the first, so this first comparison increased.
66+
67+
Your goal now is to count the number of times the sum of measurements in this sliding window increases from the previous sum. So, compare A with B, then compare B with C, then C with D, and so on. Stop when there aren't enough measurements left to create a new three-measurement sum.
68+
69+
In the above example, the sum of each three-measurement window is as follows:
70+
71+
A: 607 (N/A - no previous sum)
72+
B: 618 (increased)
73+
C: 618 (no change)
74+
D: 617 (decreased)
75+
E: 647 (increased)
76+
F: 716 (increased)
77+
G: 769 (increased)
78+
H: 792 (increased)
79+
In this example, there are 5 sums that are larger than the previous sum.
80+
81+
Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?
82+
83+
Answer:
84+
85+
Although it hasn't changed, you can still get your puzzle input.
86+
87+
You can also [Share] this puzzle.

0 commit comments

Comments
 (0)