jarinel

dots task

Oct 24th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.25 KB | None | 0 0
  1. class Rectangle {
  2.     int x1
  3.     int y1
  4.     int x2
  5.     int y2
  6.  
  7.     Rectangle(int x1, int y1, int x2, int y2) {
  8.         this.x1 = x1
  9.         this.y1 = y1
  10.         this.x2 = x2
  11.         this.y2 = y2
  12.     }
  13.  
  14.     int width() {
  15.         return x2 - x1
  16.     }
  17.  
  18.     int height() {
  19.         return y2 - y1
  20.     }
  21.  
  22.     int area() {
  23.         return width() * height()
  24.     }
  25.  
  26.     @Override
  27.     String toString() {
  28.         return "Rect($x1; $y1; $x2; $y2)".toString()
  29.     }
  30. }
  31.  
  32. class Dot {
  33.     int x
  34.     int y
  35.  
  36.     Dot(int x, int y) {
  37.         this.x = x
  38.         this.y = y
  39.     }
  40.  
  41.     @Override
  42.     String toString() {
  43.         return "Dot($x; $y)".toString()
  44.     }
  45. }
  46.  
  47. class Pair<T> {
  48.     T first
  49.     T second
  50.  
  51.     private Pair(T first, T second) {
  52.         this.first = first
  53.         this.second = second
  54.     }
  55.  
  56.     static final <T> Pair<T> of(T first, T second) {
  57.         return new Pair<>(first, second)
  58.     }
  59.  
  60.  
  61.     @Override
  62.     String toString() {
  63.         return "Pair[$first, $second]".toString()
  64.     }
  65. }
  66.  
  67. List<List<Rectangle>> split(Rectangle rectangle, Integer area) {
  68.     if (area == rectangle.area()) {
  69.         return [[rectangle]]
  70.     }
  71.     if (rectangle.area() % area != 0) {
  72.         return []
  73.     }
  74.  
  75.     List<List<Rectangle>> result = []
  76.     for (Pair<Rectangle> pair : splitter(rectangle, area)) {
  77.         def first = split(pair.first, area)
  78.         def second = split(pair.second, area)
  79.  
  80.         for (List<Rectangle> firstsList : first) {
  81.             for (List<Rectangle> secondsList : second) {
  82.                 if (firstsList && secondsList) {
  83.                     result.add(firstsList + secondsList)
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.     return result
  90. }
  91.  
  92. List<Pair<Rectangle>> splitter(Rectangle r, Integer area) {
  93.     def result = new ArrayList<Pair<Rectangle>>()
  94.  
  95.     if (area % r.width() == 0 || r.width() % area == 0) {
  96.         Integer mult = (int) (area / r.width()) ?: 1
  97.  
  98.         for (int cur = mult + r.y1; cur < r.y2; cur += mult) {
  99.             result << Pair.of(new Rectangle(r.x1, r.y1, r.x2, cur), new Rectangle(r.x1, cur, r.x2, r.y2))
  100.         }
  101.     }
  102.  
  103.     if (area % r.height() == 0 || r.height() % area == 0) {
  104.         Integer mult = (int) (area / r.height()) ?: 1
  105.  
  106.         for (int cur = mult + r.x1; cur < r.x2; cur += mult) {
  107.             result << Pair.of(new Rectangle(r.x1, r.y1, cur, r.y2), new Rectangle(cur, r.y1, r.x2, r.y2))
  108.         }
  109.     }
  110.  
  111.     return result.unique()
  112. }
  113.  
  114. def solve(Rectangle base, List<Dot> dots) {
  115.     if (base.area() % dots.size() != 0) {
  116.         println('No solution')
  117.         return
  118.     }
  119.  
  120.     int targetArea = (int) (base.area() / dots.size())
  121.     def possibleSolutions = split(base, targetArea)
  122.     def solution = possibleSolutions.find { List<Rectangle> solution ->
  123.         def bad = solution.find { Rectangle rect ->
  124.             dots.findAll { Dot dot ->
  125.                 dot.x >= rect.x1 && dot.x < rect.x2 && dot.y >= rect.y1 && dot.y < rect.y2
  126.             }.size() != 1
  127.         }
  128.         return !bad
  129.     }
  130.     if (!solution) {
  131.         println('No solution')
  132.         return
  133.     }
  134.  
  135.     println solution
  136. }
  137.  
  138. def rect = new Rectangle(0, 0, 6, 8)
  139. def dots = [new Dot(0, 5), new Dot(3, 4), new Dot(5, 7), new Dot(0, 0)]
  140.  
  141. solve(rect, dots)
Advertisement
Add Comment
Please, Sign In to add comment