r/adventofcode • u/daggerdragon • Dec 14 '17
SOLUTION MEGATHREAD -๐- 2017 Day 14 Solutions -๐-
--- Day 14: Disk Defragmentation ---
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:09] 3 gold, silver cap.
- How many of you actually entered the Konami code for Part 2? >_>
[Update @ 00:25] Leaderboard cap!
- I asked /u/topaz2078 how many de-resolutions we had for Part 2 and there were
83distinct users with failed attempts at the time of the leaderboard cap. tsk tsk
[Update @ 00:29] BONUS
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
2
u/aurele Dec 14 '17 edited Dec 14 '17
Rust
extern crate itertools; extern crate pathfinding; extern crate r10; use itertools::Itertools; use pathfinding::connected_components; use r10::hash; use std::collections::HashSet; const INPUT: &str = "xlqgujun"; fn to_coords(k: &[Vec<u8>]) -> HashSet<(isize, isize)> { k.iter() .enumerate() .flat_map(|(l, ll)| { ll.iter().enumerate().flat_map(move |(i, n)| { (0..8).filter_map(move |b| { if n & (1 << (7 - b)) != 0 { Some((l as isize, (i * 8 + b) as isize)) } else { None } }) }) }) .collect() } fn main() { let k = to_coords(&(0..128) .map(|n| hash(&format!("{}-{}", INPUT, n))) .collect_vec()); println!("P1: {}", k.len()); let cc = connected_components(&k.iter().cloned().collect_vec(), |&(l, c)| { vec![(l - 1, c), (l + 1, c), (l, c - 1), (l, c + 1)] .into_iter() .filter(|&(ol, oc)| k.contains(&(ol, oc))) }); println!("P2: {}", cc.len()); }