-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_10.rs
98 lines (87 loc) · 2.48 KB
/
day_10.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
//! https://adventofcode.com/2017/day/10
//! https://adventofcode.com/2017/day/10/input
use std::{fs::read_to_string, time::Instant};
fn reverse(numbers: &mut [usize], from: usize, length: usize) {
for i in 0..length / 2 {
numbers.swap(
(from + i) % numbers.len(),
(from + length - i - 1) % numbers.len(),
);
}
}
fn round(
numbers: &mut [usize],
lengths: &[usize],
mut cp: usize,
mut skip_size: usize,
) -> (usize, usize) {
for &length in lengths {
reverse(numbers, cp, length);
cp += length + skip_size;
skip_size += 1;
}
(cp, skip_size)
}
pub mod part1 {
use super::round;
fn parse(input: &str) -> Vec<usize> {
input.split(',').map(|n| n.parse().unwrap()).collect()
}
pub fn solve(input: &str) -> usize {
let lengths = parse(input);
let mut numbers: Vec<_> = (0..256).collect();
round(&mut numbers, &lengths, 0, 0);
numbers[0] * numbers[1]
}
}
pub mod part2 {
use super::round;
fn parse(input: &str) -> Vec<usize> {
input
.chars()
.map(|char| char as u8 as usize)
.chain([17, 31, 73, 47, 23])
.collect()
}
pub fn hash(input: &str) -> Vec<usize> {
let lengths = parse(input);
let mut numbers: Vec<_> = (0..256).collect();
let (mut cp, mut skip_size) = (0, 0);
for _ in 0..64 {
(cp, skip_size) = round(&mut numbers, &lengths, cp, skip_size);
cp %= numbers.len();
skip_size %= numbers.len();
}
let mut xors = vec![0; 16];
for i in 0..16 {
let mut xor = 0;
for j in 0..16 {
xor ^= numbers[i * 16 + j];
}
xors[i] = xor;
}
xors
}
pub fn solve(input: &str) -> String {
let hash = hash(input);
let mut result = String::new();
for bit in hash {
result.push_str(&*format!("{:0>2x}", bit));
}
result
}
}
pub fn main(test: bool) {
let test_input = "1,2,4".to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2017/day_10_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}