-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.html
87 lines (81 loc) · 1.93 KB
/
day6.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
<html>
<body>
YO
<script>
const input = [
4,
1,
15,
12,
0,
9,
9,
5,
5,
8,
7,
3,
14,
5,
12,
3
]
// const input = [0,
// 2,
// 7,
// 0,60
// ]
// const input = [2,
// 4,
// 1,
// 2,16]
// const input = [3,
// 1,
// 2,
// 3]
// const input = [0,
// 2,
// 3,
// 4]
// const input = [1,
// 3,
// 4,
// 1]
redistribute = (input, largest, indexOfLargest) => {
input[indexOfLargest] = 0
while (largest > 0) {
indexOfLargest + 1 == input.length ? indexOfLargest = 0 : indexOfLargest += 1
input[indexOfLargest] += 1
largest -= 1
}
return input
}
oneStep = (input) => {
largest = Math.max(...input);
indexOfLargest = input.indexOf(largest)
return input = redistribute(input, largest, indexOfLargest)
}
valueInArrayTwice = a => {
//based on last value being the newest one
for (var i=0; i<a.length - 1;i++) {
if (a[i].toString() == a[a.length - 1].toString() ){
console.log('part2: ')
console.log(`solution = ${a.length - 1 - i}`)
return true
}
}
return false
}
flow = (input) => {
let results = [input]
let value = input
while ( !valueInArrayTwice(results) ){
value = oneStep([...value])
results.push(value)
}
return results.length - 1
}
console.log(flow(input))
</script>
</body>
</html>