Skip to content

Commit 449dd11

Browse files
committed
feat: Day 1: Chronal Calibration (part 2)
1 parent a3e1559 commit 449dd11

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

day-01-chronal-calibration/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,30 @@ Here are other example situations:
1919

2020
Starting with a frequency of zero, **what is the resulting frequency** after all of the changes in frequency have been applied?
2121

22+
## Part Two
23+
24+
You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches **twice**.
25+
26+
For example, using the same list of changes above, the device would loop as follows:
27+
28+
- Current frequency ` 0`, change of `+1`; resulting frequency ` 1`.
29+
- Current frequency ` 1`, change of `-2`; resulting frequency `-1`.
30+
- Current frequency `-1`, change of `+3`; resulting frequency ` 2`.
31+
- Current frequency ` 2`, change of `+1`; resulting frequency ` 3`.
32+
- (At this point, the device continues from the start of the list.)
33+
- Current frequency ` 3`, change of `+1`; resulting frequency ` 4`.
34+
- Current frequency ` 4`, change of `-2`; resulting frequency ` 2`, which has already been seen.
35+
36+
In this example, the first frequency reached twice is `2`. Note that your device might need to repeat its list of frequency changes many times before a duplicate frequency is found, and that duplicates might be found while in the middle of processing the list.
37+
38+
Here are other examples:
39+
40+
- `+1, -1` first reaches `0` twice.
41+
- `+3, +3, +4, -2, -4` first reaches `10` twice.
42+
- `-6, +3, +8, +5, -6` first reaches `5` twice.
43+
- `+7, +7, -2, -7, -4` first reaches `14` twice.
44+
45+
**What is the first frequency your device reaches twice?**
46+
2247
## References
2348
- https://adventofcode.com/2018/day/1
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const calibration = (input) => {
2+
const frequencies = new Set([0]);
3+
4+
const changes = input
5+
.split('\n')
6+
.map((x) => parseInt(x));
7+
8+
let frequency = 0;
9+
let i = 0;
10+
11+
// eslint-disable-next-line no-constant-condition
12+
while (true) {
13+
if (i === changes.length) {
14+
i = 0;
15+
16+
continue;
17+
}
18+
19+
frequency += changes[i];
20+
21+
if (frequencies.has(frequency)) {
22+
break;
23+
}
24+
25+
frequencies.add(frequency);
26+
27+
i++;
28+
}
29+
30+
return frequency;
31+
};
32+
33+
module.exports = calibration;

day-01-chronal-calibration/test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const assert = require('assert');
22

33
const chronalCalibration = require('./chronal-calibration');
4+
const chronalCalibration2 = require('./chronal-calibration2');
45

56
describe('Day 1: Chronal Calibration', () => {
67
it('should calculate frequency from +1, -2, +3, +1', () => {
@@ -39,4 +40,57 @@ describe('Day 1: Chronal Calibration', () => {
3940

4041
assert.strictEqual(chronalCalibration(changes), -6);
4142
});
43+
44+
describe('Part Two', () => {
45+
it('should calculate first duplicate frequency from +1, -2, +3, +1', () => {
46+
const changes =
47+
`+1
48+
-2
49+
+3
50+
+1`;
51+
52+
assert.strictEqual(chronalCalibration2(changes), 2);
53+
});
54+
55+
it('should calculate first duplicate frequency from +1, -1', () => {
56+
const changes =
57+
`+1
58+
-1`;
59+
60+
assert.strictEqual(chronalCalibration2(changes), 0);
61+
});
62+
63+
it('should calculate first duplicate frequency from +3, +3, +4, -2, -4', () => {
64+
const changes =
65+
`+3
66+
+3
67+
+4
68+
-2
69+
-4`;
70+
71+
assert.strictEqual(chronalCalibration2(changes), 10);
72+
});
73+
74+
it('should calculate first duplicate frequency from -6, +3, +8, +5, -6', () => {
75+
const changes =
76+
`-6
77+
+3
78+
+8
79+
+5
80+
-6`;
81+
82+
assert.strictEqual(chronalCalibration2(changes), 5);
83+
});
84+
85+
it('should calculate first duplicate frequency from +7, +7, -2, -7, -4', () => {
86+
const changes =
87+
`+7
88+
+7
89+
-2
90+
-7
91+
-4`;
92+
93+
assert.strictEqual(chronalCalibration2(changes), 14);
94+
});
95+
});
4296
});

0 commit comments

Comments
 (0)