-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcubes3.rs
184 lines (162 loc) · 3.65 KB
/
cubes3.rs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::collections::HashMap;
use std::io;
use std::io::Read as _;
#[derive(Clone,Copy,Eq,PartialEq)]
enum Status {
Active,
Dead,
}
#[derive(Clone,Eq,PartialEq,Hash)]
struct Coords {
pub x: isize,
pub y: isize,
pub z: isize,
}
#[derive(Clone)]
struct BoundRange {
pub min: isize,
pub max: isize,
}
impl BoundRange {
fn enlarge(&self) -> Self {
return Self {
min: self.min - 1,
max: self.max + 1,
};
}
}
#[derive(Clone)]
struct Bounds {
pub x: BoundRange,
pub y: BoundRange,
pub z: BoundRange,
}
impl Bounds {
fn enlarge(&self) -> Self {
return Self {
x: self.x.enlarge(),
y: self.y.enlarge(),
z: self.z.enlarge(),
}
}
}
#[derive(Clone)]
struct Map {
pub cubes: HashMap<Coords, Status>,
pub bounds: Bounds,
}
impl Map {
fn get_status(&self, coords: &Coords) -> Status {
match self.cubes.get(coords) {
Some(cube) => *cube,
None => Status::Dead,
}
}
fn count_active(&self) -> usize {
self.cubes.iter().fold(0, |accumulator, (_coords, status)| {
accumulator + (if *status == Status::Active { 1 } else { 0 })
})
}
fn print(&self) {
for z in self.bounds.z.min..=self.bounds.z.max {
eprintln!("z: {}", z);
for y in self.bounds.y.min..=self.bounds.y.max {
for x in self.bounds.x.min..=self.bounds.x.max {
if self.get_status(&Coords { x, y, z }) == Status::Active {
eprint!("#");
} else {
eprint!(".");
}
}
eprintln!("");
}
}
}
}
fn parse_input() -> Map {
let mut text = String::from("");
io::stdin().lock().read_to_string(&mut text).expect("Failed to read from stdin");
let mut map = HashMap::<Coords, Status>::new();
let mut ymax = 0;
let mut xmax = 0;
for (y, line) in text.trim().split("\n").enumerate() {
for (x, chr) in line.chars().enumerate() {
let status: Status;
if chr == '#' {
status = Status::Active;
} else if chr == '.' {
status = Status::Dead;
} else {
panic!("Unexpected character \"{}\", expected \"#\" or \".\"\n", chr);
}
map.insert(Coords { x: x as isize, y: y as isize, z: 0 }, status);
xmax = x;
}
// Here we should check if each line has the same length, but whatever
ymax = y;
}
return Map {
cubes: map,
bounds: Bounds {
x: BoundRange { min: 0, max: xmax as isize },
y: BoundRange { min: 0, max: ymax as isize },
z: BoundRange { min: 0, max: 0 },
},
};
}
fn cycle(old: Map) -> Map {
let mut new = Map {
cubes: HashMap::new(),
bounds: old.bounds.enlarge(),
};
for z in new.bounds.z.min..=new.bounds.z.max {
for y in new.bounds.y.min..=new.bounds.y.max {
for x in new.bounds.x.min..=new.bounds.x.max {
let mut count_active = 0;
for delta_z in -1..=1 {
for delta_y in -1..=1 {
for delta_x in -1..=1 {
if (delta_x == 0) && (delta_y == 0) && (delta_z == 0) {
continue
}
let coords = Coords { x: x + delta_x, y: y + delta_y, z: z + delta_z };
if old.get_status(&coords) == Status::Active {
count_active += 1;
}
}
}
}
let coords = Coords { x, y, z };
let new_status = match old.get_status(&coords) {
Status::Active => {
if (count_active == 2) || (count_active == 3) {
Status::Active
} else {
Status::Dead
}
},
Status::Dead => {
if count_active == 3 {
Status::Active
} else {
Status::Dead
}
}
};
new.cubes.insert(coords, new_status);
}
}
}
return new;
}
fn main() {
let mut current = parse_input();
eprintln!("Cycle 0: {}", current.count_active());
// current.print();
for c in 1..=6 {
current = cycle(current);
// eprintln!("");
eprintln!("Cycle {}: {}", c, current.count_active());
// current.print();
}
}