-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.rs
91 lines (78 loc) · 1.86 KB
/
day14.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
fn reverse(l: &mut [u8; 256], pos: usize, size: usize) {
for i in 0..(size / 2) {
let a = (pos + i) % l.len();
let b = (pos + size - 1 - i) % l.len();
let tmp = l[a];
l[a] = l[b];
l[b] = tmp;
}
}
fn hash_it(instr: &str) -> [i32; 128] {
let mut l = [0u8; 256];
for i in 0..l.len() {
l[i] = i as u8;
}
let raw = instr.as_bytes();
let input = [raw, &[17, 31, 73, 47, 23]].concat();
let mut skip = 0;
let mut pos = 0;
for _ in 0..64 {
for s in input.iter() {
reverse(&mut l, pos, *s as usize);
pos += (*s as usize) + skip;
skip += 1;
}
}
let mut binresult = [0i32; 128];
for a in 0..16 {
let mut r = 0;
for b in 0..16 {
r ^= l[a * 16 + b];
}
for c in 0..8 {
if r & (1 << c) != 0 {
binresult[a * 8 + (7 - c)] = 1;
}
}
}
binresult
}
fn fill(x: i32, y: i32, v: &mut Vec<[i32; 128]>) {
if x < 0 || x >= 128 || y < 0 || y >= 128 {
return;
}
if v[y as usize][x as usize] != 1 {
return;
}
v[y as usize][x as usize] = -1;
fill(x + 1, y, v);
fill(x - 1, y, v);
fill(x, y + 1, v);
fill(x, y - 1, v);
}
fn tasks(input: &str) -> (i32, i32) {
let mut sum = 0i32;
let mut v = Vec::new();
for i in 0..128 {
let br = hash_it(&format!("{}-{}", input, i));
for j in 0..128 {
sum += br[j];
//print!("{}", br[j]);
}
//println!("");
v.push(br);
}
let mut islands = 0;
for y in 0..128 {
for x in 0..128 {
if v[y][x] == 1 {
fill(x as i32, y as i32, &mut v);
islands += 1;
}
}
}
(sum, islands)
}
fn main() {
println!("{:?}", tasks("ljoxqyyw"));
}