Skip to content
This repository was archived by the owner on Feb 14, 2020. It is now read-only.

Commit 038a1c9

Browse files
committed
Day 14
1 parent fe31ed5 commit 038a1c9

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

2017/input/day14.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jzgqcdpd

2017/src/bin/day14.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::io::{self, Read};
2+
3+
fn reverse(ring: &mut Vec<u32>, index: usize, len: usize) {
4+
for i in 0..(len / 2) {
5+
let a = (index + i) % ring.len();
6+
let b = (index + len - 1 - i) % ring.len();
7+
let x = ring[a];
8+
ring[a] = ring[b];
9+
ring[b] = x;
10+
}
11+
}
12+
13+
fn hash(input: &str) -> Vec<u8> {
14+
let mut ring: Vec<_> = (0..256).collect();
15+
let mut index = 0;
16+
let mut skip = 0;
17+
18+
let mut lens: Vec<_> = input.as_bytes().to_vec();
19+
lens.extend(&[17, 31, 73, 47, 23]);
20+
21+
for _ in 0..64 {
22+
for len in lens.iter().cloned() {
23+
reverse(&mut ring, index, len as usize);
24+
index += len as usize + skip;
25+
index %= ring.len();
26+
skip += 1;
27+
}
28+
}
29+
30+
ring.chunks(16)
31+
.map(|chunk| chunk.iter().fold(0, |a, b| a ^ b))
32+
.map(|x| x as u8)
33+
.collect()
34+
}
35+
36+
fn solve1(input: &str) -> u32 {
37+
(0..128)
38+
.map(|i| hash(&format!("{}-{}", input, i)))
39+
.map(|h| h.into_iter().map(u8::count_ones).sum::<u32>())
40+
.sum()
41+
}
42+
43+
fn main() {
44+
let mut input = String::new();
45+
io::stdin().read_to_string(&mut input).unwrap();
46+
47+
println!("Part 1: {}", solve1(input.trim()));
48+
}
49+
50+
#[test]
51+
fn part1() {
52+
assert_eq!(8108, solve1("flqrgnkx"));
53+
}

0 commit comments

Comments
 (0)