Skip to content

Commit ec97a6d

Browse files
committed
feat: update frequency calculation to handle input parsing and duplicates
1 parent 7b077cf commit ec97a6d

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
const fs = require("fs");
2-
const lines = fs.readFileSync("input.txt", "utf8").split("\n");
2+
const changes = fs.readFileSync("input.txt", "utf8").trim().split("\n").map(Number);
33

44
let frequency = 0;
55

6-
for (const line of lines) {
7-
if (line !== "") {
8-
frequency += Number(line);
9-
}
10-
}
11-
console.log(frequency);
6+
const seen = new Set([0]);
127

8+
for (i = 0; ;i++) {
9+
frequency += changes[i % changes.length];
10+
11+
if (seen.has(frequency)) {
12+
console.log(frequency);
13+
break;
14+
}
15+
seen.add(frequency)
16+
}

0 commit comments

Comments
 (0)