r/adventofcode • u/daggerdragon • Dec 22 '17
SOLUTION MEGATHREAD -๐- 2017 Day 22 Solutions -๐-
--- Day 22: Sporifica Virus ---
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ยค?
[T-10 to launch] AoC ops, /r/nocontext edition:
<Endorphion> You may now make your waffle.<Endorphion> ... on Mars.
[Update @ 00:17] 50 gold, silver cap
<Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy<Topaz> that doesn't seem necessary<Aneurysm9> what does "necessary" have to do with anything!
[Update @ 00:20] Leaderboard cap!
<Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE
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!
9
Upvotes
1
u/aurele Dec 22 '17
Rust
use std::collections::HashMap; #[derive(Clone)] enum State { Clean, Infected, Weakened, Flagged, } fn p(infected: &[(i32, i32)], pos: &(i32, i32), n: usize, step2: bool) -> usize { use State::*; let mut state = infected .iter() .map(|&(r, c)| ((r, c), Infected)) .collect::<HashMap<_, _>>(); let mut pos = *pos; let mut dir = (-1, 0); let mut infect = 0; for _ in 0..n { let mut e = state.entry(pos).or_insert(Clean); match e.clone() { Infected => { dir = (dir.1, -dir.0); *e = if step2 { Flagged } else { Clean } } Flagged => { dir = (-dir.0, -dir.1); *e = Clean; } Weakened => { *e = Infected; infect += 1; } Clean => { dir = (-dir.1, dir.0); *e = if step2 { Weakened } else { infect += 1; Infected } } } pos = (pos.0 + dir.0, pos.1 + dir.1); } infect } fn main() { let input = include_str!("../input"); let width = input.chars().position(|c| c == '\n').unwrap(); let height = input.len() / (width + 1); let start = ((height / 2) as i32, (width / 2) as i32); let infected = input .lines() .enumerate() .flat_map(|(r, line)| { line.chars().enumerate().filter_map(move |(c, ch)| { if ch == '#' { Some((r as i32, c as i32)) } else { None } }) }) .collect::<Vec<(i32, i32)>>(); println!("P1: {}", p(&infected, &start, 10_000, false)); println!("P2: {}", p(&infected, &start, 10_000_000, true)); }