using System; using System.Collections.Generic; namespace CodeDojo.Linjeteller { internal class Program { private static void Main(string[] args) { var startTime = DateTime.Now; var startState = ConstructParser(); var initTime = DateTime.Now; foreach (var s in args) { var runTime = DateTime.Now; var linesOfCode = GetLinesOfCode(s, startState); var stepTime = DateTime.Now; Console.WriteLine("Antall linjer: {0}", linesOfCode); Console.WriteLine("Run: {0}", (stepTime - runTime).Milliseconds); Console.WriteLine("------------------------"); } var stopTime = DateTime.Now; Console.WriteLine("InitTime: {0}", (stopTime - startTime).Milliseconds); Console.Read(); } private static State ConstructParser() { var startState = new State(); var firstSlashState = new State(); var secondSlashState = new State(); var firstSlashTextState = new State(); var secondTextSlashState = new State(); var firstAsteriskState = new State(); var secondAsteriskState = new State(); var firstTextAsteriskState = new State(); var secondTextAsteriskState = new State(); var textState = new State() { IsCode = true }; var quoteState = new State(); #region startstate startState.DefaultState = textState; startState.ValidStates.Add('/', firstSlashState); startState.ValidStates.Add('\t', startState); startState.ValidStates.Add('\n', startState); startState.ValidStates.Add(' ', startState); #endregion #region quoteState quoteState.DefaultState = quoteState; quoteState.ValidStates.Add('\n', startState); quoteState.ValidStates.Add('"', textState); #endregion #region textState textState.DefaultState = textState; textState.ValidStates.Add('/', firstSlashTextState); textState.ValidStates.Add('"', quoteState); textState.ValidStates.Add('\n', startState); #endregion #region firstSlashTextState firstSlashTextState.DefaultState = textState; firstSlashTextState.ValidStates.Add('/', secondTextSlashState); firstSlashTextState.ValidStates.Add('*', firstTextAsteriskState); #endregion #region secondSlashState secondTextSlashState.DefaultState = secondTextSlashState; secondTextSlashState.ValidStates.Add('\n', startState); #endregion #region firstSlashState firstSlashState.ValidStates.Add('/', secondSlashState); firstSlashState.ValidStates.Add('*', firstAsteriskState); firstSlashState.DefaultState = textState; #endregion #region secondSlashState secondSlashState.DefaultState = secondSlashState; secondSlashState.ValidStates.Add('\n', startState); #endregion #region firstTextAsteriskState firstTextAsteriskState.DefaultState = firstTextAsteriskState; firstTextAsteriskState.ValidStates.Add('*', secondTextAsteriskState); #endregion #region secondTextAsteriskState secondTextAsteriskState.DefaultState = firstTextAsteriskState; secondTextAsteriskState.ValidStates.Add('/', startState); #endregion #region firstAsteriskState firstAsteriskState.ValidStates.Add('*', secondAsteriskState); firstAsteriskState.DefaultState = firstAsteriskState; #endregion #region secondAsteriskState secondAsteriskState.DefaultState = firstAsteriskState; secondAsteriskState.ValidStates.Add('/', startState); #endregion return startState; } public static int GetLinesOfCode(string pathToFile, State currentState) { if (string.IsNullOrEmpty(pathToFile)) throw new ArgumentException(); var file = System.IO.File.ReadAllLines(pathToFile); var counter = 0; foreach (var line in file) { var lineHasBeenIncremented = false; foreach (var c in line.ToCharArray()) { currentState = NewState(currentState, c); if (currentState.IsCode && !lineHasBeenIncremented) { lineHasBeenIncremented = true; counter++; } } currentState = NewState(currentState, '\n'); } return counter; } private static State NewState(State currentState, char c) { if (currentState.ValidStates.ContainsKey(c)) { currentState = currentState.ValidStates[c]; } else if (currentState.DefaultState != null) { currentState = currentState.DefaultState; } return currentState; } } public class State { public Dictionary ValidStates { get; set; } public State DefaultState = null; public bool IsCode = false; public State() { ValidStates = new Dictionary(); } } }