-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgarden.js
165 lines (142 loc) · 5.99 KB
/
garden.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class Plant {
constructor(id, is_alive = false) {
this.id = id;
this.alive = is_alive;
}
}
class Garden {
constructor(initial_state, spread_rules) {
this.garden = {};
this.spread_rules = {};
this.parseStateAndSetGarden(initial_state);
this.parseSpreadRulesAndSet(spread_rules);
}
parseStateAndSetGarden(state, startingIndex = 0) {
let state_array = state.split('');
state_array.forEach((plant, i) => {
let plant_id = i + startingIndex;
let is_plant_alive = plant === '#';
/**
* Allow the plant to store a reference to the parent
* garden so it can determine the state of its neighbors
*/
this.garden[plant_id] = new Plant(plant_id, is_plant_alive);
});
}
parseSpreadRulesAndSet(spread_rules) {
const spread_rule_regex = /([\.#]{5}) => ([\.#])/;
spread_rules.forEach(rule => {
let [match, state, result] = spread_rule_regex.exec(rule);
this.spread_rules[state] = result === '#';
});
}
nextPlantFrom(id, n = 1) {
return this.garden[id + n];
}
// @note This isn't DRY, but who cares?
prevPlantFrom(id, n = 1) {
return this.garden[id - n];
}
getPlantsNextState(plant_or_plant_id) {
let plant = plant_or_plant_id;
if (typeof plant_or_plant_id === 'number') {
plant = this.garden[plant_or_plant_id];
}
let plant_state = this.buildStateStringFromPlant(plant);
return this.spread_rules[plant_state];
}
getPlantsNeighbors(plant) {
return [
this.prevPlantFrom(plant.id, 2),
this.prevPlantFrom(plant.id, 1),
plant,
this.nextPlantFrom(plant.id, 1),
this.nextPlantFrom(plant.id, 2),
];
}
buildStateStringFromPlant(plant_or_plant_id) {
let plant = plant_or_plant_id;
if (typeof plant_or_plant_id === 'number') {
plant = this.garden[plant_or_plant_id];
}
return this.getPlantsNeighbors(plant).map(p => (p ? (p.alive ? '#' : '.') : '.')).join('');
}
tick(days = 1) {
for (let i = 0; i < days; i++) {
let plants = Object.values(this.garden);
plants.sort((a, b) => {
if (a.id < b.id) return -1;
else if (a.id > b.id) return 1;
else return 0;
});
let smallest_plant_id = plants[0].id;
let largest_plant_id = plants[plants.length - 1].id;
if (plants[0].alive) {
// If the first plant is alive, then we need three more "dead" plants at the beginning
let first_plant = new Plant(smallest_plant_id - 3);
let second_plant = new Plant(smallest_plant_id - 2);
let third_plant = new Plant(smallest_plant_id - 1);
plants.unshift(first_plant, second_plant, third_plant);
} else if (plants[1].alive) {
// If the first plant is dead but 2nd is alive, then we need two more "dead" plants
let first_plant = new Plant(smallest_plant_id - 2);
let second_plant = new Plant(smallest_plant_id - 1);
plants.unshift(first_plant, second_plant);
} else if (plants[2].alive) {
// Finally, if first two plants are dead but the third is alive, add one plant
let first_plant = new Plant(smallest_plant_id - 1);
plants.unshift(first_plant);
}
// Similar logic for end of the plants
if (plants[plants.length - 1].alive) {
let end_plant = new Plant(largest_plant_id + 3);
let second_to_last_plant = new Plant(largest_plant_id + 2);
let third_to_last_plant = new Plant(largest_plant_id + 1);
plants.push(third_to_last_plant, second_to_last_plant, end_plant);
} else if (plants[plants.length - 2].alive) {
let end_plant = new Plant(largest_plant_id + 2);
let second_to_last_plant = new Plant(largest_plant_id + 1);
plants.push(second_to_last_plant, end_plant);
} else if (plants[plants.length - 3].alive) {
let end_plant = new Plant(largest_plant_id + 1);
plants.push(end_plant);
}
// Don't update `this.garden` because that'll affect the state and how a plant grows
let next_garden_state = {};
// Now that any "padding" plants are in place, compute each one's next state (but don't update garden just yet!)
plants.forEach(plant => {
if (!this.garden[plant.id]) {
this.garden[plant.id] = plant;
}
let plant_state_string = this.buildStateStringFromPlant(plant);
next_garden_state[plant.id] = this.spread_rules[plant_state_string];
});
// Now that we've computed each plants next state, actually update its internal state from our cached `next_garden_state`
plants.forEach(plant => {
plant.alive = next_garden_state[plant.id];
});
}
}
// Part One question (run after 20 ticks...)
getSumOfAlivePlantsIds() {
let plants = Object.values(this.garden);
return plants.reduce((a, b) => {
return a + (b.alive ? b.id : 0);
}, 0);
}
getGardenAsString() {
let plants = Object.values(this.garden);
plants.sort((a, b) => {
if (a.id < b.id) return -1;
else if (a.id > b.id) return 1;
else return 0;
});
// return plants.map(p => p.alive ? '#' : '.').join('');
return {
string: plants.map(p => p.id === 0 ? (p.alive ? '+' : '-') : (p.alive ? '#' : '.')).join(''),
min: plants[0].id,
max: plants[plants.length - 1].id,
};
}
}
module.exports = Garden;