-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.clj
56 lines (49 loc) · 8.96 KB
/
06.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(def input '((:on 489 959 759 964) (:off 820 516 871 914) (:off 427 423 929 502) (:on 774 14 977 877) (:on 410 146 864 337) (:on 931 331 939 812) (:off 756 53 923 339) (:off 313 787 545 979) (:off 12 823 102 934) (:toggle 756 965 812 992) (:off 743 684 789 958) (:toggle 120 314 745 489) (:toggle 692 845 866 994) (:off 587 176 850 273) (:off 674 321 793 388) (:toggle 749 672 973 965) (:on 943 30 990 907) (:on 296 50 729 664) (:on 212 957 490 987) (:toggle 171 31 688 88) (:off 991 989 994 998) (:off 913 943 958 953) (:off 278 258 367 386) (:toggle 275 796 493 971) (:off 70 873 798 923) (:toggle 258 985 663 998) (:on 601 259 831 486) (:off 914 94 941 102) (:off 558 161 994 647) (:on 119 662 760 838) (:toggle 378 775 526 852) (:off 384 670 674 972) (:off 249 41 270 936) (:on 614 742 769 780) (:on 427 70 575 441) (:on 410 478 985 753) (:off 619 46 931 342) (:on 284 55 768 922) (:off 40 592 728 685) (:on 825 291 956 950) (:on 147 843 592 909) (:off 218 675 972 911) (:toggle 249 291 350 960) (:off 556 80 967 675) (:toggle 609 148 968 279) (:toggle 217 605 961 862) (:toggle 407 177 548 910) (:toggle 400 936 599 938) (:off 721 101 925 455) (:on 268 631 735 814) (:toggle 549 969 612 991) (:toggle 553 268 689 432) (:off 817 668 889 897) (:toggle 801 544 858 556) (:toggle 615 729 832 951) (:off 427 477 958 948) (:on 164 49 852 946) (:on 542 449 774 776) (:off 923 196 980 446) (:toggle 90 310 718 846) (:off 657 215 744 252) (:off 800 239 811 712) (:on 502 90 619 760) (:toggle 649 512 862 844) (:off 334 903 823 935) (:off 630 233 839 445) (:on 713 67 839 865) (:on 932 50 982 411) (:off 480 729 984 910) (:on 100 219 796 395) (:on 758 108 850 950) (:off 427 276 439 938) (:on 178 284 670 536) (:toggle 540 27 625 102) (:off 906 722 936 948) (:toggle 345 418 859 627) (:toggle 175 775 580 781) (:toggle 863 28 929 735) (:off 824 858 905 973) (:toggle 752 312 863 425) (:on 985 716 988 852) (:off 68 504 763 745) (:toggle 76 209 810 720) (:off 657 607 676 664) (:toggle 596 869 896 921) (:off 915 411 968 945) (:off 368 39 902 986) (:on 11 549 393 597) (:off 842 893 976 911) (:toggle 274 106 581 329) (:toggle 406 403 780 950) (:toggle 408 988 500 994) (:toggle 217 73 826 951) (:on 917 872 961 911) (:toggle 394 34 510 572) (:toggle 424 603 583 626) (:toggle 106 159 755 738) (:off 244 610 472 709) (:on 350 265 884 690) (:on 688 184 928 280) (:toggle 279 443 720 797) (:off 615 493 888 610) (:toggle 118 413 736 632) (:on 798 782 829 813) (:off 250 934 442 972) (:on 68 503 400 949) (:toggle 297 482 313 871) (:toggle 710 3 839 859) (:on 125 300 546 888) (:toggle 482 39 584 159) (:off 536 89 765 962) (:on 530 518 843 676) (:on 994 467 994 676) (:on 623 628 744 927) (:toggle 704 912 837 983) (:on 154 364 517 412) (:toggle 344 409 780 524) (:off 578 740 725 879) (:on 251 933 632 957) (:on 827 705 971 789) (:toggle 191 282 470 929) (:toggle 324 525 446 867) (:toggle 534 343 874 971) (:toggle 550 650 633 980) (:toggle 837 404 881 915) (:toggle 338 881 845 905) (:on 469 462 750 696) (:on 741 703 892 870) (:off 570 215 733 562) (:on 445 576 870 775) (:on 466 747 554 878) (:off 820 453 868 712) (:off 892 706 938 792) (:off 300 238 894 746) (:off 306 44 457 444) (:off 912 569 967 963) (:toggle 109 756 297 867) (:on 37 546 41 951) (:on 321 637 790 910) (:toggle 66 50 579 301) (:toggle 933 221 933 791) (:on 486 676 878 797) (:on 417 231 556 317) (:toggle 904 468 981 873) (:on 417 675 749 712) (:on 692 371 821 842) (:toggle 324 73 830 543) (:on 912 490 977 757) (:off 634 872 902 949) (:toggle 266 779 870 798) (:on 772 982 990 996) (:off 607 46 798 559) (:on 295 602 963 987) (:on 657 86 944 742) (:off 334 639 456 821) (:off 997 667 997 670) (:off 725 832 951 945) (:off 30 120 952 984) (:on 860 965 917 976) (:toggle 471 997 840 998) (:off 319 307 928 504) (:toggle 823 631 940 908) (:toggle 969 984 981 993) (:off 691 319 865 954) (:toggle 911 926 938 929) (:on 953 937 968 991) (:toggle 914 643 975 840) (:on 266 982 436 996) (:off 101 896 321 932) (:off 193 852 751 885) (:off 576 532 863 684) (:on 761 456 940 783) (:on 20 290 398 933) (:off 435 335 644 652) (:on 830 569 905 770) (:off 630 517 905 654) (:on 664 53 886 976) (:toggle 275 416 408 719) (:on 370 621 515 793) (:on 483 373 654 749) (:on 656 786 847 928) (:off 532 752 945 974) (:toggle 301 150 880 792) (:off 951 488 958 952) (:on 207 729 882 828) (:toggle 694 532 973 961) (:toggle 676 639 891 802) (:off 653 6 905 519) (:toggle 391 109 418 312) (:on 877 423 957 932) (:on 340 145 563 522) (:off 978 467 988 895) (:off 396 418 420 885) (:off 31 308 816 316) (:on 107 675 758 824) (:on 61 82 789 876) (:on 750 743 754 760) (:toggle 88 733 736 968) (:off 754 349 849 897) (:toggle 157 50 975 781) (:off 230 231 865 842) (:off 516 317 630 329) (:off 697 820 829 903) (:on 218 250 271 732) (:toggle 56 167 404 431) (:toggle 626 891 680 927) (:toggle 370 207 791 514) (:toggle 860 74 949 888) (:on 416 527 616 541) (:off 745 449 786 908) (:on 485 554 689 689) (:on 586 62 693 141) (:toggle 506 759 768 829) (:on 473 109 929 166) (:on 760 617 773 789) (:toggle 595 683 618 789) (:off 210 775 825 972) (:toggle 12 426 179 982) (:on 774 539 778 786) (:on 102 498 121 807) (:off 706 897 834 965) (:off 678 529 824 627) (:on 7 765 615 870) (:off 730 872 974 943) (:off 595 626 836 711) (:off 215 424 841 959) (:toggle 341 780 861 813) (:toggle 507 503 568 822) (:on 252 603 349 655) (:toggle 93 521 154 834) (:on 565 682 951 954) (:on 544 318 703 418) (:toggle 756 953 891 964) (:on 531 123 856 991) (:on 148 315 776 559) (:off 925 835 963 971) (:on 895 944 967 964) (:off 102 527 650 747) (:toggle 626 105 738 720) (:off 160 75 384 922) (:toggle 813 724 903 941) (:on 207 107 982 849) (:toggle 750 505 961 697) (:toggle 105 410 885 819) (:on 226 104 298 283) (:off 224 604 508 762) (:on 477 368 523 506) (:off 477 901 627 936) (:off 887 131 889 670) (:on 896 994 938 999) (:toggle 401 580 493 728) (:toggle 987 184 991 205) (:on 821 643 882 674) (:toggle 784 940 968 959) (:off 251 293 274 632) (:off 339 840 341 844) (:off 675 351 675 836) (:toggle 918 857 944 886) (:toggle 70 253 918 736) (:off 612 604 772 680) (:off 277 40 828 348) (:toggle 692 139 698 880) (:toggle 124 446 883 453) (:toggle 969 932 990 945) (:toggle 855 692 993 693) (:toggle 722 472 887 899) (:toggle 978 149 985 442) (:toggle 837 540 916 889) (:off 612 2 835 82) (:toggle 560 767 878 856) (:on 461 734 524 991) (:toggle 206 824 976 912) (:on 826 610 879 892) (:on 577 699 956 933) (:off 9 250 50 529) (:off 77 657 817 677) (:on 68 419 86 426) (:on 991 720 992 784) (:on 668 20 935 470) (:off 133 418 613 458) (:off 487 286 540 328) (:toggle 247 874 840 955) (:toggle 301 808 754 970) (:off 34 194 578 203) (:off 451 49 492 921) (:on 907 256 912 737) (:off 479 305 702 587) (:on 545 583 732 749) (:toggle 11 16 725 868) (:on 965 343 986 908) (:on 674 953 820 965) (:toggle 398 147 504 583) (:off 778 194 898 298) (:on 179 140 350 852) (:off 241 118 530 832) (:off 41 447 932 737) (:off 820 663 832 982) (:on 550 460 964 782) (:on 31 760 655 892) (:toggle 628 958 811 992)))
; Trying to do anything to make this code less repeated
; causes it to become much slower for some reason.
; I suspect that perhaps not hardcoding literals, and instead passing
; them in, causes clojure to not figure out the types correctly.
; Using type hints (^ints) is appearently really important as well.
; This solution uses an impure java array. Clojure felt incredibly unsuitable
; for this type of problem. Solution more or less taken from here:
; https://stackoverflow.com/questions/34153369/why-is-this-clojure-program-working-on-a-mutable-array-so-slow
(defn turn-on-p1 [^ints grid [r1 c1 r2 c2]]
(doseq [i (range r1 (inc r2))
k (range (+ c1 (* i 1000)) (+ (inc c2) (* i 1000)))]
(aset grid k 1)))
(defn turn-off-p1 [^ints grid [r1 c1 r2 c2]]
(doseq [i (range r1 (inc r2))
k (range (+ c1 (* i 1000)) (+ (inc c2) (* i 1000)))]
(aset grid k 0)))
(defn toggle-p1 [^ints grid [r1 c1 r2 c2]]
(doseq [i (range r1 (inc r2))
k (range (+ c1 (* i 1000)) (+ (inc c2) (* i 1000)))]
(aset grid k (- 1 (aget grid k)))))
(defn part1 [grid ops]
(doseq [[op & box] ops]
(case op
:on (turn-on-p1 grid box)
:off (turn-off-p1 grid box)
:toggle (toggle-p1 grid box)))
(reduce + grid))
(defn turn-on-p2 [^ints grid [r1 c1 r2 c2]]
(doseq [i (range r1 (inc r2))
k (range (+ c1 (* i 1000)) (+ (inc c2) (* i 1000)))]
(aset grid k (+ 1 (aget grid k)))))
(defn turn-off-p2 [^ints grid [r1 c1 r2 c2]]
(doseq [i (range r1 (inc r2))
k (range (+ c1 (* i 1000)) (+ (inc c2) (* i 1000)))]
(aset grid k (max (+ -1 (aget grid k)) 0))))
(defn toggle-p2 [^ints grid [r1 c1 r2 c2]]
(doseq [i (range r1 (inc r2))
k (range (+ c1 (* i 1000)) (+ (inc c2) (* i 1000)))]
(aset grid k (+ 2 (aget grid k)))))
(defn part2 [grid ops]
(doseq [[op & box] ops]
(case op
:on (turn-on-p2 grid box)
:off (turn-off-p2 grid box)
:toggle (toggle-p2 grid box)))
(reduce + grid))
(->> input (part1 (int-array (* 1000 1000))) (println "Part one:"))
(->> input (part2 (int-array (* 1000 1000))) (println "Part two:"))