public static void Run(string[] args) { string algorithmFilePath = string.Empty; TicketCountAlgorithms currentAlgorhytm = TicketCountAlgorithms.None; if (args.Length == 0) { algorithmFilePath = UI.RequestFilePath(); } else { algorithmFilePath = args[0]; } AlgorithmGetter algorithmGet = new AlgorithmGetter(new AlgorithmFileReader()); currentAlgorhytm = algorithmGet.GetAlgorhytm(algorithmFilePath); if (currentAlgorhytm != TicketCountAlgorithms.None) { ulong ticketCount = 0; LuckyTicketsCounter counter; try { switch (currentAlgorhytm) { case TicketCountAlgorithms.Moscow: counter = new LuckyTicketsCounterMoscow(DEFAULT_DIGITS_NUMBER); ticketCount = counter.GetLuckyTicketsQuantity(); break; case TicketCountAlgorithms.Piter: counter = new LuckyTicketsCounterPiter(DEFAULT_DIGITS_NUMBER); ticketCount = counter.GetLuckyTicketsQuantity(); break; default: break; } } catch (InvalidTicketDigitsCountException ex) { UI.PrintExceptionMessage(ex); } catch (RecursionDepthTooBigException ex) { UI.PrintExceptionMessage(ex); } UI.PrintTicketsCount(ticketCount); } else { UI.PrintHelp(Path.GetFullPath(algorithmFilePath)); } }
public static void Run(string[] args) { string algorithmFilePath = string.Empty; if (args.Length == 0) { algorithmFilePath = UI.RequestFilePath(); } else { algorithmFilePath = args[0]; } algorithmFilePath = Path.GetFullPath(algorithmFilePath); if (!TryValidateFilePath(ref algorithmFilePath)) { UI.PrintHelp(algorithmFilePath); return; } TicketCountAlgorithms currentAlgorhytm; if (TryGetAlgorhytm(algorithmFilePath, out currentAlgorhytm)) { ulong ticketCount = 0; try { if (currentAlgorhytm == TicketCountAlgorithms.Moscow) { ticketCount = new LuckyTicketsCounterMoscow(DEFAULT_DIGITS_NUMBER).GetLuckyTicketsQuantity(); } if (currentAlgorhytm == TicketCountAlgorithms.Piter) { ticketCount = new LuckyTicketsCounterPiter(DEFAULT_DIGITS_NUMBER).GetLuckyTicketsQuantity(); } } catch (InvalidTicketDigitsCountException ex) { UI.PrintExceptionMessage(ex); } catch (RecursionDepthTooBigException ex) { UI.PrintExceptionMessage(ex); } UI.PrintTicketsCount(ticketCount); } else { UI.PrintHelp(algorithmFilePath); } }
/// <summary> /// Calculating the number of "Petersburg-type" lucky tickets can be done by transferring /// all the even digits to the right part of the number, while transferring all the odd /// digits to the left part (or vice-versa), then using the "Moscow-type" tickets calculating /// algorithm. /// So the quantity of "Petersburg-type" tickets is always equal to "Moscow-type" tickets quantity. /// Thus, creating the scpecific algorithm for calculating "Petersburg-type" tickets quantity /// is redundant. /// ///Proof: http://kvant.mccme.ru/1975/07/razgovor_v_tramvae.htm /// </summary> /// <param name="digits"></param> /// <returns></returns> public override ulong GetLuckyTicketsQuantity() { LuckyTicketsCounterMoscow ticketCounter = new LuckyTicketsCounterMoscow(TicketDigitsCount); return(ticketCounter.GetLuckyTicketsQuantity()); }