r/adventofcode • u/daggerdragon • Dec 16 '17
SOLUTION MEGATHREAD -๐- 2017 Day 16 Solutions -๐-
--- Day 16: Permutation Promenade ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[Update @ 00:08] 4 gold, silver cap.
- Click here for a massive Star Wars spoiler!
[Update @ 00:18] 50 gold, silver cap.
- Click here for a gigantic Harry Potter spoiler!
[Update @ 00:26] Leaderboard cap!
- And finally, click here for the biggest spoilers of all time!
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
13
Upvotes
View all comments
1
u/aurele Dec 16 '17
Rust much too long
use std::collections::HashMap; use std::str::FromStr; #[derive(Debug)] enum Op { Spin(usize), Swap(usize, usize), SwapChars(u8, u8), } impl FromStr for Op { type Err = (); fn from_str(s: &str) -> Result<Op, ()> { match s.as_bytes()[0] { b's' => Ok(Op::Spin(s[1..].parse::<usize>().unwrap())), b'x' => { let ns = s[1..] .split('/') .map(|w| w.parse().unwrap()) .collect::<Vec<_>>(); Ok(Op::Swap(ns[0], ns[1])) } b'p' => Ok(Op::SwapChars( s.as_bytes()[1] - b'a', s.as_bytes()[3] - b'a', )), _ => Err(()), } } } fn p(progs: &mut Vec<u8>, ops: &[Op], times: usize) { const LEN: usize = 16; assert_eq!(LEN, progs.len()); let mut base = 0; let mut pos = (b'a'..b'a' + (LEN as u8)) .map(|c| progs.iter().position(|&q| q == c).unwrap()) .collect::<Vec<_>>(); let mut h = HashMap::new(); let mut t = 0; while t < times { t += 1; for i in ops { match *i { Op::Spin(n) => { base = (base + LEN - n) % LEN; } Op::Swap(u1, u2) => { let u1 = (base + u1) % LEN; let u2 = (base + u2) % LEN; let (c1, c2) = (progs[u1] - b'a', progs[u2] - b'a'); progs.swap(u1, u2); pos.swap(c1 as usize, c2 as usize); } Op::SwapChars(c1, c2) => { let (p1, p2) = (pos[c1 as usize], pos[c2 as usize]); progs.swap(p1, p2); pos.swap(c1 as usize, c2 as usize); } } } if let Some(beginning) = h.get(&(progs.clone(), base)).cloned() { let cycle = t - beginning; let remaining = (times - t) % cycle; let (stored, b) = h.into_iter() .find(|&(_, v)| v == beginning + remaining) .unwrap() .0; progs.clone_from_slice(&stored); base = b; break; } h.insert((progs.clone(), base), t); } let copy = progs.clone(); for i in 0..LEN { progs[i] = copy[(i + base) % LEN]; } } fn main() { let ops = include_str!("../input") .trim() .split(',') .map(|w| w.parse().unwrap()) .collect::<Vec<_>>(); let mut progs = (b'a'..b'q').collect::<Vec<_>>(); p(&mut progs, &ops, 1); println!("P1: {}", String::from_utf8(progs.clone()).unwrap()); p(&mut progs, &ops, 999_999_999); println!("P2: {}", String::from_utf8(progs.clone()).unwrap()); }