-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettlers.rs
157 lines (127 loc) · 3.58 KB
/
settlers.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
use std::convert::TryInto;
use std::io;
use std::io::Write;
use std::process;
use std::vec;
struct Area {
rows: usize,
columns: usize,
data: Vec<u8>
}
fn string_to_ints(string: &str) -> Vec<usize> {
let mut result: Vec<usize> = Vec::new();
for fragment in string.trim().split_ascii_whitespace() {
let value: usize = fragment.parse().expect("Failed to parse integer value");
result.push(value);
}
return result;
}
fn read_stdin() -> Area {
let mut line = String::new();
let ok = io::stdin().read_line(&mut line);
if !ok.is_ok() {
eprintln!("Failed to read Row #0 of input: {}", ok.unwrap_err());
process::exit(1);
}
let size = string_to_ints(&line);
if size.len() < 2 {
eprintln!("Row #0 of input should contain number of rows and columns");
process::exit(1);
}
let rows = size[0];
let columns = size[1];
let mut data: Vec<u8> = Vec::with_capacity(rows * columns);
for y in 0..rows {
line.clear();
let ok = io::stdin().read_line(&mut line);
if !ok.is_ok() {
eprintln!("Failed to read Row #{} of input: {}", (y+1), ok.unwrap_err());
process::exit(1);
}
let bytes = line.trim().as_bytes();
if bytes.len() != columns {
eprintln!("Expected {} bytes in input row, but got {}", columns, bytes.len());
process::exit(1);
}
data.append(&mut bytes.to_vec());
}
if data.len() != rows*columns {
eprintln!("Expected {} bytes in input total, but got {}", rows*columns, data.len());
process::exit(1);
}
return Area{ rows: rows, columns: columns, data: data };
}
fn get_tile(data: &Area, x: usize, y: usize) -> u8 {
if (x >= data.columns) || (y >= data.rows) {
return 'x' as u8;
}
return data.data[(y * data.columns) + x];
}
fn get_tile_i(data: &Area, x: isize, y: isize) -> u8 {
if (x < 0) || (y < 0) {
return 'x' as u8;
}
return get_tile(data, x.try_into().unwrap(), y.try_into().unwrap());
}
fn step(data: &Area) -> Area {
let mut result: Vec<u8> = Vec::with_capacity(data.rows * data.columns);
for y in 0..data.rows {
for x in 0..data.columns {
let (mut trees, mut lumber) = (0, 0);
for yd in 0..3 {
for xd in 0..3 {
let nx = (x as isize) + (xd as isize) - 1;
let ny = (y as isize) + (yd as isize) - 1;
if (ny == y as isize) && (nx == x as isize) {
continue;
}
match get_tile_i(&data, nx, ny) as char {
'|' => trees += 1,
'#' => lumber += 1,
_ => ()
}
}
}
let mut tile = get_tile(&data, x, y);
match tile as char {
'.' => { if trees >= 3 { tile = '|' as u8; }}
'|' => { if lumber >= 3 { tile = '#' as u8; }}
'#' => { if (lumber == 0) || (trees == 0) { tile = '.' as u8; }}
_ => ()
}
result.push(tile);
}
}
return Area{ rows: data.rows, columns: data.columns, data: result };
}
fn print_area(a: &Area) {
let mut out = io::stdout();
for y in 0..a.rows {
let start = y * a.columns;
let end = (y+1) * a.columns;
out.write(&a.data[start..end]);
out.write(&['\n' as u8]);
}
out.write(&['\n' as u8]);
}
fn resource_value(data: &Area) -> u64 {
let (mut trees, mut lumber) : (u64, u64) = (0, 0);
for i in 0..(data.rows * data.columns) {
match data.data[i] as char {
'|' => trees += 1,
'#' => lumber += 1,
_ => ()
}
}
return trees * lumber;
}
fn main() {
let mut area = read_stdin();
println!("Initially: {}", resource_value(&area));
print_area(&area);
for m in 1..11 {
area = step(&area);
println!("After {} minutes: {}", m, resource_value(&area));
print_area(&area);
}
}