-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.js
67 lines (56 loc) · 1.59 KB
/
day06.js
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
var fs = require('fs')
function load(file) {
var input = fs.readFileSync(file, 'utf8')
let fishes = []
input.split(",").forEach(item => {
fishes.push(+item)
})
return fishes
}
function runDay06(file, maxDays) {
let fishes = load(file)
for (let dateInDay = 1; dateInDay <= maxDays; dateInDay++) {
let newFish = 0
for (let i = 0; i < fishes.length; i++) {
if (fishes[i] == 0) {
newFish ++
fishes[i] = 6
} else {
fishes[i]--
}
}
for (let i = 0; i < newFish; i++) {
fishes.push(8)
}
}
console.log("Step1", file, maxDays, "->", fishes.length)
}
function runDay06s2(file, maxDays) {
let fishes = Array(9).fill(0)
load(file).forEach(loadedFish => {
fishes[loadedFish]++
})
for (let dateInDay = 1; dateInDay <= maxDays; dateInDay++) {
let newFishes = Array(9).fill(0)
for (let i = 0; i < fishes.length; i++) {
if (i == 0) {
newFishes[6] += fishes[0]
newFishes[8] += fishes[0]
} else {
newFishes[i-1] += fishes[i]
}
}
fishes = newFishes
}
let numberOfFish = 0
for (let i = 0; i < fishes.length; i++) {
numberOfFish += fishes[i]
}
console.log("step2", file, maxDays, "->", numberOfFish)
}
runDay06('input-test.txt', 80)
runDay06('input.txt', 80)
runDay06s2('input-test.txt', 80)
runDay06s2('input.txt', 80)
runDay06s2('input-test.txt', 256)
runDay06s2('input.txt', 256)