diff --git a/2021/Cargo.toml b/2021/Cargo.toml index ce383da..b406b65 100644 --- a/2021/Cargo.toml +++ b/2021/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +fnv = "1.0.7" itertools = "0.10" paste = "1.0" diff --git a/2021/inputs/day05 b/2021/inputs/day05 new file mode 100644 index 0000000..d6a5213 --- /dev/null +++ b/2021/inputs/day05 @@ -0,0 +1,500 @@ +456,846 -> 221,846 +980,926 -> 73,19 +682,930 -> 562,930 +766,592 -> 274,100 +247,685 -> 247,21 +106,800 -> 635,800 +953,340 -> 135,340 +293,223 -> 293,12 +454,196 -> 454,463 +886,766 -> 164,766 +592,590 -> 192,590 +436,982 -> 436,545 +731,571 -> 420,260 +741,11 -> 466,11 +727,541 -> 579,541 +341,553 -> 25,553 +942,470 -> 942,196 +203,600 -> 203,647 +965,595 -> 949,611 +554,306 -> 554,401 +902,438 -> 902,728 +864,609 -> 525,270 +187,790 -> 187,323 +956,950 -> 427,950 +847,554 -> 422,554 +935,900 -> 701,900 +192,854 -> 866,180 +512,946 -> 543,915 +978,979 -> 491,979 +708,61 -> 708,878 +738,508 -> 282,52 +23,25 -> 841,843 +204,750 -> 204,797 +703,500 -> 703,419 +14,311 -> 694,311 +646,301 -> 785,301 +397,168 -> 439,168 +680,931 -> 561,812 +540,448 -> 90,448 +706,668 -> 91,53 +848,319 -> 318,319 +198,948 -> 198,307 +686,58 -> 686,541 +867,234 -> 867,498 +134,125 -> 134,688 +824,566 -> 53,566 +437,167 -> 276,167 +94,65 -> 638,609 +36,971 -> 971,36 +494,330 -> 494,197 +920,438 -> 920,364 +698,84 -> 49,733 +59,842 -> 59,876 +328,577 -> 328,677 +757,701 -> 134,78 +466,274 -> 135,605 +81,925 -> 988,18 +40,142 -> 882,984 +50,96 -> 882,928 +782,47 -> 782,427 +247,599 -> 24,599 +112,812 -> 191,733 +487,198 -> 144,198 +327,663 -> 327,756 +117,76 -> 688,76 +530,71 -> 530,958 +558,602 -> 671,489 +677,830 -> 677,556 +529,669 -> 349,669 +336,966 -> 341,971 +20,31 -> 851,862 +423,880 -> 423,573 +521,657 -> 552,657 +412,822 -> 18,428 +423,311 -> 423,105 +381,614 -> 705,614 +521,248 -> 394,121 +286,47 -> 286,403 +286,27 -> 711,452 +347,61 -> 489,61 +760,454 -> 760,954 +746,573 -> 911,573 +839,933 -> 839,776 +124,815 -> 290,649 +577,848 -> 419,848 +393,206 -> 410,206 +364,755 -> 881,755 +788,68 -> 788,215 +94,798 -> 192,798 +292,250 -> 453,250 +601,545 -> 293,237 +438,923 -> 438,655 +70,757 -> 887,757 +184,402 -> 818,402 +586,49 -> 103,49 +202,315 -> 735,315 +534,504 -> 534,523 +367,236 -> 367,736 +24,163 -> 24,240 +185,426 -> 634,875 +485,189 -> 39,189 +556,30 -> 374,30 +969,821 -> 676,528 +254,435 -> 254,43 +290,615 -> 741,164 +345,601 -> 120,826 +224,641 -> 887,641 +190,716 -> 581,325 +552,646 -> 552,393 +413,177 -> 413,103 +397,900 -> 360,900 +138,980 -> 138,55 +909,891 -> 909,593 +926,986 -> 79,139 +954,67 -> 53,968 +180,30 -> 595,30 +823,165 -> 823,660 +285,176 -> 375,176 +915,826 -> 184,95 +735,230 -> 667,230 +934,865 -> 917,865 +48,602 -> 737,602 +477,319 -> 385,411 +981,17 -> 11,987 +458,401 -> 24,401 +118,415 -> 849,415 +176,678 -> 176,852 +567,753 -> 567,37 +285,868 -> 830,323 +555,623 -> 822,623 +522,546 -> 674,546 +880,21 -> 23,878 +591,103 -> 591,407 +434,64 -> 434,401 +245,968 -> 275,968 +726,510 -> 450,786 +768,366 -> 768,738 +488,745 -> 488,94 +675,674 -> 675,705 +618,237 -> 265,237 +802,709 -> 802,59 +144,696 -> 144,542 +547,381 -> 547,799 +78,667 -> 78,916 +409,271 -> 302,271 +294,694 -> 938,50 +140,571 -> 97,571 +682,875 -> 682,534 +748,816 -> 748,183 +84,622 -> 84,258 +485,696 -> 582,599 +909,233 -> 954,233 +203,711 -> 203,350 +335,904 -> 455,904 +578,778 -> 578,21 +830,954 -> 902,954 +78,252 -> 78,682 +920,220 -> 684,220 +309,301 -> 104,301 +270,795 -> 270,919 +906,479 -> 304,479 +627,164 -> 627,986 +122,960 -> 915,167 +664,916 -> 770,810 +692,810 -> 826,810 +981,951 -> 192,162 +183,423 -> 809,423 +632,464 -> 567,464 +94,266 -> 94,587 +261,770 -> 569,770 +51,403 -> 466,818 +631,645 -> 187,645 +141,238 -> 141,145 +357,21 -> 173,21 +138,248 -> 839,949 +889,957 -> 807,957 +399,431 -> 105,725 +548,331 -> 548,821 +790,844 -> 43,97 +675,671 -> 221,671 +874,143 -> 620,397 +205,435 -> 205,546 +521,434 -> 822,133 +141,86 -> 257,86 +427,28 -> 290,165 +49,694 -> 567,694 +846,344 -> 266,924 +425,910 -> 433,918 +956,498 -> 485,27 +798,498 -> 798,634 +879,13 -> 766,126 +737,475 -> 737,425 +338,473 -> 425,386 +510,615 -> 214,319 +758,415 -> 758,490 +969,208 -> 239,938 +917,188 -> 917,528 +34,820 -> 806,820 +85,633 -> 857,633 +262,355 -> 262,748 +373,784 -> 971,186 +146,577 -> 60,663 +613,570 -> 613,199 +300,319 -> 300,108 +764,171 -> 764,17 +555,921 -> 555,825 +241,197 -> 770,197 +600,832 -> 600,807 +934,987 -> 20,73 +960,730 -> 837,730 +976,50 -> 46,980 +829,834 -> 153,158 +785,835 -> 785,58 +586,633 -> 689,736 +804,250 -> 348,706 +226,539 -> 16,539 +411,940 -> 98,940 +289,589 -> 893,589 +738,616 -> 738,55 +225,54 -> 542,54 +793,246 -> 303,736 +332,752 -> 984,100 +413,18 -> 839,444 +840,122 -> 840,233 +989,970 -> 215,196 +329,361 -> 573,605 +242,537 -> 242,619 +943,898 -> 943,535 +469,865 -> 501,833 +226,717 -> 196,687 +819,803 -> 712,803 +532,663 -> 532,672 +61,931 -> 940,52 +623,218 -> 274,567 +281,326 -> 281,790 +815,176 -> 679,176 +790,862 -> 942,710 +18,771 -> 18,514 +479,377 -> 309,377 +704,402 -> 704,150 +961,335 -> 492,335 +745,829 -> 745,477 +556,543 -> 771,543 +832,336 -> 917,251 +742,755 -> 742,174 +206,735 -> 493,735 +151,216 -> 312,55 +445,157 -> 615,157 +781,143 -> 781,76 +833,717 -> 514,398 +357,14 -> 357,36 +771,405 -> 771,422 +662,886 -> 169,886 +689,990 -> 22,990 +680,445 -> 379,445 +92,369 -> 502,779 +64,948 -> 64,363 +295,957 -> 976,276 +113,920 -> 634,399 +542,662 -> 305,899 +566,514 -> 566,645 +528,106 -> 549,106 +205,367 -> 821,367 +313,105 -> 313,928 +532,177 -> 532,664 +862,773 -> 905,816 +800,796 -> 911,796 +870,80 -> 11,939 +188,900 -> 154,900 +420,509 -> 520,609 +540,863 -> 28,863 +31,72 -> 78,72 +823,648 -> 503,648 +879,252 -> 606,252 +677,117 -> 677,507 +743,303 -> 196,850 +220,491 -> 220,891 +216,815 -> 577,815 +540,819 -> 745,819 +152,721 -> 382,721 +280,745 -> 985,745 +479,367 -> 358,488 +913,413 -> 649,413 +40,678 -> 817,678 +467,533 -> 467,214 +132,68 -> 843,779 +519,109 -> 669,259 +619,791 -> 221,791 +114,622 -> 628,622 +951,636 -> 866,636 +172,569 -> 775,569 +244,972 -> 173,972 +283,64 -> 739,520 +68,604 -> 68,156 +529,30 -> 529,925 +813,883 -> 137,883 +893,231 -> 629,231 +673,658 -> 673,389 +725,899 -> 218,899 +317,318 -> 105,318 +82,706 -> 100,688 +222,227 -> 440,227 +810,371 -> 810,985 +414,321 -> 289,446 +901,158 -> 260,799 +198,967 -> 717,448 +928,454 -> 875,454 +974,437 -> 974,764 +657,13 -> 760,13 +498,966 -> 976,966 +66,104 -> 66,15 +773,569 -> 980,362 +420,496 -> 403,513 +57,920 -> 85,920 +879,551 -> 879,662 +98,395 -> 98,398 +483,685 -> 483,55 +222,935 -> 586,935 +89,926 -> 807,208 +744,160 -> 744,462 +588,973 -> 588,548 +312,572 -> 38,298 +27,131 -> 552,656 +591,935 -> 591,86 +907,478 -> 907,279 +981,75 -> 981,972 +316,947 -> 935,947 +906,38 -> 906,216 +374,521 -> 345,550 +579,29 -> 579,107 +444,636 -> 444,557 +458,608 -> 830,980 +479,839 -> 155,515 +766,600 -> 766,71 +976,965 -> 31,20 +928,49 -> 269,708 +787,238 -> 787,983 +583,742 -> 112,742 +966,268 -> 554,680 +671,354 -> 671,966 +274,340 -> 274,894 +673,185 -> 607,185 +73,171 -> 874,171 +861,526 -> 861,410 +739,591 -> 739,138 +209,355 -> 209,146 +286,501 -> 887,501 +495,902 -> 700,902 +192,889 -> 821,260 +400,21 -> 154,21 +861,301 -> 325,301 +552,990 -> 511,990 +908,21 -> 11,918 +127,724 -> 821,30 +935,46 -> 170,811 +947,91 -> 374,91 +625,420 -> 265,60 +214,228 -> 546,228 +375,547 -> 715,887 +516,350 -> 870,350 +610,138 -> 665,193 +214,621 -> 678,621 +497,248 -> 600,145 +549,558 -> 576,558 +364,537 -> 364,312 +840,324 -> 310,854 +441,945 -> 441,458 +459,531 -> 459,100 +937,113 -> 150,900 +277,405 -> 259,405 +409,527 -> 409,359 +534,766 -> 534,740 +534,934 -> 681,934 +456,419 -> 83,419 +871,986 -> 873,986 +14,59 -> 916,961 +911,963 -> 971,963 +25,325 -> 139,211 +937,184 -> 354,767 +460,416 -> 289,245 +193,171 -> 861,839 +840,299 -> 840,911 +531,45 -> 531,619 +599,315 -> 455,315 +455,97 -> 455,811 +38,748 -> 392,748 +841,79 -> 841,88 +105,571 -> 105,545 +801,458 -> 344,458 +491,535 -> 558,535 +835,814 -> 223,202 +563,85 -> 405,85 +410,396 -> 600,396 +273,670 -> 818,125 +671,647 -> 817,647 +46,892 -> 678,260 +456,736 -> 110,736 +962,941 -> 619,598 +388,406 -> 53,71 +558,895 -> 227,564 +944,182 -> 807,319 +484,898 -> 59,473 +808,214 -> 488,534 +451,679 -> 155,383 +858,931 -> 381,931 +723,377 -> 723,281 +694,283 -> 182,795 +385,191 -> 320,256 +33,380 -> 584,931 +480,91 -> 817,91 +677,91 -> 677,126 +291,651 -> 760,182 +832,962 -> 153,283 +38,60 -> 479,501 +249,350 -> 789,350 +603,341 -> 266,678 +52,303 -> 52,102 +911,201 -> 559,201 +46,210 -> 46,275 +960,212 -> 554,212 +375,374 -> 169,580 +10,10 -> 989,989 +844,140 -> 40,944 +916,408 -> 916,815 +834,401 -> 834,169 +553,479 -> 784,248 +543,452 -> 543,848 +854,910 -> 334,390 +685,491 -> 793,491 +552,943 -> 709,943 +723,367 -> 124,367 +95,55 -> 881,841 +155,267 -> 573,267 +59,357 -> 84,357 +218,435 -> 218,344 +491,584 -> 491,649 +676,445 -> 676,333 +361,618 -> 783,618 +220,295 -> 220,267 +668,758 -> 299,389 +965,845 -> 674,845 +285,603 -> 47,603 +853,417 -> 853,757 +859,906 -> 856,906 +55,364 -> 753,364 +893,474 -> 978,474 +602,32 -> 58,576 +171,445 -> 96,370 +214,592 -> 214,286 +400,946 -> 745,946 +559,37 -> 112,484 +624,510 -> 90,510 +329,714 -> 329,850 +458,287 -> 657,287 +99,385 -> 99,949 +50,736 -> 719,67 +273,195 -> 273,306 +490,902 -> 490,798 +619,131 -> 921,131 +266,652 -> 266,730 +745,661 -> 745,555 +311,878 -> 311,679 +491,982 -> 643,830 +735,875 -> 816,875 +936,353 -> 936,529 +792,467 -> 565,467 +141,140 -> 141,988 +98,171 -> 414,487 +257,259 -> 257,484 +24,41 -> 969,986 +302,453 -> 223,453 +807,363 -> 492,678 +823,22 -> 835,10 +301,94 -> 399,94 +946,110 -> 248,808 +983,985 -> 21,23 +510,145 -> 510,58 +13,661 -> 13,639 +218,260 -> 218,54 +475,846 -> 475,770 +458,644 -> 458,529 +912,934 -> 912,136 +152,823 -> 550,823 +136,470 -> 443,470 +253,871 -> 905,219 +765,212 -> 793,240 +11,402 -> 11,42 +348,813 -> 348,768 +368,321 -> 823,776 +343,495 -> 343,809 +117,616 -> 117,273 +92,92 -> 732,92 +914,31 -> 28,917 +259,944 -> 214,944 +630,759 -> 462,759 +134,653 -> 134,610 +14,989 -> 988,15 +139,181 -> 139,451 +598,636 -> 598,442 +263,42 -> 686,465 diff --git a/2021/src/bin/day05.rs b/2021/src/bin/day05.rs new file mode 100644 index 0000000..77e3b89 --- /dev/null +++ b/2021/src/bin/day05.rs @@ -0,0 +1,76 @@ +#![feature(test)] +extern crate test; +use aoc2021::common::*; +use fnv::FnvHashMap; +use itertools::Itertools; +use std::collections::HashMap; + +const DAY: usize = 5; +type Parsed = Vec<((usize, usize), (usize, usize))>; + +fn parse_input(raw: &str) -> Parsed { + raw.lines() + .filter_map(|line| line.split_once(" -> ")) + .filter_map(|(c1, c2)| c1.split_once(',').zip(c2.split_once(','))) + .map(|((x1, y1), (x2, y2))| ((x1.parse().unwrap(), y1.parse().unwrap()), (x2.parse().unwrap(), y2.parse().unwrap()))) + .collect() +} + +fn part1(parsed: &Parsed) -> usize { + let mut map: HashMap<_, _, _> = FnvHashMap::default(); + parsed + .iter() + .flat_map(|cs| match cs { + ((x1, y1), (x2, y2)) if x1 == x2 => (*y1.min(y2)..=*y1.max(y2)).map(|y| (*x1, y)).collect(), + ((x1, y1), (x2, y2)) if y1 == y2 => (*x1.min(x2)..=*x1.max(x2)).map(|x| (x, *y1)).collect(), + _ => vec![], + }) + .for_each(|c| *map.entry(c).or_insert(0) += 1); + map.values().filter(|&&n| n > 1).count() +} + +fn part2(parsed: &Parsed) -> usize { + let mut map: HashMap<_, _, _> = FnvHashMap::default(); + parsed + .iter() + .flat_map(|cs| match cs { + ((x1, y1), (x2, y2)) if x1 == x2 => (*y1.min(y2)..=*y1.max(y2)).map(|y| (*x1, y)).collect_vec(), + ((x1, y1), (x2, y2)) if y1 == y2 => (*x1.min(x2)..=*x1.max(x2)).map(|x| (x, *y1)).collect(), + ((x1, y1), (x2, y2)) if x1 < x2 && y1 < y2 => (*x1..=*x2).zip(*y1..=*y2).collect(), + ((x1, y1), (x2, y2)) if x1 < x2 && y1 > y2 => (*x1..=*x2).zip((*y2..=*y1).rev()).collect(), + ((x1, y1), (x2, y2)) if x1 > x2 && y1 < y2 => (*x2..=*x1).rev().zip(*y1..=*y2).collect(), + ((x1, y1), (x2, y2)) if x1 > x2 && y1 > y2 => (*x2..=*x1).rev().zip((*y2..=*y1).rev()).collect(), + _ => unreachable!(), + }) + .for_each(|c| *map.entry(c).or_insert(0) += 1); + map.values().filter(|&&n| n > 1).count() +} + +fn main() { + let input = parse_input(&read_file(DAY)); + println!("Part 1: {}", part1(&input)); + println!("Part 2: {}", part2(&input)); +} + +#[cfg(test)] +mod tests { + use super::*; + use aoc2021::*; + + const TEST_INPUT: &str = "0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2"; + + test!(part1() == 5); + test!(part2() == 12); + bench!(part1() == 5280); + bench!(part2() == 16716); + bench_input!(Vec::len => 500); +}