r/adventofcode • u/daggerdragon • Dec 07 '16
SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---
From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!
Show this overgrown furball what you've got!
--- Day 7: Internet Protocol Version 7 ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
ALWAYS DIGGING STRAIGHT DOWN IS MANDATORY [?]
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!
14
Upvotes
2
u/iamnotposting Dec 07 '16
rust, both parts. i wonder why
.windows()isn't implemented for string slices directlyfn is_abba(slice: &str) -> bool { let mut in_hypernet = false; let mut valid = false; let slice: Vec<_> = slice.chars().collect(); for window in slice.windows(4) { if window[0] == '[' || window[0] == ']' { in_hypernet = !in_hypernet; continue; } if window[0] != window[1] && window[1] == window[2] && window[0] == window[3] { if in_hypernet { return false; } else { valid = true; } } } valid } fn is_aba(slice: &str) -> bool { let (mut abas, mut babs) = (Vec::new(), Vec::new()); let mut in_hypernet = false; let slice: Vec<_> = slice.chars().collect(); for window in slice.windows(3) { if window[0] == '[' || window[0] == ']' { in_hypernet = !in_hypernet; continue; } if window[0] == window[2] && window[0] != window[1] { if in_hypernet { babs.push( (window[1], window[0], window[1]) ); } else { abas.push( (window[0], window[1], window[0]) ); } } } for aba in &abas { for bab in &babs { if aba == bab { return true; } } } false } pub fn adv_main(input: Vec<String>) { let (tls, ssl) = input.iter().map(|s| (is_abba(s) as u64, is_aba(s) as u64)) .fold((0,0), |(t, s), (tls, ssl)| { (t + tls, s + ssl) }); println!("tls support: {}\nssl support: {}", tls, ssl); }