コード例 #1
0
        private static void InitEntries()
        {
            savedEntries = new List <TramLog>();
            var values = data.Split('\n').Select(d => d.Trim().Split(',')).ToList();

            foreach (var v in values)
            {
                var tl = new TramLog();
                tl.Id       = int.Parse(v[0].Substring(2));
                tl.TheirId  = v[1];
                tl.ParsedId = long.Parse(v[1]);
                savedEntries.Add(tl);
            }
        }
コード例 #2
0
        public static int?GuessId(string vehicleId)
        {
            if (savedEntries == null)
            {
                InitEntries();
            }

            if (string.IsNullOrWhiteSpace(vehicleId))
            {
                return(null);
            }

            long pId = 0;

            if (!long.TryParse(vehicleId, out pId))
            {
                return(null);
            }

            TramLog result   = null;
            long    distance = long.MaxValue;

            foreach (var tl in savedEntries)
            {
                var d = Math.Abs(tl.ParsedId - pId);
                if (d < 400 && d < distance && d % 4 == 0)
                {
                    if (d == 0)
                    {
                        return(tl.Id);
                    }

                    distance = d;
                    result   = tl;
                }
            }

            if (result == null)
            {
                return(null);
            }

            var realDist = result.ParsedId - pId;

            return((int)(result.Id - (realDist / 4)));
        }