@@ -10,15 +10,55 @@ const initialState = '#...#...##..####..##.####.#...#...#.#.#.#......##....#....
10
10
11
11
const init = ( data ) => {
12
12
const rules = data
13
- const plantTracker = new Plants ( initialState , rules )
13
+ let plantTracker = new Plants ( initialState , rules )
14
14
const generations = 20
15
15
for ( let gen = 1 ; gen <= generations ; gen ++ ) {
16
16
plantTracker . advance ( )
17
17
}
18
18
console . log ( `Generating ${ generations } generations from the input looks like this:` )
19
19
plantTracker . display ( )
20
20
const answer = plantTracker . getCheckSum ( generations )
21
- const answer2 = ''
21
+
22
+ // Start for part 2
23
+ const generations2 = 500
24
+ for ( let gen = generations + 1 ; gen <= generations2 ; gen ++ ) {
25
+ plantTracker . advance ( )
26
+ }
27
+ console . log ( `Generating ${ generations2 } generations from the input looks like this:` )
28
+ plantTracker . display ( )
29
+
30
+ // 500 generations takes about 30s, so running 50B generations isn't possible. It's
31
+ // clear looking at the log there's a "Game of Life"-style glider.
32
+ // See output/500-generations.txt and output/500-generations.png
33
+ // This probably is reflected in a pattern in the checksum.
34
+ let prevCheckSum = 0
35
+ let prevDelta = 0
36
+ const stableThreshold = 5 // The number of sequentially identical deltas necessary to determine stabilization
37
+ const stableDeltas = Array ( stableThreshold ) . fill ( 0 )
38
+ let stableGeneration = 0
39
+ let stableCheckSum = 0
40
+ for ( let gen = 0 ; gen <= generations2 ; gen ++ ) {
41
+ const checkSum = plantTracker . getCheckSum ( gen )
42
+ const delta = checkSum - prevCheckSum
43
+ console . log ( `Generation ${ gen } checksum: ${ plantTracker . getCheckSum ( gen ) } which is Δ of ${ delta } )` )
44
+
45
+ // When delta matches previous generation, we may have reached stabilization
46
+ if ( delta === prevDelta ) {
47
+ stableDeltas . shift ( )
48
+ stableDeltas . push ( delta )
49
+ // Reached true stable point when there are N deltas in a row that are the same.
50
+ if ( stableDeltas . filter ( ( Δ ) => Δ === delta ) . length === stableDeltas . length ) {
51
+ stableCheckSum = checkSum
52
+ stableGeneration = gen
53
+ break
54
+ }
55
+ }
56
+ prevCheckSum = checkSum
57
+ prevDelta = delta
58
+ }
59
+ console . log ( `At generation ${ stableGeneration } the Δ is ${ stableDeltas [ 0 ] } and the checksum is ${ stableCheckSum } .` )
60
+ // Calculate the checksum for 50B generations (minus the generation we're already at)
61
+ const answer2 = ( stableDeltas [ 0 ] * ( 50000000000 - stableGeneration - 1 ) ) + stableCheckSum
22
62
23
63
console . log ( `-- Part 1 --` )
24
64
console . log ( `Answer: ${ answer } ` )
0 commit comments