r/adventofcode • • Dec 02 '16

SOLUTION MEGATHREAD --- 2016 Day 2 Solutions ---

--- Day 2: Bathroom Security ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


BLINKENLIGHTS ARE MANDATORY [?]

Edit: Told you they were 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!

20 Upvotes

210 comments sorted by

View all comments

Show parent comment

2

u/rgdmarshall Dec 02 '16 edited Dec 02 '16

More Rust:

static INPUT: &'static str = "ULL
RRDDD
LURDL
UUUUD";

lazy_static! {
    static ref PAD_MOVEMENT: Vec<[u32;4]> = {
        vec![
            [ 1, 4, 1, 2 ], [ 2, 5, 1, 3 ], [ 3, 6, 2, 3 ],
            [ 1, 7, 4, 5 ], [ 2, 8, 4, 6 ], [ 3, 9, 5, 6 ],
            [ 4, 7, 7, 8 ], [ 5, 8, 7, 9 ], [ 6, 9, 8, 9 ]
        ]
    };
    static ref COMMITTEE_PAD_MOVEMENT: Vec<[u32;4]> = {
        vec![
            [ 1, 3, 1, 1 ], [ 2, 6, 2, 3 ], [ 1, 7, 2, 4 ], [ 4, 8, 3, 4 ],
            [ 5, 5, 5, 6 ], [ 2, 10, 5, 7 ], [ 3, 11, 6, 8 ], [ 4, 12, 7, 9 ],
            [ 9, 9, 8, 9 ], [ 6, 10, 10, 11 ], [ 7, 13, 10, 12 ], [ 8, 12, 11, 12 ],
            [ 11, 13, 13, 13 ]
        ]
    };
}

use std::char;

fn pad_walk(movt: &Vec<[u32;4]>) -> String {
    let mut position: u32 = 5;
    let mut code = String::new();
    let base = movt.len() + 1;
    for key_dance in INPUT.lines() {
        for chr in key_dance.as_bytes() {
            if let Some(idx) = "UDLR".find(char::from_u32(*chr as u32).expect("direction")) {
                position = movt[(position - 1) as usize][idx];
            }
        }
        code.push(char::from_digit(position, base as u32).expect("digit"));
    }
    code
}

pub fn one() {
    println!("The code is {}", pad_walk(&PAD_MOVEMENT));
}

pub fn two() {
    println!("The code is {}", pad_walk(&COMMITTEE_PAD_MOVEMENT));
}

*Edit: This is module source, to run it as a standalone program add #[macro_use] extern crate lazy_static; and fn main().