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
|
extern crate advent_of_code_2017;
use advent_of_code_2017::*;
fn main() {
let args = AdventArgs::init();
let lengths: Vec<usize> = if args.part == 1 {
args.input[0].split(",").map(|x| x.parse().unwrap()).collect()
} else {
let suffix: [usize; 5] = [17, 31, 73, 47, 23];
args.input[0].as_bytes()
.iter().map(|&x| x as usize)
.chain(suffix.iter().cloned())
.collect()
};
let mut position = 0;
let mut list: Vec<u32> = (0..256).collect();
if args.part == 1 {
hash_round(&mut list, &lengths, &mut position, 0);
} else {
for i in 0..64 {
let skip = lengths.len() * i;
hash_round(&mut list, &lengths, &mut position, skip);
}
}
if args.part == 1 {
let answer = list[0]*list[1];
println!("{}", answer);
} else {
let mut current_char = 0;
for (i, l) in list.iter().enumerate() {
current_char = current_char ^ l;
if i % 16 == 15 {
print!("{:02x}", current_char);
current_char = 0;
}
}
println!("");
}
}
fn hash_round(list: &mut Vec<u32>, lengths: &Vec<usize>, position: &mut usize, skip: usize) {
for (inner_skip, &length) in lengths.iter().enumerate() {
reverse(list, *position, length);
*position = (*position + length + skip + inner_skip) % list.len();
}
}
fn reverse(list: &mut Vec<u32>, position: usize, length: usize) {
let mut a = position;
let mut b = position + length - 1;
let len = list.len();
while a < b {
list.swap(a%len, b%len);
a += 1;
b -= 1;
}
}
|