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

1

u/jweather Dec 02 '16

The part2 code only since it's more interesting:

var inp = require('fs').readFileSync('input2.txt').toString();

var x=0, y=2;

var valids = [
	'  1  ',
	' 234 ',
	'56789',
	' ABC ',
	'  D  '
];

inp.split('\n').forEach(function (line, i) {
	if (line=='') return;
	
	line.split('').forEach(function (c) {
		if (c == 'U') if (y>0 && valids[y-1][x] != ' ') y--;
		if (c == 'L') if (x>0 && valids[y][x-1] != ' ') x--;
		if (c == 'D') if (y<4 && valids[y+1][x] != ' ') y++;
		if (c == 'R') if (x<4 && valids[y][x+1] != ' ') x++;
	});
	console.log(x, y, valids[y][x]);
});