2017/10-11
This commit is contained in:
4
2017/10/Cargo.lock
generated
Normal file
4
2017/10/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "day-10"
|
||||
version = "0.1.0"
|
||||
|
||||
6
2017/10/Cargo.toml
Normal file
6
2017/10/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day-10"
|
||||
version = "0.1.0"
|
||||
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
1
2017/10/input.txt
Normal file
1
2017/10/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
225,171,131,2,35,5,0,13,1,246,54,97,255,98,254,110
|
||||
81
2017/10/src/main.rs
Normal file
81
2017/10/src/main.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use std::io::{self, Read};
|
||||
|
||||
|
||||
fn hash(input: &[usize], list: &mut [usize], rounds: usize) {
|
||||
let len = list.len();
|
||||
|
||||
let mut index = 0;
|
||||
let mut skip = 0;
|
||||
|
||||
for _ in 0..rounds {
|
||||
for x in input {
|
||||
if *x > 0 {
|
||||
let mut start = index;
|
||||
let mut end = (index + *x - 1) % len;
|
||||
|
||||
for _ in 0..(*x / 2) {
|
||||
list.swap(start, end);
|
||||
|
||||
start = if start == len - 1 { 0 } else { start + 1 };
|
||||
end = if end == 0 { len - 1 } else { end - 1 };
|
||||
}
|
||||
}
|
||||
|
||||
index = (index + *x + skip) % len;
|
||||
skip += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_to_string(&mut input)
|
||||
.expect("faild to read input");
|
||||
|
||||
for line in input.lines().filter(|line| !line.is_empty()) {
|
||||
let input = line.split(',')
|
||||
.filter_map(|num| num.parse::<usize>().ok())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut list = (0..256).collect::<Vec<_>>();
|
||||
|
||||
hash(&input, &mut list, 1);
|
||||
|
||||
println!("part_one={}", list[0] * list[1]);
|
||||
|
||||
let input = line.bytes()
|
||||
.chain([17, 31, 73, 47, 23].iter().cloned())
|
||||
.map(usize::from)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut list = (0..256).collect::<Vec<_>>();
|
||||
|
||||
hash(&input, &mut list, 64);
|
||||
|
||||
let hex = list.chunks(16)
|
||||
.map(|chunk| chunk.iter().fold(0, |a, b| a ^ b))
|
||||
.map(|block| format!("{:02x}", block))
|
||||
.collect::<String>();
|
||||
|
||||
println!("part_two={}", hex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
let mut list = [0, 1, 2, 3, 4];
|
||||
|
||||
hash(&[3, 4, 1, 5], &mut list, 1);
|
||||
|
||||
assert_eq!(list, [3, 4, 2, 1, 0]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user