01020304050607080910111213141516171819202122232425

Advent of Code

2016/2

Bathroom Security

in C#

by encse

You arrive at Easter Bunny Headquarters under cover of darkness. However, you left in such a rush that you forgot to use the bathroom! Fancy office buildings like this one usually have keypad locks on their bathrooms, so you search the front desk for the code.

"In order to improve security," the document you find says, "bathroom codes will no longer be written down. Instead, please memorize and follow the procedure below to access the bathrooms."

Read the full puzzle.

using System;

namespace AdventOfCode.Y2016.Day02;

[ProblemName("Bathroom Security")]
class Solution : Solver {

    public object PartOne(string input) => Solve(input, "123\n456\n789");
    public object PartTwo(string input) => Solve(input, "  1  \n 234 \n56789\n ABC \n  D  ");

    string Solve(string input, string keypad) {
        var res = "";
        var lines = keypad.Split('\n');
        var (crow, ccol) = (lines.Length, lines[0].Length);
        var (irow, icol) = (crow / 2, ccol / 2);
        foreach (var line in input.Split('\n')) {
            foreach (var ch in line) {
                var (drow, dcol) = ch switch {
                    'U' => (-1, 0),
                    'D' =>  (1, 0),
                    'L' => (0, -1),
                    'R' => (0, 1),
                    _ => throw new ArgumentException()
                };

                var (irowT, icolT) = (irow + drow, icol + dcol);
                if (irowT >= 0 && irowT < crow && icolT >= 0 && icolT < ccol && lines[irowT][icolT] != ' ') {
                    (irow, icol) = (irowT, icolT);
                }
            }
            res += lines[irow][icol];
        }
        return res;
    }
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator