Skip to content

Instantly share code, notes, and snippets.

@fabienhinault
Created September 17, 2017 15:59
Show Gist options
  • Save fabienhinault/9137993c8da9bcb777f07dc237bba7a6 to your computer and use it in GitHub Desktop.
Save fabienhinault/9137993c8da9bcb777f07dc237bba7a6 to your computer and use it in GitHub Desktop.
lights challenge
/**
* Created by fab on 16/09/17.
*/
public abstract class AbstractBox {
abstract void turn(boolean b, int i, int i1, int i2, int i3);
abstract void toggle(int left, int bottom, int right, int top);
public void doStuffWithoutInverted(){
doStuffBeforeInverted();
doStuffAfterInverted();
}
public void doStuffWithInverted(){
doStuffBeforeInverted();
// inverted coords !!!!!
this.toggle(0,499, 500,399);
doStuffAfterInverted();
}
public void doStuffAfterInverted(){
this.turn(true, 50,950, 60,960);
this.turn(true, 170,950, 180,960);
this.turn(true, 290,950, 300,960);
this.turn(true, 410,950, 420,960);
this.toggle(50,930, 60,940);
this.toggle(170,930, 180,940);
this.toggle(290,930, 300,940);
this.toggle(410,930, 420,940);
this.turn(true, 50,910, 60,920);
this.turn(true, 170,910, 180,920);
this.turn(true, 290,910, 300,920);
this.turn(true, 410,910, 420,920);
this.turn(true, 50,890, 60,900);
this.turn(true, 170,890, 180,900);
this.turn(true, 290,890, 300,900);
this.turn(true, 410,890, 420,900);
this.turn(true, 40,880, 50,890);
this.turn(true, 160,880, 170,890);
this.turn(true, 280,880, 290,890);
this.turn(true, 400,880, 410,890);
this.turn(true, 30,870, 40,880);
this.turn(true, 150,870, 160,880);
this.turn(true, 270,870, 280,880);
this.turn(true, 390,870, 400,880);
this.turn(true, 30,930, 40,940);
this.turn(true, 150,930, 160,940);
this.turn(true, 270,930, 280,940);
this.turn(true, 390,930, 400,940);
this.turn(true, 60,880, 70,890);
this.turn(true, 180,880, 190,890);
this.turn(true, 300,880, 310,890);
this.turn(true, 420,880, 430,890);
this.turn(true, 70,870, 80,880);
this.turn(true, 190,870, 200,880);
this.turn(true, 310,870, 320,880);
this.turn(true, 430,870, 440,880);
this.turn(true, 50,870, 60,880);
this.turn(true, 170,870, 180,880);
this.turn(true, 290,870, 300,880);
this.turn(true, 410,870, 420,880);
this.turn(true, 70,930, 80,940);
this.turn(true, 190,930, 200,940);
this.turn(true, 310,930, 320,940);
this.turn(true, 430,930, 440,940);
this.turn(true, 110,850, 120,860);
this.turn(true, 230,850, 240,860);
this.turn(true, 350,850, 360,860);
this.turn(true, 110,830, 120,840);
this.turn(true, 230,830, 240,840);
this.turn(true, 350,830, 360,840);
this.turn(true, 110,810, 120,820);
this.turn(true, 230,810, 240,820);
this.turn(true, 350,810, 360,820);
this.turn(true, 110,790, 120,800);
this.turn(true, 230,790, 240,800);
this.turn(true, 350,790, 360,800);
this.turn(true, 100,780, 110,790);
this.turn(true, 220,780, 230,790);
this.turn(true, 340,780, 350,790);
this.turn(true, 90,770, 100,780);
this.turn(true, 210,770, 220,780);
this.turn(true, 330,770, 340,780);
this.turn(true, 120,780, 130,790);
this.turn(true, 240,780, 250,790);
this.turn(true, 360,780, 370,790);
this.turn(true, 130,770, 140,780);
this.turn(true, 250,770, 260,780);
this.turn(true, 370,770, 380,780);
this.turn(true, 110,770, 120,780);
this.turn(true, 230,770, 240,780);
this.turn(true, 350,770, 360,780);
this.turn(true, 90,830, 100,840);
this.turn(true, 210,830, 220,840);
this.turn(true, 330,830, 340,840);
this.turn(true, 130,830, 140,840);
this.turn(true, 250,830, 260,840);
this.turn(true, 370,830, 380,840);
this.turn(true, 50,750, 60,760);
this.turn(true, 170,750, 180,760);
this.turn(true, 290,750, 300,760);
this.turn(true, 410,750, 420,760);
this.turn(true, 50,730, 60,740);
this.turn(true, 170,730, 180,740);
this.turn(true, 290,730, 300,740);
this.turn(true, 410,730, 420,740);
this.turn(true, 50,710, 60,720);
this.turn(true, 170,710, 180,720);
this.turn(true, 290,710, 300,720);
this.turn(true, 410,710, 420,720);
this.turn(true, 50,690, 60,700);
this.turn(true, 170,690, 180,700);
this.turn(true, 290,690, 300,700);
this.turn(true, 410,690, 420,700);
this.turn(true, 40,680, 50,690);
this.turn(true, 160,680, 170,690);
this.turn(true, 280,680, 290,690);
this.turn(true, 400,680, 410,690);
this.turn(true, 30,670, 40,680);
this.turn(true, 150,670, 160,680);
this.turn(true, 270,670, 280,680);
this.turn(true, 390,670, 400,680);
this.turn(true, 60,680, 70,690);
this.turn(true, 180,680, 190,690);
this.turn(true, 300,680, 310,690);
this.turn(true, 420,680, 430,690);
this.turn(true, 70,670, 80,680);
this.turn(true, 190,670, 200,680);
this.turn(true, 310,670, 320,680);
this.turn(true, 430,670, 440,680);
this.turn(true, 50,670, 60,680);
this.turn(true, 170,670, 180,680);
this.turn(true, 290,670, 300,680);
this.turn(true, 410,670, 420,680);
this.turn(true, 30,730, 40,740);
this.turn(true, 150,730, 160,740);
this.turn(true, 270,730, 280,740);
this.turn(true, 390,730, 400,740);
this.turn(true, 70,730, 80,740);
this.turn(true, 190,730, 200,740);
this.turn(true, 310,730, 320,740);
this.turn(true, 430,730, 440,740);
}
public void doStuffBeforeInverted(){
this.toggle(461,550, 564,900);
this.turn(false,370,39, 425,839);
this.turn(false,464,858, 833,915);
this.turn(false,812,389, 865,874);
this.turn(true,599,989, 806,993);
this.turn(true,376,415, 768,548);
this.turn(true,606,361, 892,600);
this.turn(false, 448,208, 645,684);
this.toggle( 50,472, 452,788);
this.toggle( 205,417, 703,826);
this.toggle( 533,331, 906,873);
this.toggle( 857,493, 989,970);
this.turn(false, 631,950, 894,975);
this.turn(false, 387,19, 720,700);
this.turn(false, 511,843, 581,945);
this.toggle( 514,557, 662,883);
this.turn(false, 269,809, 876,847);
this.turn(false, 149,517, 716,777);
this.turn(false, 994,939, 998,988);
this.toggle( 467,662, 555,957);
this.turn(true, 952,417, 954,845);
this.turn(true, 565,226, 944,880);
this.turn(true, 214,319, 805,722);
this.toggle( 532,276, 636,847);
this.toggle( 619,80, 689,507);
this.turn(true, 390,706, 884,722);
this.toggle( 17,634, 537,766);
// 4 / 12
this.toggle(706,440, 834,441);
this.toggle(318,207, 499,530);
this.toggle(698,185, 830,343);
this.toggle(566,679, 744,716);
this.toggle(347,482, 959,482);
this.toggle(39,799, 981,872);
this.turn(true, 583,543, 846,710);
this.turn(false, 367,664, 595,872);
this.turn(true, 805,439, 964,995);
this.toggle(209,584, 513,802);
this.turn(false, 106,497, 266,770);
this.turn(true, 975,2, 984,623);
this.turn(false, 316,684, 369,876);
this.turn(false, 30,309, 259,554);
this.turn(false, 399,680, 861,942);
this.toggle(227,740, 850,829);
this.turn(true, 386,603, 552,879);
this.turn(false, 703,795, 791,963);
this.turn(false, 573,803, 996,878);
this.turn(false, 993,939, 997,951);
this.turn(true, 809,221, 869,723);
this.turn(false, 38,720, 682,751);
this.turn(false, 318,732, 720,976);
this.toggle(88,459, 392,654);
this.turn(false, 865,654, 911,956);
this.toggle(264,284, 857,956);
this.turn(false, 281,776, 610,797);
this.toggle(492,660, 647,910);
this.turn(false, 879,703, 925,981);
this.turn(false, 772,414, 974,518);
this.turn(true, 694,41, 755,96);
this.turn(true, 452,406, 885,881);
this.turn(false, 107,905, 497,910);
this.turn(false, 647,222, 910,532);
this.turn(true, 679,40, 845,358);
this.turn(false, 144,205, 556,362);
this.turn(true, 871,804, 962,878);
this.turn(true, 545,676, 545,929);
this.turn(false, 316,716, 413,941);
this.toggle(488,826, 755,971);
this.toggle(957,832, 976,992);
this.toggle(857,770, 905,964);
this.toggle(319,198, 787,673);
this.turn(true, 832,813, 863,844);
this.turn(true, 818,296, 818,681);
this.turn(true, 71,699, 91,960);
this.turn(false, 838,578, 967,928);
this.toggle(440,856, 507,942);
this.toggle(121,970, 151,974);
this.toggle(391,192, 659,751);
this.turn(true, 78,210, 681,419);
this.turn(true, 324,591, 593,939);
this.toggle(159,366, 249,760);
this.turn(false, 617,167, 954,601);
this.toggle(484,607, 733,657);
this.turn(true, 587,96, 888,819);
this.turn(false, 680,984, 941,991);
this.turn(true, 800,512, 968,691);
this.turn(false, 123,588, 853,603);
this.turn(true, 1,862, 507,912);
this.turn(true, 699,839, 973,878);
this.turn(false, 848,89, 887,893);
this.toggle(344,353, 462,403);
this.turn(true, 780,731, 841,760);
this.toggle(693,973, 847,984);
this.toggle(989,936, 996,958);
this.toggle(168,475, 206,963);
this.turn(true, 742,683, 769,845);
this.toggle(768,116, 987,396);
this.turn(true, 190,364, 617,526);
this.turn(false, 470,266, 530,839);
this.toggle(122,497, 969,645);
this.turn(false, 492,432, 827,790);
this.turn(true, 505,636, 957,820);
this.turn(true, 295,476, 698,958);
this.toggle(63,298, 202,396);
this.turn(true, 157,315, 412,939);
this.turn(false, 69,789, 134,837);
this.turn(false, 678,335, 896,541);
this.toggle(140,516, 842,668);
this.turn(false, 697,585, 712,668);
this.toggle(507,832, 578,949);
this.turn(true, 678,279, 886,621);
this.toggle(449,744, 826,910);
this.turn(false, 835,354, 921,741);
this.toggle(924,878, 985,952);
this.turn(true, 666,503, 922,905);
this.turn(true, 947,453, 961,587);
this.toggle(525,190, 795,654);
this.turn(false, 62,320, 896,362);
this.turn(true, 21,458, 972,536);
this.turn(true, 446,429, 821,970);
this.toggle(376,423, 805,455);
this.toggle(494,896, 715,937);
this.turn(true, 583,270, 667,482);
this.turn(false, 183,468, 280,548);
this.toggle(623,289, 750,524);
this.turn(true, 836,706, 967,768);
this.turn(true, 419,569, 912,908);
this.turn(true, 428,260, 660,433);
this.turn(false, 683,627, 916,816);
this.turn(true, 447,973, 866,980);
this.turn(true, 688,607, 938,990);
this.turn(true, 245,187, 597,405);
this.turn(false, 558,843, 841,942);
this.turn(false, 325,666, 713,834);
this.toggle(672,606, 814,935);
this.turn(false, 161,812, 490,954);
this.turn(true, 950,362, 985,898);
this.turn(true, 143,22, 205,821);
this.turn(true, 89,762, 607,790);
this.toggle(234,245, 827,303);
this.turn(true, 65,599, 764,997);
this.turn(true, 232,466, 965,695);
this.turn(true, 739,122, 975,590);
this.turn(false, 206,112, 940,558);
this.toggle(690,365, 988,552);
this.turn(true, 907,438, 977,691);
this.turn(false, 838,809, 944,869);
this.turn(true, 222,12, 541,832);
//
this.toggle(337,66, 669,812);
this.turn(true, 732,821, 897,912);
this.toggle(182,862, 638,996);
this.turn(true, 955,808, 983,847);
this.toggle(346,227, 841,696);
this.turn(true, 983,270, 989,756);
this.turn(false, 874,849, 876,905);
this.turn(false, 7,760, 678,795);
this.toggle(973,977, 995,983);
this.turn(false, 911,961, 914,976);
this.turn(true, 913,557, 952,722);
this.turn(false, 607,933, 939,999);
this.turn(true, 226,604, 517,622);
this.turn(false, 3,564, 344,842);
this.toggle(340,578, 428,610);
this.turn(true, 248,916, 687,925);
this.toggle(650,185, 955,965);
this.toggle(831,359, 933,536);
this.turn(false, 544,614, 896,953);
this.toggle(648,939, 975,997);
this.turn(true, 464,269, 710,521);
this.turn(false, 643,149, 791,320);
this.turn(false, 875,549, 972,643);
this.turn(false, 953,969, 971,972);
this.turn(false, 236,474, 772,591);
this.toggle(313,212, 489,723);
this.toggle(896,829, 897,837);
this.toggle(544,449, 995,905);
this.turn(false, 278,645, 977,876);
this.turn(false, 887,947, 946,977);
this.turn(true, 342,861, 725,935);
this.turn(true, 636,316, 692,513);
this.toggle(857,470, 950,528);
this.turn(false, 736,196, 826,889);
this.turn(true, 17,878, 850,987);
this.turn(true, 142,968, 169,987);
this.turn(true, 46,470, 912,853);
this.turn(true, 182,252, 279,941);
this.toggle(261,143, 969,657);
this.turn(false, 69,600, 518,710);
this.turn(true, 372,379, 779,386);
this.toggle(867,391, 911,601);
this.turn(false, 174,287, 900,536);
this.toggle(951,842, 993,963);
this.turn(false, 626,733, 985,827);
this.toggle(622,70, 666,291);
this.turn(false, 980,671, 985,835);
this.turn(false, 477,63, 910,72);
this.turn(false, 779,39, 940,142);
this.turn(true, 986,570, 997,638);
this.toggle(842,805, 943,985);
this.turn(false, 890,886, 976,927);
this.turn(false, 893,172, 897,619);
this.turn(false, 198,780, 835,826);
this.toggle(202,209, 219,291);
this.turn(false, 193,52, 833,283);
this.toggle(414,427, 987,972);
this.turn(true, 375,231, 668,236);
this.turn(false, 646,598, 869,663);
this.toggle(271,462, 414,650);
this.turn(false, 679,121, 845,467);
this.toggle(76,847, 504,904);
this.turn(false, 15,617, 509,810);
this.toggle(248,105, 312,451);
this.turn(false, 126,546, 922,879);
this.turn(true, 531,831, 903,872);
this.toggle(602,431, 892,792);
this.turn(false, 795,223, 892,623);
this.toggle(167,721, 533,929);
this.toggle(813,251, 998,484);
this.toggle(64,640, 752,942);
this.turn(true, 155,955, 892,985);
this.turn(true, 251,329, 996,497);
this.turn(false, 341,716, 462,994);
this.toggle(760,127, 829,189);
this.turn(true, 86,413, 408,518);
this.toggle(340,102, 918,558);
this.turn(false, 441,642, 751,889);
this.turn(true, 785,292, 845,325);
this.turn(false, 123,389, 725,828);
this.turn(true, 905,73, 983,270);
this.turn(false, 807,86, 879,276);
this.toggle(500,866, 864,916);
this.turn(true, 809,366, 828,534);
this.toggle(219,356, 720,617);
this.turn(false, 320,964, 769,990);
this.turn(false, 903,167, 936,631);
this.toggle(300,137, 333,693);
this.toggle(5,675, 755,848);
this.turn(false, 852,235, 946,783);
this.toggle(355,556, 941,664);
this.turn(true, 810,830, 867,891);
this.turn(false, 509,869, 667,903);
this.toggle(769,400, 873,892);
this.turn(true, 553,614, 810,729);
this.turn(true, 179,873, 589,962);
this.turn(false, 466,866, 768,926);
this.toggle(143,943, 465,984);
this.toggle(182,380, 569,552);
this.turn(false, 735,808, 917,910);
this.turn(true, 731,802, 910,847);
this.turn(false, 522,74, 731,485);
this.turn(true, 444,127, 566,996);
this.turn(false, 232,962, 893,979);
this.turn(false, 231,492, 790,976);
this.turn(true, 874,567, 943,684);
this.toggle(911,840, 990,932);
this.toggle(547,895, 667,935);
this.turn(false, 93,294, 648,636);
this.turn(false, 190,902, 532,970);
this.turn(false, 451,530, 704,613);
this.toggle(936,774, 937,775);
this.turn(false, 116,843, 533,934);
this.turn(true, 950,906, 986,993);
this.turn(true, 910,51, 945,989);
this.turn(true, 986,498, 994,945);
this.turn(false, 125,324, 433,704);
this.turn(false, 60,313, 75,728);
this.turn(true, 899,494, 940,947);
this.toggle(832,316, 971,817);
this.toggle(994,983, 998,984);
this.toggle(23,353, 917,845);
this.toggle(174,799, 658,859);
this.turn(false, 490,878, 534,887);
this.turn(false, 623,963, 917,975);
this.toggle(721,333, 816,975);
this.toggle(589,687, 890,921);
this.turn(true, 936,388, 948,560);
this.turn(false, 485,17, 655,610);
this.turn(true, 435,158, 689,495);
this.turn(true, 192,934, 734,936);
this.turn(false, 299,723, 622,847);
this.toggle(484,160, 812,942);
this.turn(false, 245,754, 818,851);
this.turn(true, 298,419, 824,634);
this.toggle(868,687, 969,760);
this.toggle(131,250, 685,426);
this.turn(false, 201,954, 997,983);
this.turn(true, 353,910, 832,961);
this.turn(false, 518,781, 645,875);
this.turn(false, 866,97, 924,784);
this.toggle(836,599, 857,767);
this.turn(true, 80,957, 776,968);
this.toggle(277,130, 513,244);
this.turn(false, 62,266, 854,434);
this.turn(true, 792,764, 872,842);
this.turn(false, 160,949, 273,989);
this.turn(false, 664,203, 694,754);
this.toggle(491,615, 998,836);
this.turn(false, 210,146, 221,482);
this.turn(false, 209,780, 572,894);
this.turn(true, 766,112, 792,868);
this.turn(true, 222,12, 856,241);
this.turn(false, 0,0, 999,999);
this.turn(true, 0,100, 999,199);
this.turn(true, 0,300, 999,399);
this.turn(true, 0,500, 999,599);
this.turn(true, 500,700, 999,799);
this.turn(true, 500,900, 999,999);
this.turn(true, 500,900, 999,999);
}
}
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Box extends AbstractBox{
public static void main(String [ ] args) {
Box bigBox = new Box(0, 0, 999, 999, false, new SubBoxes());
bigBox.doStuffWithInverted();
System.out.print(bigBox.countLit(true));
// 464174
}
@Override
void turn(boolean _on, int left, int bottom, int right, int top) {
Box box = createBox(left, bottom, right, top, _on);
subBoxes.splitX(box.left, box);
subBoxes.splitX(box.right + 1, box);
subBoxes.splitY(box.bottom, box);
subBoxes.splitY(box.top + 1, box);
this.getInsides(box).map(Map.Entry<Corner, Box>::getKey)
.collect(Collectors.toList()).stream()
.forEach(corner -> subBoxes.remove(corner));
if (_on != on) {
addToSubBoxes(box);
}
}
protected Box createBox(int left, int bottom, int right, int top, boolean _on) {
return createBox(left, bottom, right, top, _on, new SubBoxes());
}
protected Box createBox(int left, int bottom, int right, int top, boolean _on, SubBoxes subBoxes) {
return new Box(left, bottom, right, top, _on, subBoxes);
}
protected void addToSubBoxes(Box box) {
subBoxes.add(box);
}
@Override
void toggle(int left, int bottom, int right, int top) {
Box box = createBox(left, bottom, right, top, !on);
subBoxes.splitX(box.left, box);
subBoxes.splitX(box.right + 1, box);
subBoxes.splitY(box.bottom, box);
subBoxes.splitY(box.top + 1, box);
this.getInsides(box).map(Map.Entry<Corner, Box>::getValue)
.collect(Collectors.toList()).stream()
.forEach(inside -> passTo(inside, box));
addToSubBoxes(box);
}
static class Corner implements Comparable<Corner> {
Integer left;
Integer bottom;
public Corner(Integer left, Integer bottom) {
this.left = left;
this.bottom = bottom;
}
@Override
public int compareTo(Corner o) {
int lefts = this.left.compareTo(o.left);
if (lefts != 0) {
return lefts;
} else {
return this.bottom.compareTo(o.bottom);
}
}
}
public static class SubBoxes extends TreeMap<Corner, Box>{
void add(Box box){
this.put(box.getLeftBottom(), box);
}
void addAll(List<Box> boxes){
for (Box box : boxes) {
this.add(box);
}
}
SubBoxes cutLeft(int x){
SubBoxes result = new SubBoxes();
for(Box box : values()){
if (box.left < x) {
result.add(box.cutLeft(x));
}
}
return result;
}
SubBoxes cutRight(int x){
SubBoxes result = new SubBoxes();
for(Box box : values()){
if (box.right >= x) {
result.add(box.cutRight(x));
}
}
return result;
}
SubBoxes cutDown(int y){
SubBoxes result = new SubBoxes();
for(Box box : values()){
if (box.bottom < y) {
result.add(box.cutDown(y));
}
}
return result;
}
SubBoxes cutUp(int y){
SubBoxes result = new SubBoxes();
for(Box box : values()){
if (box.top >= y) {
result.add(box.cutUp(y));
}
}
return result;
}
void splitX(int x, Box newBox) {
ArrayList<Box> toAdd = new ArrayList<>();
Iterator<Map.Entry<Corner, Box>> iterator = this.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Corner, Box> entry = iterator.next();
Box box = entry.getValue();
if (newBox.isOverlapping(box)) {
if (box.left < x && x <= box.right) {
iterator.remove();
toAdd.addAll(box.splitX(x));
}
}
}
addAll(toAdd);
}
void splitY(int y, Box newBox) {
ArrayList<Box> toAdd = new ArrayList<>();
Iterator<Map.Entry<Corner, Box>> iterator = this.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Corner, Box> entry = iterator.next();
Box box = entry.getValue();
if (newBox.isOverlapping(box)) {
if (box.bottom < y && y <= box.top) {
iterator.remove();
toAdd.addAll(box.splitY(y));
}
}
}
addAll(toAdd);
}
}
int left;
int bottom;
int right;
int top;
boolean on;
SubBoxes subBoxes;
public Box() {
left = 0;
bottom = 0;
right = 999;
top = 999;
subBoxes = new SubBoxes();
}
public Box(int left, int bottom, int right, int top, boolean on, SubBoxes subBoxes) {
int tmp;
if (right < left) {
tmp = left;
left = right;
right = tmp;
}
if (top < bottom) {
tmp = bottom;
bottom = top;
top = tmp;
}
this.left = left;
this.bottom = bottom;
this.right = right;
this.top = top;
this.on = on;
this.subBoxes = subBoxes;
}
Box cutLeft(int x) {
if (this.right < x) {
return this;
} else {
return createBox(left, bottom, x - 1, top, on, subBoxes.cutLeft(x));
}
}
Box cutRight(int x) {
if (this.left >= x) {
return this;
} else {
return createBox(x, bottom, right, top, on, subBoxes.cutRight(x));
}
}
Box cutDown(int y) {
if (this.top < y) {
return this;
} else {
return createBox(left, bottom, right, y - 1, on, subBoxes.cutDown(y));
}
}
Box cutUp(int y) {
if (this.bottom >= y) {
return this;
} else {
return createBox(left, y, right, top, on, subBoxes.cutUp(y));
}
}
public static int area(int left, int bottom, int right, int top){
return ((top - bottom + 1) * (right - left + 1));
}
int countLit(boolean _on) {
if( _on == on ) {
int sum = 0;
for (Box sub : subBoxes.values()) {
sum += sub.countLit(!_on);
}
return area(left, bottom, right, top) - sum;
} else {
int sum = 0;
for (Box sub : subBoxes.values()) {
sum += sub.countLit(_on);
}
return sum;
}
}
void toggle() {
on = !on;
subBoxes.values().stream().forEach(Box::toggle);
}
void passTo(Box toPass, Box receiver) {
toPass.toggle();
receiver.addToSubBoxes(toPass);
subBoxes.remove(toPass.getLeftBottom());
}
Corner getLeftBottom() {
return new Corner(left, bottom);
}
Corner getRightTop() {
return new Corner(right, top);
}
Stream<Map.Entry<Corner, Box>> getInsides(Box box) {
return subBoxes.entrySet().stream().filter((e) -> e.getValue().isInside(box));
}
boolean isInside(Box box) {
return (box.left <= this.left) &&
(box.bottom <= this.bottom) &&
(box.right >= this.right) &&
(box.top >= this.top);
}
static boolean isOverlapping1d(int l1, int r1, int l2, int r2) {
return (r1 >= l2) && (l1 <= r2);
}
boolean isOverlapping(Box box) {
return isOverlapping1d(this.left, this.right, box.left, box.right) &&
isOverlapping1d(this.bottom, this.top, box.bottom, box.top);
}
// x will be in the right box
ArrayList<Box> splitX(int x) {
ArrayList<Box> result = new ArrayList<>();
if ((x > this.left) && (x <= this.right)) {
result.add(cutLeft(x));
result.add(cutRight(x));
} else {
result.add(this);
}
return result;
}
// y will be in the upper box
ArrayList<Box> splitY(int y) {
ArrayList<Box> result = new ArrayList<>();
if ((y > this.bottom) && (y <= this.top)) {
result.add(cutUp(y));
result.add(cutDown(y));
} else {
result.add(this);
}
return result;
}
}
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created by fab on 16/09/17.
*/
public class BoxTest {
@Test
public void test() {
}
@Test
public void testFinalWithoutInverted(){
Box bigBox = new Box(0,0, 999, 999, false, new Box.SubBoxes());
bigBox.doStuffWithoutInverted();
Assert.assertEquals(414575, bigBox.countLit(true));
}
@Test
public void testFinalBruteForceWithoutInverted(){
BruteForce bruteForce = new BruteForce();
bruteForce.doStuffWithoutInverted();
Assert.assertEquals(414575, bruteForce.countLit(true));
}
@Test
public void testFinalWithInverted(){
BruteForce bruteForce = new BruteForce();
bruteForce.doStuffWithInverted();
Box bigBox = new Box(0,0, 999, 999, false, new Box.SubBoxes());
bigBox.doStuffWithInverted();
Assert.assertEquals(bruteForce.countLit(true) , bigBox.countLit(true));
}
@Test
public void testTestingBox(){
TestingBox testingBox = new TestingBox(0, 0, 999, 999, false, new Box.SubBoxes());
testingBox.doStuffWithInverted();
}
@Test
public void testSplitXRight(){
Box box = new Box(637, 723, 805, 798, true, new Box.SubBoxes());
ArrayList<Box> splitResult = box.splitX(805);
Assert.assertEquals(2, splitResult.size());
}
@Test
public void testSplitXLeft(){
Box box = new Box(637, 723, 805, 798, true, new Box.SubBoxes());
ArrayList<Box> splitResult = box.splitX(637);
Assert.assertEquals(1, splitResult.size());
}
@Test
public void testSplitYBottom(){
Box box = new Box(637, 723, 805, 798, true, new Box.SubBoxes());
ArrayList<Box> splitResult = box.splitY(723);
Assert.assertEquals(1, splitResult.size());
}
@Test
public void testSplitYTop(){
Box box = new Box(637, 723, 805, 798, true, new Box.SubBoxes());
ArrayList<Box> splitResult = box.splitY(798);
Assert.assertEquals(2, splitResult.size());
}
@Test
public void testGetInsides(){
Box pixel = new Box(50, 890, 50, 890, true, new Box.SubBoxes());
Box.SubBoxes subBoxes = new Box.SubBoxes();
subBoxes.add(pixel);
Box bigBox = new Box(0, 999, 0, 999, false, subBoxes);
Box newBox = new Box(50, 890, 60, 900, true, new Box.SubBoxes());
List<Map.Entry<Box.Corner, Box>> insides = bigBox.getInsides(newBox).collect(Collectors.toList());
Assert.assertEquals(1, insides.size());
}
@Test
public void testGetInsides2(){
Box pixel = new Box(50, 890, 50, 890, true, new Box.SubBoxes());
Box.SubBoxes subBoxes = new Box.SubBoxes();
subBoxes.add(pixel);
Box bigBox = new Box(0, 999, 0, 999, false, subBoxes);
Box newBox = new Box(40, 880, 50, 890, true, new Box.SubBoxes());
List<Map.Entry<Box.Corner, Box>> insides = bigBox.getInsides(newBox).collect(Collectors.toList());
Assert.assertEquals(1, insides.size());
}
@Test
public void test343(){
TestingBox testingBox = new TestingBox(0, 0, 999, 999, false, new Box.SubBoxes());
testingBox.superToggle(461,550, 564,900);
testingBox.superTurn(false,370,39, 425,839);
testingBox.superTurn(false,464,858, 833,915);
testingBox.superTurn(false,812,389, 865,874);
testingBox.superTurn(true,599,989, 806,993);
testingBox.superTurn(true,376,415, 768,548);
testingBox.superTurn(true,606,361, 892,600);
testingBox.superTurn(false, 448,208, 645,684);
testingBox.superToggle( 50,472, 452,788);
testingBox.superToggle( 205,417, 703,826);
testingBox.superToggle( 533,331, 906,873);
testingBox.superToggle( 857,493, 989,970);
testingBox.superTurn(false, 631,950, 894,975);
testingBox.superTurn(false, 387, 19, 720,700);
testingBox.superTurn(false, 511,843, 581,945);
testingBox.superToggle( 514,557, 662,883);
testingBox.superTurn(false, 269,809, 876,847);
testingBox.superTurn(false, 149,517, 716,777);
testingBox.superTurn(false, 994,939, 998,988);
testingBox.superToggle( 467,662, 555,957);
testingBox.superTurn(true, 952,417, 954,845);
testingBox.superTurn(true, 565,226, 944,880);
testingBox.superTurn(true, 214,319, 805,722);
testingBox.superToggle( 532,276, 636,847);
testingBox.superToggle( 619,80, 689,507);
testingBox.superTurn(true, 390,706, 884,722);
testingBox.superToggle( 17,634, 537,766);
//
testingBox.superToggle(706,440, 834,441);
testingBox.superToggle(318,207, 499,530);
testingBox.superToggle(698,185, 830,343);
testingBox.superToggle(566,679, 744,716);
testingBox.superToggle(347,482, 959,482);
testingBox.superToggle(39,799, 981,872);
testingBox.superTurn(true, 583,543, 846,710);
testingBox.superTurn(false, 367,664, 595,872);
testingBox.superTurn(true, 805,439, 964,995);
testingBox.superToggle(209,584, 513,802);
testingBox.superTurn(false, 106,497, 266,770);
testingBox.superTurn(true, 975,2, 984,623);
testingBox.superTurn(false, 316,684, 369,876);
testingBox.superTurn(false, 30,309, 259,554);
testingBox.superTurn(false, 399,680, 861,942);
testingBox.superToggle(227,740, 850,829);
testingBox.superTurn(true, 386,603, 552,879);
testingBox.superTurn(false, 703,795, 791,963);
testingBox.superTurn(false, 573,803, 996,878);
testingBox.superTurn(false, 993,939, 997,951);
testingBox.superTurn(true, 809,221, 869,723);
testingBox.superTurn(false, 38,720, 682,751);
testingBox.superTurn(false, 318,732, 720,976);
testingBox.superToggle(88,459, 392,654);
testingBox.superTurn(false, 865,654, 911,956);
testingBox.superToggle(264,284, 857,956);
testingBox.superTurn(false, 281,776, 610,797);
testingBox.superToggle(492,660, 647,910);
testingBox.superTurn(false, 879,703, 925,981);
testingBox.superTurn(false, 772,414, 974,518);
testingBox.superTurn(true, 694,41, 755,96);
testingBox.superTurn(true, 452,406, 885,881);
testingBox.superTurn(false, 107,905, 497,910);
testingBox.superTurn(false, 647,222, 910,532);
testingBox.superTurn(true, 679,40, 845,358);
testingBox.superTurn(false, 144,205, 556,362);
testingBox.superTurn(true, 871,804, 962,878);
testingBox.superTurn(true, 545,676, 545,929);
testingBox.superTurn(false, 316,716, 413,941);
testingBox.superToggle(488,826, 755,971);
testingBox.superToggle(957,832, 976,992);
testingBox.superToggle(857,770, 905,964);
testingBox.superToggle(319,198, 787,673);
testingBox.superTurn(true, 832,813, 863,844);
testingBox.superTurn(true, 818,296, 818,681);
testingBox.superTurn(true, 71,699, 91,960);
testingBox.superTurn(false, 838,578, 967,928);
testingBox.superToggle(440,856, 507,942);
testingBox.superToggle(121,970, 151,974);
testingBox.superToggle(391,192, 659,751);
testingBox.superTurn(true, 78,210, 681,419);
testingBox.superTurn(true, 324,591, 593,939);
testingBox.superToggle(159,366, 249,760);
testingBox.superTurn(false, 617,167, 954,601);
testingBox.superToggle(484,607, 733,657);
testingBox.superTurn(true, 587,96, 888,819);
testingBox.superTurn(false, 680,984, 941,991);
testingBox.superTurn(true, 800,512, 968,691);
testingBox.superTurn(false, 123,588, 853,603);
testingBox.superTurn(true, 1,862, 507,912);
testingBox.superTurn(true, 699,839, 973,878);
testingBox.superTurn(false, 848,89, 887,893);
testingBox.superToggle(344,353, 462,403);
testingBox.superTurn(true, 780,731, 841,760);
testingBox.superToggle(693,973, 847,984);
testingBox.superToggle(989,936, 996,958);
testingBox.superToggle(168,475, 206,963);
testingBox.superTurn(true, 742,683, 769,845);
testingBox.superToggle(768,116, 987,396);
testingBox.superTurn(true, 190,364, 617,526);
testingBox.superTurn(false, 470,266, 530,839);
testingBox.superToggle(122,497, 969,645);
testingBox.superTurn(false, 492,432, 827,790);
testingBox.superTurn(true, 505,636, 957,820);
testingBox.superTurn(true, 295,476, 698,958);
testingBox.superToggle(63,298, 202,396);
testingBox.superTurn(true, 157,315, 412,939);
testingBox.superTurn(false, 69,789, 134,837);
testingBox.superTurn(false, 678,335, 896,541);
testingBox.superToggle(140,516, 842,668);
testingBox.superTurn(false, 697,585, 712,668);
testingBox.superToggle(507,832, 578,949);
testingBox.superTurn(true, 678,279, 886,621);
testingBox.superToggle(449,744, 826,910);
testingBox.superTurn(false, 835,354, 921,741);
testingBox.superToggle(924,878, 985,952);
testingBox.superTurn(true, 666,503, 922,905);
testingBox.superTurn(true, 947,453, 961,587);
testingBox.superToggle(525,190, 795,654);
testingBox.superTurn(false, 62,320, 896,362);
testingBox.superTurn(true, 21,458, 972,536);
testingBox.superTurn(true, 446,429, 821,970);
testingBox.superToggle(376,423, 805,455);
testingBox.superToggle(494,896, 715,937);
testingBox.superTurn(true, 583,270, 667,482);
testingBox.superTurn(false, 183,468, 280,548);
testingBox.superToggle(623,289, 750,524);
testingBox.superTurn(true, 836,706, 967,768);
testingBox.superTurn(true, 419,569, 912,908);
testingBox.superTurn(true, 428,260, 660,433);
testingBox.superTurn(false, 683,627, 916,816);
testingBox.superTurn(true, 447,973, 866,980);
testingBox.superTurn(true, 688,607, 938,990);
testingBox.superTurn(true, 245,187, 597,405);
testingBox.superTurn(false, 558,843, 841,942);
testingBox.superTurn(false, 325,666, 713,834);
testingBox.superToggle(672,606, 814,935);
testingBox.superTurn(false, 161,812, 490,954);
testingBox.superTurn(true, 950,362, 985,898);
testingBox.superTurn(true, 143,22, 205,821);
testingBox.superTurn(true, 89,762, 607,790);
testingBox.superToggle(234,245, 827,303);
testingBox.superTurn(true, 65,599, 764,997);
testingBox.superTurn(true, 232,466, 965,695);
testingBox.superTurn(true, 739,122, 975,590);
testingBox.superTurn(false, 206,112, 940,558);
testingBox.superToggle(690,365, 988,552);
testingBox.superTurn(true, 907,438, 977,691);
testingBox.superTurn(false, 838,809, 944,869);
testingBox.superTurn(true, 222,12, 541,832);
//
testingBox.superToggle(337,66, 669,812);
testingBox.superTurn(true, 732,821, 897,912);
testingBox.superToggle(182,862, 638,996);
testingBox.superTurn(true, 955,808, 983,847);
testingBox.superToggle(346,227, 841,696);
testingBox.superTurn(true, 983,270, 989,756);
testingBox.superTurn(false, 874,849, 876,905);
testingBox.superTurn(false, 7,760, 678,795);
testingBox.superToggle(973,977, 995,983);
testingBox.superTurn(false, 911,961, 914,976);
testingBox.superTurn(true, 913,557, 952,722);
testingBox.superTurn(false, 607,933, 939,999);
testingBox.superTurn(true, 226,604, 517,622);
testingBox.superTurn(false, 3,564, 344,842);
testingBox.superToggle(340,578, 428,610);
testingBox.superTurn(true, 248,916, 687,925);
testingBox.superToggle(650,185, 955,965);
testingBox.superToggle(831,359, 933,536);
testingBox.superTurn(false, 544,614, 896,953);
testingBox.superToggle(648,939, 975,997);
testingBox.superTurn(true, 464,269, 710,521);
testingBox.superTurn(false, 643,149, 791,320);
testingBox.superTurn(false, 875,549, 972,643);
testingBox.superTurn(false, 953,969, 971,972);
testingBox.superTurn(false, 236,474, 772,591);
testingBox.superToggle(313,212, 489,723);
testingBox.superToggle(896,829, 897,837);
testingBox.superToggle(544,449, 995,905);
testingBox.superTurn(false, 278,645, 977,876);
testingBox.superTurn(false, 887,947, 946,977);
testingBox.superTurn(true, 342,861, 725,935);
testingBox.superTurn(true, 636,316, 692,513);
testingBox.superToggle(857,470, 950,528);
testingBox.superTurn(false, 736,196, 826,889);
testingBox.superTurn(true, 17,878, 850,987);
testingBox.superTurn(true, 142,968, 169,987);
testingBox.superTurn(true, 46,470, 912,853);
testingBox.superTurn(true, 182,252, 279,941);
testingBox.superToggle(261,143, 969,657);
testingBox.superTurn(false, 69,600, 518,710);
testingBox.superTurn(true, 372,379, 779,386);
testingBox.superToggle(867,391, 911,601);
testingBox.superTurn(false, 174,287, 900,536);
testingBox.superToggle(951,842, 993,963);
testingBox.superTurn(false, 626,733, 985,827);
testingBox.superToggle(622,70, 666,291);
testingBox.superTurn(false, 980,671, 985,835);
testingBox.superTurn(false, 477,63, 910,72);
testingBox.superTurn(false, 779,39, 940,142);
testingBox.superTurn(true, 986,570, 997,638);
testingBox.superToggle(842,805, 943,985);
testingBox.superTurn(false, 890,886, 976,927);
testingBox.superTurn(false, 893,172, 897,619);
testingBox.superTurn(false, 198,780, 835,826);
testingBox.superToggle(202,209, 219,291);
testingBox.superTurn(false, 193,52, 833,283);
testingBox.superToggle(414,427, 987,972);
testingBox.superTurn(true, 375,231, 668,236);
testingBox.superTurn(false, 646,598, 869,663);
testingBox.superToggle(271,462, 414,650);
testingBox.superTurn(false, 679,121, 845,467);
testingBox.superToggle(76,847, 504,904);
testingBox.superTurn(false, 15,617, 509,810);
testingBox.superToggle(248,105, 312,451);
testingBox.superTurn(false, 126,546, 922,879);
testingBox.superTurn(true, 531,831, 903,872);
testingBox.superToggle(602,431, 892,792);
testingBox.superTurn(false, 795,223, 892,623);
testingBox.superToggle(167,721, 533,929);
testingBox.superToggle(813,251, 998,484);
testingBox.superToggle(64,640, 752,942);
testingBox.superTurn(true, 155,955, 892,985);
testingBox.superTurn(true, 251,329, 996,497);
testingBox.superTurn(false, 341,716, 462,994);
testingBox.superToggle(760,127, 829,189);
testingBox.superTurn(true, 86,413, 408,518);
testingBox.superToggle(340,102, 918,558);
testingBox.superTurn(false, 441,642, 751,889);
testingBox.superTurn(true, 785,292, 845,325);
testingBox.superTurn(false, 123,389, 725,828);
testingBox.superTurn(true, 905,73, 983,270);
testingBox.superTurn(false, 807,86, 879,276);
testingBox.superToggle(500,866, 864,916);
testingBox.superTurn(true, 809,366, 828,534);
testingBox.superToggle(219,356, 720,617);
testingBox.superTurn(false, 320,964, 769,990);
testingBox.superTurn(false, 903,167, 936,631);
testingBox.superToggle(300,137, 333,693);
testingBox.superToggle(5,675, 755,848);
testingBox.superTurn(false, 852,235, 946,783);
testingBox.superToggle(355,556, 941,664);
testingBox.superTurn(true, 810,830, 867,891);
testingBox.superTurn(false, 509,869, 667,903);
testingBox.superToggle(769,400, 873,892);
testingBox.superTurn(true, 553,614, 810,729);
testingBox.superTurn(true, 179,873, 589,962);
testingBox.superTurn(false, 466,866, 768,926);
testingBox.superToggle(143,943, 465,984);
testingBox.superToggle(182,380, 569,552);
testingBox.superTurn(false, 735,808, 917,910);
testingBox.superTurn(true, 731,802, 910,847);
testingBox.superTurn(false, 522,74, 731,485);
testingBox.superTurn(true, 444,127, 566,996);
testingBox.superTurn(false, 232,962, 893,979);
testingBox.superTurn(false, 231,492, 790,976);
testingBox.superTurn(true, 874,567, 943,684);
testingBox.superToggle(911,840, 990,932);
testingBox.superToggle(547,895, 667,935);
testingBox.superTurn(false, 93,294, 648,636);
testingBox.superTurn(false, 190,902, 532,970);
testingBox.superTurn(false, 451,530, 704,613);
testingBox.superToggle(936,774, 937,775);
testingBox.superTurn(false, 116,843, 533,934);
testingBox.superTurn(true, 950,906, 986,993);
testingBox.superTurn(true, 910,51, 945,989);
testingBox.superTurn(true, 986,498, 994,945);
testingBox.superTurn(false, 125,324, 433,704);
testingBox.superTurn(false, 60,313, 75,728);
testingBox.superTurn(true, 899,494, 940,947);
testingBox.superToggle(832,316, 971,817);
testingBox.superToggle(994,983, 998,984);
testingBox.superToggle(23,353, 917,845);
testingBox.superToggle(174,799, 658,859);
testingBox.superTurn(false, 490,878, 534,887);
testingBox.superTurn(false, 623,963, 917,975);
testingBox.superToggle(721,333, 816,975);
testingBox.superToggle(589,687, 890,921);
testingBox.superTurn(true, 936,388, 948,560);
testingBox.superTurn(false, 485,17, 655,610);
testingBox.superTurn(true, 435,158, 689,495);
testingBox.superTurn(true, 192,934, 734,936);
testingBox.superTurn(false, 299,723, 622,847);
testingBox.superToggle(484,160, 812,942);
testingBox.superTurn(false, 245,754, 818,851);
testingBox.superTurn(true, 298,419, 824,634);
testingBox.superToggle(868,687, 969,760);
testingBox.superToggle(131,250, 685,426);
testingBox.superTurn(false, 201,954, 997,983);
testingBox.superTurn(true, 353,910, 832,961);
testingBox.superTurn(false, 518,781, 645,875);
testingBox.superTurn(false, 866,97, 924,784);
testingBox.superToggle(836,599, 857,767);
testingBox.superTurn(true, 80,957, 776,968);
testingBox.superToggle(277,130, 513,244);
testingBox.superTurn(false, 62,266, 854,434);
testingBox.superTurn(true, 792,764, 872,842);
testingBox.superTurn(false, 160,949, 273,989);
testingBox.superTurn(false, 664,203, 694,754);
testingBox.superToggle(491,615, 998,836);
testingBox.superTurn(false, 210,146, 221,482);
testingBox.superTurn(false, 209,780, 572,894);
testingBox.superTurn(true, 766,112, 792,868);
testingBox.superTurn(true, 222,12, 856,241);
testingBox.superTurn(false, 0,0, 999,999);
testingBox.superTurn(true, 0,100, 999,199);
testingBox.superTurn(true, 0,300, 999,399);
testingBox.superTurn(true, 0,500, 999,599);
testingBox.superTurn(true, 500,700, 999,799);
testingBox.superTurn(true, 500,900, 999,999);
testingBox.superTurn(true, 500,900, 999,999);
testingBox.superToggle(0,499, 500,399);
testingBox.superTurn(true, 50,950, 60,960);
testingBox.superTurn(true, 170,950, 180,960);
testingBox.superTurn(true, 290,950, 300,960);
testingBox.superTurn(true, 410,950, 420,960);
testingBox.superToggle(50,930, 60,940);
testingBox.superToggle(170,930, 180,940);
testingBox.superToggle(290,930, 300,940);
testingBox.superToggle(410,930, 420,940);
testingBox.superTurn(true, 50,910, 60,920);
testingBox.superTurn(true, 170,910, 180,920);
testingBox.superTurn(true, 290,910, 300,920);
testingBox.superTurn(true, 410,910, 420,920);
testingBox.superTurn(true, 50,890, 60,900);
testingBox.superTurn(true, 170,890, 180,900);
testingBox.superTurn(true, 290,890, 300,900);
testingBox.superTurn(true, 410,890, 420,900);
testingBox.turn(true, 40,880, 50,890);
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by fab on 16/09/17.
*/
public class BruteForce extends AbstractBox{
ArrayList<List<Boolean>> lights = new ArrayList<>();
public BruteForce(){
for(int i = 0; i < 1000; i++) {
Boolean[] row = new Boolean[1000];
Arrays.fill(row, new Boolean(false));
lights.add(Arrays.asList(row));
}
}
@Override
public void turn(boolean _on, int left, int bottom, int right, int top) {
int tmp;
if (right < left) {
tmp = left;
left = right;
right = tmp;
}
if (top < bottom) {
tmp = bottom;
bottom = top;
top = tmp;
}
for (int yy = bottom; yy <= top; yy++){
for (int xx = left; xx <= right; xx++) {
lights.get(yy).set(xx, _on);
}
}
}
@Override
public void toggle(int left, int bottom, int right, int top) {
int tmp;
if (right < left) {
tmp = left;
left = right;
right = tmp;
}
if (top < bottom) {
tmp = bottom;
bottom = top;
top = tmp;
}
for (int yy = bottom; yy <= top; yy++) {
List<Boolean> row = lights.get(yy);
for (int xx = left; xx <= right; xx++) {
row.set(xx, !row.get(xx));
}
}
}
public int countLit(boolean _on) {
return countLit(_on, 0, 0, 999, 999);
}
public int countLit(boolean _on, int left, int bottom, int right, int top) {
int result = 0;
for (int yy = bottom; yy <= top; yy++) {
List<Boolean> row = lights.get(yy);
for (int xx = left; xx <= right; xx++) {
if(row.get(xx) == _on){
result++;
}
}
}
return result;
}
}
import org.junit.Assert;
import java.util.Map;
/**
* Created by fab on 16/09/17.
*/
public class TestingBox extends Box {
BruteForce bruteForce = new BruteForce();
public TestingBox(int left, int bottom, int right, int top, boolean on, SubBoxes subBoxes) {
super(left, bottom, right, top, on, subBoxes);
}
@Override
protected Box createBox(int left, int bottom, int right, int top, boolean _on, SubBoxes subBoxes) {
TestingBox box = new TestingBox(left, bottom, right, top, _on, subBoxes);
box.bruteForce = bruteForce;
return box;
}
@Override
void turn(boolean _on, int left, int bottom, int right, int top) {
super.turn(_on, left, bottom, right, top);
bruteForce.turn(_on, left, bottom, right, top);
Assert.assertEquals(bruteForce.countLit(true), this.countLit(true));
}
void superTurn(boolean _on, int left, int bottom, int right, int top) {
super.turn(_on, left, bottom, right, top);
bruteForce.turn(_on, left, bottom, right, top);
}
@Override
void toggle(int left, int bottom, int right, int top) {
super.toggle(left, bottom, right, top);
bruteForce.toggle(left, bottom, right, top);
Assert.assertEquals(bruteForce.countLit(true), this.countLit(true));
}
void superToggle(int left, int bottom, int right, int top) {
super.toggle(left, bottom, right, top);
bruteForce.toggle(left, bottom, right, top);
}
@Override
int countLit(boolean _on) {
int result;
if( _on == on ) {
int sum = 0;
for (Box sub : subBoxes.values()) {
sum += sub.countLit(!_on);
}
result = area(left, bottom, right, top) - sum;
} else {
int sum = 0;
for (Box sub : subBoxes.values()) {
sum += sub.countLit(_on);
}
result = sum;
}
for (Box sub : subBoxes.values()) {
Assert.assertTrue(sub.left >= this.left &&
sub.right <= this.right &&
sub.bottom >= this.bottom &&
sub.top <= this.top);
}
testOverlap();
Assert.assertEquals(bruteForce.countLit(_on, this.left, this.bottom, this.right, this.top),
result);
return result;
}
void testOverlap(){
for(Map.Entry<Corner, Box> entry1 : subBoxes.entrySet()) {
for(Map.Entry<Corner, Box> entry2 : subBoxes.entrySet()) {
if (entry1.getKey().compareTo(entry2.getKey()) > 0) {
Assert.assertFalse(entry1.getValue().isOverlapping(entry2.getValue()));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment