Esempio n. 1
0
        public CodesResponse GetCode(CodesRequest request)
        {
            if (!_helper.IsDayYearValid(request.Day, request.Year))
            {
                throw new DayOrYearNotValidException();
            }

            var language = request.Language ?? Constants.DefaultLanguage;
            var script   = _helper.BuildScriptPath(request.Year, request.Day, language);

            if (!File.Exists(script))
            {
                throw new DayNotImplementedException();
            }

            return(new CodesResponse
            {
                Language = language,
                Content = File.ReadAllText(script)
            });
        }
Esempio n. 2
0
        public SolutionsResponse GetSolution(SolutionsRequest request)
        {
            if (!_helper.IsDayYearValid(request.Day, request.Year))
            {
                throw new DayOrYearNotValidException();
            }

            var language = Constants.DefaultLanguage;
            var script   = _helper.BuildScriptPath(request.Year, request.Day, language);

            if (!File.Exists(script))
            {
                throw new DayNotImplementedException();
            }

            var interpreter = _config[language];

            if (!File.Exists(interpreter))
            {
                throw new InterpreterNotFoundException();
            }

            File.WriteAllText(@"..\input\" + request.Day + ".txt", request.Input);

            var(err, result) = RunScript(interpreter, script);

            var parts = result.Split("\r\n");

            if (parts.Length > 2)
            {
                throw new UnexpectedResultLengthException(result);
            }

            return(new SolutionsResponse
            {
                Part1 = parts.Length > 0 ? parts[0] : null,
                Part2 = parts.Length > 1 ? parts[1] : null,
                Error = parts.Length > 2 ? "Output: " + string.Join(";", parts.Skip(2)) + (err == null ? "" : "\nError: " + err) : err
            });
        }