Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. namespace Wunderpahkina_vol6
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var lines = File.ReadAllLines("input.txt");
  12. var solver = new Solver(lines.Select(int.Parse).ToArray());
  13. var result = solver.Solve();
  14. Console.WriteLine($"Biggest square: {result} units x {result} units (Area: {result * result} units^2)");
  15. }
  16. }
  17.  
  18. public class Solver
  19. {
  20. private readonly int[] _data;
  21.  
  22. public Solver(int[] data)
  23. {
  24. _data = data;
  25. }
  26.  
  27. public int Solve()
  28. {
  29. int rangeMin = 0;
  30. int rangeMax = _data.Length;
  31. while (rangeMin + 1 < rangeMax)
  32. {
  33. // go through the sizes of squares by dividing and conquering
  34. int pivot = rangeMin + (rangeMax - rangeMin) / 2;
  35. // can a square with with a side size like the "pivot" fit?
  36. var canFit = CanFit(pivot);
  37. Console.WriteLine($"Does a square with a side of {pivot} fit? {canFit}");
  38.  
  39. if (canFit)
  40. {
  41. rangeMin = pivot;
  42. }
  43. else
  44. {
  45. rangeMax = pivot;
  46. }
  47. }
  48.  
  49. return rangeMin;
  50. }
  51.  
  52. private bool CanFit(int side)
  53. {
  54. var consecutiveTallEnoughColumns = 0;
  55. for (int index = 0; index < _data.Length; index++)
  56. {
  57. if (_data[index] >= side)
  58. {
  59. consecutiveTallEnoughColumns++;
  60. if (consecutiveTallEnoughColumns == side) return true;
  61. }
  62. else
  63. {
  64. consecutiveTallEnoughColumns = 0;
  65. }
  66. }
  67. return false;
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement