Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- namespace Wunderpahkina_vol6
- {
- class Program
- {
- static void Main(string[] args)
- {
- var lines = File.ReadAllLines("input.txt");
- var solver = new Solver(lines.Select(int.Parse).ToArray());
- var result = solver.Solve();
- Console.WriteLine($"Biggest square: {result} units x {result} units (Area: {result * result} units^2)");
- }
- }
- public class Solver
- {
- private readonly int[] _data;
- public Solver(int[] data)
- {
- _data = data;
- }
- public int Solve()
- {
- int rangeMin = 0;
- int rangeMax = _data.Length;
- while (rangeMin + 1 < rangeMax)
- {
- // go through the sizes of squares by dividing and conquering
- int pivot = rangeMin + (rangeMax - rangeMin) / 2;
- // can a square with with a side size like the "pivot" fit?
- var canFit = CanFit(pivot);
- Console.WriteLine($"Does a square with a side of {pivot} fit? {canFit}");
- if (canFit)
- {
- rangeMin = pivot;
- }
- else
- {
- rangeMax = pivot;
- }
- }
- return rangeMin;
- }
- private bool CanFit(int side)
- {
- var consecutiveTallEnoughColumns = 0;
- for (int index = 0; index < _data.Length; index++)
- {
- if (_data[index] >= side)
- {
- consecutiveTallEnoughColumns++;
- if (consecutiveTallEnoughColumns == side) return true;
- }
- else
- {
- consecutiveTallEnoughColumns = 0;
- }
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement