From 2f73e55b7fe3a375425ad49e4f269c9edd427a47 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Fri, 15 Dec 2017 13:53:00 -0500 Subject: Day 10 --- 2017/src/bin/day10.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2017/src/bin/day10.rs (limited to '2017/src') diff --git a/2017/src/bin/day10.rs b/2017/src/bin/day10.rs new file mode 100644 index 0000000..af90961 --- /dev/null +++ b/2017/src/bin/day10.rs @@ -0,0 +1,38 @@ +use std::io::{self, Read}; + +fn reverse(ring: &mut Vec, index: usize, len: usize) { + for i in 0..(len / 2) { + let a = (index + i) % ring.len(); + let b = (index + len - 1 - i) % ring.len(); + let x = ring[a]; + ring[a] = ring[b]; + ring[b] = x; + } +} + +fn solve1(size: u32, input: &str) -> u32 { + let mut ring: Vec<_> = (0..size).collect(); + let mut index = 0; + let mut skip = 0; + + for len in input.split(',').map(str::parse).map(Result::unwrap) { + reverse(&mut ring, index, len); + index += len + skip; + index %= ring.len(); + skip += 1; + } + + ring[0] * ring[1] +} + +fn main() { + let mut input = String::new(); + io::stdin().read_to_string(&mut input).unwrap(); + + println!("Part 1: {}", solve1(256, input.trim())); +} + +#[test] +fn part1() { + assert_eq!(12, solve1(5, "3,4,1,5")); +} -- cgit 1.4.1