예제 #1
0
            public void Move(String instruction)
            {
                Match m     = InstructionRegex.Match(instruction);
                char  key   = m.Groups[1].Value[0];
                long  value = long.Parse(m.Groups[2].Value);

                switch (key)
                {
                case 'N':
                case 'S':
                case 'E':
                case 'W':
                    m_Waypoint.Move(key, value);
                    break;

                case 'L':
                    m_Waypoint.Rotate((int)(360 - value % 360));
                    break;

                case 'R':
                    m_Waypoint.Rotate((int)(value % 360));
                    break;

                case 'F':
                    Distance_NS += value * m_Waypoint.Distance_NS;
                    Distance_EW += value * m_Waypoint.Distance_EW;
                    break;
                }
            }
예제 #2
0
        public int Solve()
        {
            var instructions = Input.SplitByNewLines().Select(line => InstructionRegex.Match(line));
            var registers    = new Dictionary <string, int>();

            foreach (var instruction in instructions)
            {
                var registerToModify   = instruction.Groups["targetreg"].Value;
                var registerToTest     = instruction.Groups["testreg"].Value;
                var valueToTestAgainst = registers.ContainsKey(registerToTest) ? registers[registerToTest] : 0;
                var valueToTest        = int.Parse(instruction.Groups["testval"].Value);
                var testSucceeded      = false;

                switch (instruction.Groups["cond"].Value)
                {
                case ">":
                    testSucceeded = valueToTestAgainst > valueToTest;
                    break;

                case ">=":
                    testSucceeded = valueToTestAgainst >= valueToTest;
                    break;

                case "==":
                    testSucceeded = valueToTestAgainst == valueToTest;
                    break;

                case "!=":
                    testSucceeded = valueToTestAgainst != valueToTest;
                    break;

                case "<":
                    testSucceeded = valueToTestAgainst < valueToTest;
                    break;

                case "<=":
                    testSucceeded = valueToTestAgainst <= valueToTest;
                    break;

                default:
                    throw new InvalidOperationException($"Unable to parse condition in {instruction.Value}");
                }

                var valueToChangeBy = testSucceeded ? int.Parse(instruction.Groups["val"].Value) : 0;

                if (instruction.Groups["op"].Value == "dec")
                {
                    valueToChangeBy = -valueToChangeBy;
                }

                registers[registerToModify] = (registers.ContainsKey(registerToModify) ? registers[registerToModify] : 0) + valueToChangeBy;
            }

            return(registers.Max(regWithValue => regWithValue.Value));
        }