-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.rs
76 lines (74 loc) · 1.21 KB
/
day08.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
use std::fs::File;
use std::io::Read;
fn parse (s: &str) -> Vec<i32> {
let mut v = Vec::new();
for c in s.chars() {
match c as u8 {
n @ 48..=57 => v.push(n as i32 - 48),
_ => (),
};
}
v
}
fn main () -> std::io::Result<()> {
let mut data = String::new();
let mut file = File::open("input08")?;
file.read_to_string(&mut data)?;
let v = parse(&data);
let mut ls = Vec::new();
ls.push(Vec::new());
for p in &v {
let l = ls.len() - 1;
ls[l].push(p);
if ls[l].len() == 150 {
ls.push(Vec::new());
}
}
let mut fewest = (-1,std::i32::MAX);
for i in 0..ls.len() {
let mut zs = 0;
for p in &ls[i] {
if **p == 0 {
zs += 1;
}
}
if zs != 0 && zs < fewest.1 {
println!("{}",zs);
fewest = (i as i32,zs);
}
}
let mut xs = (0,0);
for p in &ls[fewest.0 as usize] {
if **p == 1 {
xs.0 += 1;
}
if **p == 2 {
xs.1 += 1;
}
}
println!("{}",xs.0*xs.1);
let mut img = String::new();
let mut p = 0;
for i in 0..150 {
for l in &ls {
match l[i] {
0 => {
img.push(' ');
break;
},
1 => {
img.push('x');
break;
},
_ => (),
}
}
p += 1;
if p == 25 {
img.push('\n');
p = 0;
}
}
println!("{}",img);
Ok(())
}