diff --git a/aoc2020/aoc2020_bench/benches/aoc2020_bench.rs b/aoc2020/aoc2020_bench/benches/aoc2020_bench.rs index 8e13e66..d6a8ba8 100644 --- a/aoc2020/aoc2020_bench/benches/aoc2020_bench.rs +++ b/aoc2020/aoc2020_bench/benches/aoc2020_bench.rs @@ -15,6 +15,7 @@ use aoc2020::day12; use aoc2020::day13; use aoc2020::day14; use aoc2020::day15; +use aoc2020::day16; fn aoc2020_all(c: &mut Criterion) { c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); @@ -32,6 +33,7 @@ fn aoc2020_all(c: &mut Criterion) { c.bench_function("day13", |b| b.iter(|| day13::run().unwrap())); c.bench_function("day14", |b| b.iter(|| day14::run().unwrap())); c.bench_function("day15", |b| b.iter(|| day15::run().unwrap())); + c.bench_function("day16", |b| b.iter(|| day16::run().unwrap())); } criterion_group! { diff --git a/aoc2020/input/day16.txt b/aoc2020/input/day16.txt new file mode 100644 index 0000000..4780707 --- /dev/null +++ b/aoc2020/input/day16.txt @@ -0,0 +1,263 @@ +departure location: 37-479 or 485-954 +departure station: 35-653 or 668-974 +departure platform: 36-225 or 240-955 +departure track: 27-854 or 879-966 +departure date: 46-828 or 834-951 +departure time: 36-591 or 613-957 +arrival location: 26-770 or 785-955 +arrival station: 33-695 or 712-961 +arrival platform: 39-369 or 391-968 +arrival track: 34-331 or 354-963 +class: 27-63 or 89-951 +duration: 42-545 or 561-956 +price: 50-250 or 258-955 +route: 41-150 or 168-953 +row: 49-356 or 366-970 +seat: 39-429 or 441-974 +train: 30-180 or 206-958 +type: 37-577 or 588-950 +wagon: 38-137 or 148-956 +zone: 36-909 or 927-970 + +your ticket: +97,101,149,103,137,61,59,223,263,179,131,113,241,127,53,109,89,173,107,211 + +nearby tickets: +446,499,748,453,135,109,525,721,179,796,622,944,175,303,882,287,177,185,828,423 +875,895,652,511,224,634,100,622,677,415,223,620,57,841,511,532,476,716,887,674 +899,387,218,646,539,745,265,213,834,897,591,507,621,799,617,732,621,286,628,571 +313,472,835,450,109,589,455,643,568,538,848,427,507,10,517,497,449,96,493,755 +483,325,573,319,491,695,564,325,468,615,694,407,108,63,327,213,576,324,446,491 +767,554,816,767,277,788,298,313,225,272,589,850,889,888,744,575,619,306,899,321 +285,411,766,561,244,293,755,329,559,137,793,243,97,423,821,800,715,303,798,927 +97,572,539,517,208,301,812,760,91,882,220,132,677,673,892,559,57,670,615,295 +791,800,759,117,122,985,588,172,949,692,277,568,110,115,539,848,168,287,563,416 +210,843,503,89,786,141,827,815,330,946,397,102,148,527,458,317,55,99,726,653 +524,671,330,260,865,277,148,118,672,750,678,354,131,540,936,269,545,740,906,841 +628,798,908,843,310,798,750,11,247,479,401,101,799,283,94,785,108,314,800,444 +419,640,781,319,269,259,541,760,730,739,112,322,625,502,847,759,399,244,493,949 +504,899,417,644,172,468,565,103,500,356,822,92,274,461,893,616,126,693,782,355 +272,935,806,420,217,277,72,220,890,749,767,752,168,884,211,392,427,120,305,733 +107,835,287,589,457,735,279,930,840,693,530,397,476,222,525,168,78,754,395,900 +122,907,531,769,293,505,720,809,723,757,771,930,249,241,522,533,649,366,892,505 +526,929,815,100,319,976,122,588,130,105,290,123,279,854,468,499,533,823,521,533 +619,402,444,907,500,823,727,277,458,590,2,759,933,94,758,304,748,850,526,730 +678,213,474,101,800,813,581,175,245,740,108,306,92,899,127,574,763,841,96,262 +637,247,225,577,327,270,819,272,329,501,417,266,298,292,288,181,280,272,759,854 +367,265,838,934,277,825,867,461,822,441,575,51,284,408,61,410,942,898,748,644 +355,836,325,756,679,260,744,930,728,414,193,542,303,928,891,114,933,179,250,733 +200,765,799,820,418,838,728,808,939,264,716,293,687,644,536,330,217,57,424,178 +821,273,906,881,812,777,170,474,747,110,652,763,880,216,615,687,325,938,755,542 +850,260,396,445,318,299,677,280,907,58,214,809,501,849,477,327,640,78,135,881 +366,97,51,888,842,576,800,466,136,209,286,255,447,271,901,416,399,220,501,98 +627,939,524,845,685,616,481,62,400,55,400,443,853,301,294,398,52,881,694,442 +148,368,879,308,891,486,60,60,127,513,132,892,145,102,451,518,247,934,719,849 +723,692,745,716,715,149,171,243,739,620,790,494,608,462,424,741,836,846,733,573 +909,111,899,457,214,748,412,822,800,591,275,441,102,745,512,144,421,639,99,318 +456,755,356,907,259,222,208,136,900,623,733,460,334,116,892,95,841,734,446,284 +61,95,272,813,475,927,615,983,508,250,826,810,172,528,269,688,420,92,262,260 +713,738,212,441,535,621,695,678,582,123,674,423,939,419,486,223,105,321,109,766 +212,893,297,524,50,323,276,941,120,717,270,175,447,553,803,445,314,117,134,935 +884,281,98,429,415,329,490,86,111,90,689,128,510,50,245,130,727,217,293,848 +401,150,329,674,304,57,813,885,647,59,240,472,856,805,419,895,508,749,649,99 +246,413,321,741,629,462,906,937,222,129,651,624,306,295,943,682,150,520,349,298 +464,543,903,474,730,111,632,225,591,9,58,685,902,300,398,463,504,887,460,669 +603,620,104,683,792,793,423,124,850,126,840,311,889,785,323,115,692,904,177,834 +426,527,405,104,800,650,943,800,260,490,237,148,475,403,904,296,508,125,310,893 +444,522,268,214,533,260,217,891,527,615,981,498,180,451,356,278,457,844,626,810 +246,838,509,850,843,618,62,309,410,279,819,825,19,520,91,588,618,282,532,669 +933,503,685,760,800,725,940,108,89,400,216,388,852,848,811,561,632,761,824,944 +827,448,172,564,119,879,514,97,803,522,286,164,879,122,297,740,843,788,762,614 +927,168,740,52,413,879,230,854,719,734,724,214,817,844,640,645,898,530,890,170 +732,250,539,288,766,561,212,288,621,324,742,606,413,888,737,836,150,886,743,769 +293,217,104,680,404,498,623,917,413,330,842,943,123,629,91,741,450,262,746,799 +474,838,295,746,379,442,306,768,494,104,488,755,172,935,94,944,58,897,148,291 +8,323,623,110,266,267,450,495,648,716,323,668,633,393,446,220,217,905,902,327 +787,789,96,94,785,448,421,719,622,908,890,469,779,134,618,305,180,539,689,270 +122,530,209,562,945,312,811,109,281,398,827,2,637,806,811,681,836,848,129,325 +104,288,751,561,643,629,248,295,315,220,355,392,56,916,312,425,108,804,356,322 +530,125,201,271,588,369,472,50,266,575,841,508,807,423,137,415,295,760,516,765 +279,366,261,734,884,766,942,217,772,57,815,836,681,322,898,399,882,840,632,732 +911,176,718,132,931,690,785,755,539,498,54,614,730,454,892,816,680,422,472,215 +798,504,809,314,771,222,403,398,108,539,276,754,121,487,651,839,896,468,168,645 +851,123,52,173,678,619,401,622,617,57,20,129,316,475,131,590,687,828,852,303 +835,645,618,113,496,422,792,945,394,836,881,588,524,509,883,522,821,986,61,259 +216,736,539,464,258,487,745,242,143,115,677,650,763,785,641,451,591,678,884,577 +791,682,757,103,354,573,810,367,803,669,150,535,443,315,170,474,15,270,806,130 +289,325,719,322,269,576,684,294,281,242,567,119,209,853,894,516,20,452,355,947 +562,303,285,445,104,292,888,849,887,589,574,123,53,540,264,411,737,302,81,262 +466,638,770,502,638,93,828,169,465,463,454,747,234,128,132,935,392,756,836,795 +448,288,846,811,546,750,826,478,526,406,314,277,745,722,694,686,265,927,747,802 +811,589,392,883,129,853,684,622,264,753,838,833,423,57,109,788,645,319,803,243 +814,532,530,762,724,518,640,427,111,505,949,657,798,451,494,446,473,215,644,319 +668,410,748,462,267,529,826,426,805,540,747,547,448,836,637,901,760,292,53,309 +116,790,412,467,905,444,750,55,646,879,217,478,583,684,685,722,728,747,836,940 +767,623,307,331,732,669,795,536,631,90,909,86,729,127,885,795,271,446,101,574 +60,813,280,446,279,52,204,276,109,724,947,683,291,799,423,308,790,802,849,939 +826,461,821,397,457,669,61,310,167,690,574,789,317,931,805,937,418,496,940,853 +282,940,502,199,726,412,673,896,897,761,887,367,127,321,637,570,244,59,173,846 +545,639,667,885,300,741,262,478,687,930,355,322,214,811,408,106,59,180,888,899 +16,179,564,119,498,675,453,815,150,573,222,442,461,61,356,455,846,544,460,804 +539,275,941,129,801,102,895,278,984,458,575,637,506,135,720,763,712,472,297,569 +725,392,886,414,570,314,3,572,477,805,97,319,282,169,617,392,223,901,647,119 +930,243,807,948,499,403,835,564,789,648,52,731,619,83,448,428,280,632,570,894 +214,804,321,818,111,298,271,770,412,845,242,636,508,494,112,297,921,824,491,889 +806,740,827,465,92,418,740,463,315,641,356,796,177,928,491,409,899,652,139,244 +809,836,724,461,201,212,471,498,723,933,563,542,532,308,267,692,317,531,461,327 +373,527,819,247,479,135,175,718,681,892,463,949,123,848,674,464,404,497,241,809 +651,543,798,734,272,420,651,290,265,535,109,391,394,535,310,671,392,143,172,569 +61,534,894,421,800,570,568,761,729,272,217,923,623,672,682,408,827,309,329,719 +677,787,926,838,412,538,803,847,405,265,122,395,111,421,631,770,577,94,57,645 +506,129,888,450,218,476,712,571,447,61,822,177,728,758,693,486,894,601,630,890 +400,107,935,637,450,838,170,769,367,519,883,442,120,242,897,992,94,261,790,927 +642,135,277,747,128,103,104,764,575,116,886,481,322,416,823,806,168,544,418,99 +816,810,302,414,806,297,743,827,246,522,324,880,405,142,208,173,89,504,820,175 +141,394,893,134,403,409,890,395,684,693,497,279,685,565,533,626,839,304,838,393 +717,614,884,485,900,504,555,243,466,897,400,751,512,427,939,367,490,466,837,714 +832,282,211,636,770,652,108,467,268,760,755,880,727,312,89,932,714,732,406,506 +891,282,175,174,813,195,116,638,303,649,895,442,450,792,817,806,517,884,476,57 +249,789,570,527,314,689,327,549,137,678,99,244,791,887,740,938,274,645,125,525 +497,243,786,816,538,52,811,217,258,743,515,650,810,235,208,250,571,305,288,626 +684,849,406,179,820,485,468,839,790,907,265,63,509,658,122,316,628,686,501,50 +726,807,691,441,400,259,891,605,528,943,458,302,635,176,797,128,589,278,882,297 +687,406,247,889,461,677,95,111,475,92,683,510,202,264,724,214,726,765,329,460 +265,526,572,442,845,854,4,242,948,525,326,452,355,767,932,322,441,219,304,894 +452,648,63,642,627,976,95,671,631,245,468,124,401,891,841,289,635,496,519,500 +400,147,411,106,839,536,642,305,929,177,267,843,804,310,215,652,394,730,573,424 +526,809,851,945,768,940,724,895,395,526,713,824,558,293,488,515,468,216,676,516 +511,121,221,404,149,568,499,675,329,884,691,897,800,258,305,2,890,296,395,521 +769,444,316,993,288,794,933,62,105,63,760,900,129,734,568,248,496,888,941,901 +639,175,497,56,283,462,306,541,743,544,175,124,209,9,688,493,247,468,571,688 +175,526,453,571,287,57,283,613,805,510,418,148,933,294,189,532,676,302,281,810 +97,311,90,531,731,320,935,522,290,489,789,845,927,424,502,243,16,933,455,391 +177,452,521,843,675,651,752,695,737,774,892,852,284,898,810,799,223,811,889,689 +412,471,724,768,652,397,60,712,678,323,939,936,679,19,90,751,801,810,331,415 +222,243,51,757,623,310,523,229,298,811,303,812,624,732,731,298,848,128,420,329 +647,790,908,947,849,169,134,121,329,746,767,508,209,904,725,735,694,551,471,904 +536,249,897,148,890,404,822,782,625,562,729,804,299,569,455,407,369,618,451,54 +590,692,310,693,719,591,759,932,934,685,490,579,59,301,500,312,786,931,499,722 +813,719,725,617,405,302,350,682,168,513,315,828,713,545,110,462,614,444,528,538 +225,840,451,115,574,314,727,465,90,948,308,784,539,118,543,304,613,849,851,220 +80,111,736,289,792,717,937,394,416,460,486,813,527,247,101,169,496,882,453,623 +525,647,120,879,750,803,728,318,395,927,542,822,312,445,174,62,745,265,190,487 +460,277,800,825,13,491,572,681,850,149,854,520,712,110,133,98,454,909,615,105 +303,93,763,452,213,222,423,621,367,828,590,329,793,768,524,639,389,948,826,477 +519,904,225,394,93,516,507,544,214,817,393,170,402,492,739,989,134,105,536,766 +754,896,528,315,247,527,207,625,572,399,283,94,570,192,55,134,520,109,209,479 +791,683,738,101,763,644,568,313,873,523,637,293,110,280,278,413,935,99,943,927 +51,813,480,136,215,217,214,502,299,684,644,928,614,369,219,805,505,127,524,179 +835,179,571,565,825,221,771,409,404,473,223,752,494,724,453,731,613,324,515,818 +836,218,179,529,397,534,534,56,480,326,446,945,122,648,625,414,273,572,804,315 +329,321,314,110,850,300,451,471,830,57,931,809,219,834,61,907,614,367,939,692 +933,695,214,891,726,588,494,454,379,767,170,503,617,792,691,396,494,621,752,241 +537,837,948,721,369,95,271,590,170,427,759,899,723,313,271,908,168,372,464,906 +506,935,227,149,425,676,305,294,891,121,803,477,949,326,673,822,497,814,397,615 +207,306,111,881,269,356,941,686,684,211,652,644,569,313,848,932,757,61,602,739 +474,263,941,532,239,742,58,766,327,673,249,392,671,674,491,899,446,826,751,537 +536,445,393,881,931,455,935,746,221,852,532,572,470,497,691,532,554,647,819,623 +131,293,221,216,938,617,543,8,740,471,717,212,451,949,726,50,564,479,215,212 +691,354,307,283,757,713,57,89,368,407,92,939,450,838,273,509,533,172,921,631 +500,114,897,929,516,720,929,504,172,948,500,739,219,175,526,985,447,644,737,471 +246,802,69,328,475,733,485,889,429,149,148,60,627,241,470,283,719,571,614,240 +791,527,766,735,900,118,355,317,181,765,509,638,880,879,838,689,52,897,266,175 +885,540,565,133,982,802,643,525,677,510,946,209,392,309,822,450,808,575,177,303 +747,541,537,413,576,686,179,756,691,416,400,307,793,485,749,577,322,247,228,801 +716,889,507,884,311,748,421,542,341,838,752,792,292,308,498,792,755,692,510,904 +442,498,940,768,938,838,621,368,909,499,681,449,381,722,265,534,454,297,852,645 +753,566,121,714,213,325,434,421,453,307,899,739,814,789,458,845,109,529,629,406 +211,845,172,571,100,223,885,142,306,613,427,630,101,688,221,753,518,806,940,647 +443,938,99,793,474,271,653,815,309,240,382,215,407,835,640,930,630,277,733,265 +474,133,747,296,135,122,524,249,125,191,531,641,489,96,51,50,638,799,734,645 +356,369,827,508,688,331,789,538,509,712,493,495,917,847,249,421,886,469,415,108 +263,219,750,754,418,517,493,464,261,989,935,575,769,797,943,268,770,882,746,323 +837,688,134,418,427,421,485,392,276,645,478,866,272,743,689,640,217,420,682,685 +890,725,110,736,749,209,413,216,510,490,850,936,764,377,723,523,317,354,528,419 +942,884,832,295,63,744,927,315,590,504,298,247,695,734,758,692,355,716,693,472 +300,395,610,408,883,279,320,623,545,897,511,760,626,721,834,266,55,891,732,215 +463,115,524,814,62,474,411,945,530,638,912,287,695,400,459,677,798,278,423,470 +928,91,447,320,446,472,209,723,668,10,827,261,518,946,244,633,116,304,901,813 +847,264,692,797,260,614,938,248,946,591,220,793,730,778,462,621,274,224,800,445 +401,191,763,563,760,689,544,424,406,715,674,117,170,478,891,891,493,220,121,736 +733,936,749,821,424,287,456,823,834,443,840,826,82,469,770,122,150,58,530,573 +898,794,478,746,92,511,295,621,242,797,309,235,297,741,669,943,639,561,627,931 +644,564,843,296,281,619,260,811,767,120,537,519,258,731,631,168,802,247,224,4 +324,317,243,395,250,99,424,800,787,134,220,683,848,756,848,488,929,421,547,748 +840,854,588,89,101,106,465,442,650,269,534,278,499,97,101,781,908,730,485,125 +750,517,56,807,299,99,992,454,631,107,398,150,115,649,293,748,882,124,817,527 +467,766,752,545,516,516,428,172,459,295,442,509,250,325,517,149,297,234,269,319 +329,265,294,642,83,573,456,128,491,241,289,297,570,634,797,509,818,791,590,453 +418,301,313,904,468,489,449,55,674,674,322,883,822,829,751,298,574,712,457,500 +909,688,279,96,121,837,420,671,127,575,628,724,572,734,140,763,426,411,837,412 +651,943,856,89,171,419,446,309,355,570,225,320,222,367,794,250,885,619,535,730 +577,634,673,130,626,616,543,302,591,804,107,634,789,746,107,628,786,899,271,15 +304,569,180,939,452,295,297,52,667,420,487,946,323,506,725,676,804,315,687,63 +131,450,416,132,723,894,787,930,577,15,52,319,898,714,303,259,744,318,627,733 +803,670,272,939,854,651,469,565,285,519,393,323,817,95,741,293,996,259,216,590 +223,755,323,723,823,279,733,424,653,810,297,328,705,617,441,414,318,818,653,834 +413,355,526,259,686,301,377,639,737,413,812,455,96,114,940,615,467,571,150,880 +174,589,138,446,288,331,323,630,214,522,503,170,149,114,718,129,844,898,102,928 +530,302,516,731,60,931,486,391,88,945,536,691,208,224,392,642,737,497,493,736 +272,544,277,840,496,614,562,940,900,893,128,534,881,507,404,631,470,499,484,276 +466,240,266,727,946,179,89,821,609,809,714,424,315,295,727,907,852,537,818,688 +643,916,57,694,714,693,822,478,647,749,461,330,354,210,427,839,632,459,507,852 +902,517,127,310,145,684,179,947,415,302,213,261,501,789,59,617,269,500,452,694 +392,273,565,942,947,98,443,480,499,719,791,51,521,746,691,511,788,577,356,209 +461,671,547,308,248,318,100,930,570,689,722,92,497,445,685,301,495,454,133,279 +674,118,796,177,91,715,879,258,244,643,824,758,52,944,776,932,487,329,577,212 +274,309,821,822,949,93,224,268,242,843,116,616,295,565,451,942,415,825,658,841 +457,903,931,471,844,988,213,618,284,110,318,631,466,127,304,533,906,763,122,246 +472,618,942,842,941,725,520,797,671,817,64,406,620,896,532,941,626,852,442,896 +92,475,764,112,128,101,729,573,724,8,491,220,327,846,568,290,892,314,929,630 +544,101,122,591,59,278,446,224,305,849,680,332,118,809,509,320,668,492,800,742 +108,61,486,486,479,847,323,374,271,527,60,618,94,511,488,679,792,570,511,759 +112,500,461,490,615,102,454,470,259,315,105,623,271,314,627,213,782,751,331,214 +879,894,298,515,290,476,55,516,223,172,119,89,732,105,628,206,813,776,802,52 +228,124,613,893,269,619,845,501,269,905,273,443,676,933,394,620,627,112,543,630 +540,802,649,486,98,641,652,737,320,824,175,803,734,795,567,631,678,21,277,494 +308,131,395,63,295,63,744,720,623,565,673,262,834,722,212,617,144,519,542,718 +328,800,491,496,453,305,91,274,418,278,731,289,440,671,812,266,316,240,815,788 +933,249,850,392,897,571,914,515,760,881,647,448,766,753,427,731,476,502,330,531 +271,488,61,458,911,328,668,842,744,499,279,314,446,743,302,590,822,476,676,591 +280,527,89,246,527,130,563,840,499,52,59,406,320,936,249,904,184,932,308,752 +929,150,686,176,593,649,137,108,692,805,797,135,215,93,460,934,880,463,398,676 +618,271,396,641,823,651,745,523,102,813,497,722,988,517,218,944,728,108,475,393 +677,515,538,296,928,753,811,172,400,691,405,567,492,277,567,376,885,509,844,835 +53,419,679,93,470,314,460,285,882,751,512,247,936,846,897,944,443,915,303,845 +447,135,723,834,456,172,883,767,403,489,695,712,694,940,935,366,298,901,12,899 +305,270,586,534,689,505,133,758,443,818,513,741,938,276,118,469,220,261,673,568 +815,302,178,217,457,2,447,678,311,285,529,807,948,106,491,218,715,178,714,540 +452,820,735,216,297,259,888,928,673,241,541,269,298,119,408,900,787,802,830,651 +180,330,720,122,262,391,138,454,249,501,930,394,311,308,635,821,932,790,686,442 +400,739,221,978,946,366,736,680,758,907,131,422,98,307,879,673,281,853,793,309 +290,501,417,51,100,885,908,755,286,400,643,441,323,282,651,881,812,392,380,804 +415,901,129,468,802,880,531,618,232,312,113,122,522,429,171,127,429,520,135,835 +560,653,282,903,283,887,258,577,180,790,508,805,669,264,454,291,471,903,275,58 +631,126,565,306,537,422,470,896,514,948,307,528,684,859,458,319,310,879,886,634 +847,476,270,638,170,681,672,871,306,265,568,100,674,354,293,941,286,319,725,615 +754,564,815,943,743,490,518,648,732,415,508,185,641,885,103,945,369,476,802,633 +496,796,747,395,693,245,770,931,818,177,146,94,891,522,797,883,524,319,515,677 +120,750,531,294,756,401,678,268,915,116,247,494,273,454,751,797,721,280,807,812 +469,637,840,763,616,443,793,397,463,285,768,900,650,607,224,614,319,571,54,136 +313,466,516,473,561,884,501,108,903,690,619,674,521,590,149,645,885,241,875,278 +284,290,352,366,750,565,588,840,91,291,622,742,902,631,272,928,278,410,766,113 +653,642,380,50,285,399,573,805,508,544,786,305,443,93,567,486,887,313,767,637 +773,443,884,574,731,788,170,885,678,57,850,225,571,591,277,445,117,713,421,51 +414,901,222,468,630,671,312,109,137,214,731,478,163,899,369,124,545,127,131,260 +981,411,223,893,884,849,243,802,785,897,112,590,941,934,471,500,130,295,177,289 +732,464,281,98,806,56,604,466,94,327,476,589,794,330,714,847,395,57,97,278 +630,686,428,769,53,753,830,422,881,489,909,490,650,179,447,785,292,420,849,853 +115,844,136,539,807,262,535,939,109,121,759,144,446,895,814,220,478,619,854,422 +544,308,209,944,413,732,322,932,937,300,408,737,480,309,262,809,123,787,474,328 +170,57,908,538,766,496,935,815,470,177,720,136,830,295,534,815,63,174,627,310 +693,613,814,281,148,907,848,326,479,517,690,978,412,789,820,127,838,882,175,639 +245,786,686,274,91,815,259,63,880,646,745,246,252,817,220,115,565,297,216,123 +458,400,620,292,948,949,492,851,622,428,55,242,657,106,278,933,446,882,55,301 +895,529,753,470,102,55,157,643,221,727,443,682,411,807,296,448,673,246,89,89 +208,938,62,316,561,944,240,197,715,421,519,417,414,644,219,613,576,619,445,767 +320,428,213,535,475,939,842,676,508,785,281,300,356,482,368,302,741,52,149,470 +744,106,261,292,57,547,457,642,516,302,113,633,945,396,718,569,717,355,112,240 +940,265,787,752,299,118,54,356,807,901,89,175,907,290,790,53,589,890,18,804 +844,776,879,948,896,511,122,169,653,467,636,838,569,209,307,114,261,723,542,521 +415,54,814,806,461,652,661,626,730,446,668,310,653,490,103,812,91,91,879,844 +119,457,222,687,479,827,444,529,417,627,547,312,311,826,683,62,568,406,326,848 +591,319,492,280,644,246,293,245,309,879,494,439,676,446,115,136,213,936,127,511 diff --git a/aoc2020/input/day16_provided.txt b/aoc2020/input/day16_provided.txt new file mode 100644 index 0000000..fe34e40 --- /dev/null +++ b/aoc2020/input/day16_provided.txt @@ -0,0 +1,12 @@ +class: 1-3 or 5-7 +row: 6-11 or 33-44 +seat: 13-40 or 45-50 + +your ticket: +7,1,14 + +nearby tickets: +7,3,47 +40,4,50 +55,2,20 +38,6,12 diff --git a/aoc2020/src/day16.rs b/aoc2020/src/day16.rs new file mode 100644 index 0000000..ab65d65 --- /dev/null +++ b/aoc2020/src/day16.rs @@ -0,0 +1,137 @@ +use std::fmt::Write; +use std::ops::RangeInclusive; + +use anyhow::{Context, Result}; + +const INPUT: &str = include_str!("../input/day16.txt"); + +pub fn run() -> Result { + let mut res = String::with_capacity(128); + + writeln!(res, "part 1: {}", part1(INPUT)?)?; + + Ok(res) +} + +fn part1(input: &str) -> Result { + let (fields, _, tickets) = parse_input(input)?; + + Ok(tickets + .iter() + .map(|t| t.invalid_values(&fields)) + .flatten() + .sum()) +} + +fn parse_input(input: &str) -> Result<(Vec, Ticket, Vec)> { + let mut parts = input.split("\n\n"); + + let fields_part = parts.next().context("no fields specification found")?; + let my_ticket_part = parts.next().context("no personal ticket found")?; + let tickets_part = parts.next().context("no list of tickets found")?; + + let fields = fields_part + .lines() + .map(|line| line.parse()) + .collect::>() + .context("couldn't parse fields")?; + let my_ticket = my_ticket_part + .lines() + .skip(1) + .map(|line| line.parse()) + .next() + .context("no second line for ticket")??; + let tickets = tickets_part + .lines() + .skip(1) + .map(|line| line.parse()) + .collect::>() + .context("couldn't parse tickets")?; + + Ok((fields, my_ticket, tickets)) +} + +#[derive(Debug)] +struct Ticket { + values: Vec, +} + +impl Ticket { + fn invalid_values(&self, fields: &[Field]) -> Vec { + self.values + .iter() + .filter(|val| !fields.iter().any(|f| f.contains(*val))) + .copied() + .collect() + } +} + +impl std::str::FromStr for Ticket { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + let values = s + .split(',') + .map(|num| { + num.parse() + .with_context(|| format!("couldn't parse `{}`", num)) + }) + .collect::>()?; + + Ok(Ticket { values }) + } +} + +#[derive(Debug)] +struct Field { + name: String, + ranges: (RangeInclusive, RangeInclusive), +} + +impl Field { + fn contains(&self, val: &u64) -> bool { + self.ranges.0.contains(val) || self.ranges.1.contains(val) + } +} + +impl std::str::FromStr for Field { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + let mut parts = s.split(": "); + + let name = parts.next().context("no name found")?.to_string(); + + let mut ranges = parts.next().context("no ranges found")?.split(" or "); + + let mut range1 = ranges.next().context("no first range found")?.split('-'); + let range1_start = range1.next().context("no bound for range")?.parse()?; + let range1_end = range1.next().context("no bound for range")?.parse()?; + + let mut range2 = ranges.next().context("no second range found")?.split('-'); + let range2_start = range2.next().context("no bound for range")?.parse()?; + let range2_end = range2.next().context("no bound for range")?.parse()?; + + Ok(Field { + name, + ranges: (range1_start..=range1_end, range2_start..=range2_end), + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const PROVIDED: &str = include_str!("../input/day16_provided.txt"); + + #[test] + fn part1_provided() { + assert_eq!(part1(PROVIDED).unwrap(), 71); + } + + #[test] + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 20013); + } +} diff --git a/aoc2020/src/lib.rs b/aoc2020/src/lib.rs index c4cadc0..3ef96aa 100644 --- a/aoc2020/src/lib.rs +++ b/aoc2020/src/lib.rs @@ -13,3 +13,4 @@ pub mod day12; pub mod day13; pub mod day14; pub mod day15; +pub mod day16; diff --git a/aoc2020/src/main.rs b/aoc2020/src/main.rs index 936890e..c73812f 100644 --- a/aoc2020/src/main.rs +++ b/aoc2020/src/main.rs @@ -17,6 +17,7 @@ use aoc2020::day12; use aoc2020::day13; use aoc2020::day14; use aoc2020::day15; +use aoc2020::day16; fn main() -> Result<()> { let days: &[DayFunc] = &[ @@ -35,6 +36,7 @@ fn main() -> Result<()> { day13::run, day14::run, day15::run, + day16::run, ]; aoc::run(days)