Skip to content

Instantly share code, notes, and snippets.

@boldijar
Created February 21, 2017 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boldijar/87cef9a73a6d8e47e524f468609b0621 to your computer and use it in GitHub Desktop.
Save boldijar/87cef9a73a6d8e47e524f468609b0621 to your computer and use it in GitHub Desktop.
closest location quiz
import java.io.*;
import java.util.*;
/**
* Created by BoldijarPaul on 31/01/2017.
*/
public class Main {
public static class Location implements Comparable {
int x;
int y;
double distance;
int initialIndex;
Location(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Object o) {
if (o == null || !(o instanceof Location)) {
return 0;
}
Location location = (Location) o;
if (distance < location.distance) {
return -1;
}
return 1;
}
}
public static class TestCase {
Location userLocation;
TreeSet<Location> stores;
}
private static double distance(Location location1, Location location2) {
return Math.sqrt((location1.x - location2.x) * (location1.x - location2.x) +
(location1.y - location2.y) * (location1.y - location2.y));
}
private static Location locationFromString(String locationString) {
String[] coords = locationString.split("\\s+");
return new Location(Integer.valueOf(coords[0]), Integer.valueOf(coords[1]));
}
public static void main(String[] args) throws IOException {
List<TestCase> testCaseList = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(new File("task1-test-input.txt")));
int testCases = Integer.valueOf(reader.readLine());
reader.readLine();
for (int i = 1; i <= testCases; i++) {
TestCase testCase = new TestCase();
testCase.stores = new TreeSet<>();
testCase.userLocation = locationFromString(reader.readLine());
int storesCount = Integer.valueOf(reader.readLine());
for (int j = 1; j <= storesCount; j++) {
Location storeLocation = locationFromString(reader.readLine());
storeLocation.distance = distance(storeLocation, testCase.userLocation);
storeLocation.initialIndex = j;
testCase.stores.add(storeLocation);
}
testCaseList.add(testCase);
reader.readLine();
}
reader.close();
outputTestCases(testCaseList);
}
private static void outputTestCases(List<TestCase> testCaseList) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter printWriter = new PrintWriter("task1-test-output.txt", "UTF-8");
for (int i = 0, testCaseListSize = testCaseList.size(); i < testCaseListSize; i++) {
TestCase testCase = testCaseList.get(i);
Location closestLocation = testCase.stores.first();
printWriter.println(String.format("Case #%d: %d %.3f", i + 1, closestLocation.initialIndex, closestLocation.distance));
}
printWriter.close();
}
}
20
0 0
3
50 50
20 40
60 30
50 20
3
50 50
20 40
60 30
50 50
3
50 50
20 40
60 30
0 0
3
500 500
707 0
600 374
10 296
1
543 156
313 561
1
64 391
280 305
1
997 87
628 893
40
628 324
628 404
628 732
628 380
628 197
628 58
628 540
628 744
628 124
628 476
628 991
628 406
628 471
628 596
628 62
628 973
628 228
628 805
628 742
628 826
628 888
628 680
628 353
628 274
628 328
628 754
628 387
628 56
628 615
628 978
628 520
628 443
628 16
628 398
628 812
628 696
628 487
628 506
628 870
628 388
601 62
97
601 991
601 545
601 233
601 732
601 207
601 817
601 875
601 688
601 651
601 767
601 549
601 0
601 962
601 144
601 142
601 267
601 148
601 687
601 519
601 511
601 476
601 612
601 249
601 546
601 358
601 963
601 521
601 923
601 328
601 218
601 654
601 293
601 678
601 874
601 778
601 613
601 92
601 985
601 213
601 311
601 532
601 859
601 928
601 916
601 228
601 105
601 1000
601 374
601 758
601 563
601 321
601 170
601 781
601 134
601 65
601 256
601 410
601 30
601 586
601 973
601 459
601 696
601 857
601 825
601 6
601 530
601 945
601 431
601 235
601 409
601 702
601 277
601 780
601 138
601 633
601 179
601 878
601 240
601 15
601 38
601 834
601 40
601 649
601 192
601 727
601 564
601 957
601 920
601 529
601 792
601 548
601 634
601 226
601 850
601 320
601 955
601 837
311 989
30
311 204
311 89
311 820
311 779
311 508
311 61
311 160
311 813
311 328
311 970
311 459
311 22
311 408
311 14
311 101
311 830
311 907
311 411
311 163
311 523
311 82
311 863
311 959
311 597
311 107
311 892
311 963
311 244
311 401
311 755
3 332
86
413 332
778 332
658 332
815 332
153 332
595 332
547 332
952 332
896 332
246 332
872 332
774 332
871 332
241 332
627 332
436 332
496 332
628 332
929 332
812 332
82 332
971 332
532 332
459 332
794 332
904 332
790 332
947 332
661 332
865 332
121 332
16 332
895 332
150 332
777 332
976 332
58 332
177 332
248 332
88 332
722 332
87 332
358 332
861 332
220 332
686 332
95 332
339 332
727 332
884 332
141 332
743 332
185 332
170 332
937 332
165 332
940 332
549 332
285 332
697 332
514 332
226 332
465 332
980 332
745 332
75 332
412 332
466 332
427 332
30 332
585 332
401 332
137 332
513 332
48 332
307 332
282 332
862 332
479 332
319 332
394 332
179 332
902 332
93 332
973 332
72 332
412 474
19
237 474
657 474
756 474
731 474
516 474
773 474
926 474
52 474
147 474
100 474
528 474
997 474
849 474
838 474
249 474
644 474
755 474
168 474
931 474
122 447
20
56 447
476 447
975 447
899 447
325 447
139 447
45 447
226 447
760 447
513 447
32 447
775 447
158 447
480 447
843 447
467 447
30 447
6 447
255 447
769 447
21 598
100
591 67
275 817
640 725
983 843
819 14
801 283
906 649
989 76
75 831
678 233
729 708
175 426
402 121
21 861
701 733
458 292
800 734
799 130
148 781
973 967
486 463
941 81
112 929
157 878
450 526
110 869
924 977
294 326
97 5
877 488
739 335
470 228
759 269
358 907
740 331
564 225
484 504
998 596
432 154
164 572
680 274
441 294
941 735
310 728
431 186
216 169
521 376
397 970
335 446
877 75
467 130
991 951
634 988
547 756
833 711
328 202
675 769
496 306
193 806
33 314
993 940
483 203
316 571
173 651
16 740
416 173
870 407
123 193
84 360
949 607
761 967
810 436
425 305
432 309
802 465
623 484
95 797
687 411
57 550
752 764
289 859
937 849
956 59
41 39
420 681
647 871
337 146
997 453
141 428
762 943
894 74
426 679
561 804
89 618
43 531
381 23
389 317
872 344
67 604
74 177
975 411
100
47 1
557 44
454 698
472 215
331 55
981 757
734 541
250 513
159 294
735 230
317 123
548 879
158 305
482 923
482 146
333 529
148 580
263 602
968 426
508 298
171 178
746 596
719 996
108 568
980 843
799 296
657 36
866 505
341 37
427 513
184 450
732 332
720 996
624 687
421 822
676 592
1000 421
878 409
106 987
977 777
519 465
763 866
191 628
60 222
666 177
735 540
317 467
562 36
152 185
414 573
698 89
855 697
200 733
796 997
409 463
773 618
928 536
174 119
163 234
341 519
412 767
58 729
233 620
456 385
496 870
648 193
649 192
580 849
615 376
845 714
839 618
22 456
844 196
575 697
430 607
216 532
373 274
952 296
585 407
371 80
967 709
964 615
591 543
463 897
609 998
300 137
615 322
594 458
518 859
155 639
155 61
861 218
25 812
514 610
909 575
380 875
283 33
179 565
267 642
461 876
330 761
100
704 945
774 988
93 291
536 939
620 692
690 481
910 715
983 114
15 892
689 85
456 663
119 636
227 386
968 378
952 297
829 655
933 602
332 716
584 869
344 203
250 33
374 159
748 357
964 453
248 343
539 394
5 658
29 923
734 688
991 685
675 819
30 607
111 362
12 695
921 356
588 170
389 963
330 828
319 984
971 567
326 509
961 21
857 681
944 590
368 624
966 42
133 686
340 244
47 352
629 659
399 216
829 478
178 849
305 497
833 276
754 849
475 405
871 22
776 504
302 834
128 267
566 261
953 906
195 691
949 824
349 347
39 177
825 909
717 821
405 549
787 850
397 952
945 958
974 720
462 276
553 590
233 118
541 877
715 736
567 353
249 606
700 979
473 214
887 189
725 983
428 201
832 516
153 776
473 126
185 935
92 428
214 326
236 445
202 951
871 459
303 119
755 693
98 227
598 985
107 12
535 214
8
488 50
367 954
214 183
829 839
276 256
53 292
493 188
184 133
643 127
50
869 397
510 657
314 107
332 421
811 989
957 24
477 697
391 120
601 264
950 440
540 896
183 522
388 371
706 522
120 38
339 990
125 849
336 130
647 668
551 457
657 197
481 824
585 562
634 185
826 583
315 56
169 498
268 557
560 975
769 680
703 107
359 519
957 696
649 603
53 199
750 710
87 230
223 672
482 858
547 998
130 863
53 299
50 322
547 610
987 315
981 379
113 339
898 69
725 546
362 779
111 178
87
213 31
402 885
203 950
121 200
79 984
945 69
34 266
616 334
943 621
314 321
734 344
910 493
759 145
855 227
271 656
406 484
377 498
368 270
447 180
471 216
163 415
285 888
371 591
913 3
212 226
14 946
260 924
439 19
760 984
246 30
330 342
515 397
840 573
668 977
753 829
193 607
243 168
184 304
760 96
307 972
13 321
917 964
936 45
673 695
720 610
725 740
952 930
136 792
193 494
458 636
322 651
242 255
820 117
559 579
213 866
240 917
878 156
881 813
893 553
507 302
162 922
41 805
852 868
286 735
362 744
370 374
395 303
630 905
420 188
483 323
745 723
240 622
569 811
124 151
53 631
453 907
552 494
711 93
52 997
828 414
430 889
478 825
191 107
419 611
987 592
624 421
314 554
574 364
26
857 415
108 487
869 14
729 52
415 513
104 101
30 208
531 919
687 46
800 484
465 410
470 57
725 581
61 278
313 325
333 169
741 441
346 299
145 75
351 250
278 146
351 308
44 882
918 421
618 717
906 83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment