diff --git a/Cargo.toml b/Cargo.toml index 14bfb68..6044e5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,3 +50,7 @@ path = "src/day11.rs" [[bin]] name = "day12" path = "src/day12.rs" + +[[bin]] +name = "day13" +path = "src/day13.rs" diff --git a/inputs/day13.txt b/inputs/day13.txt new file mode 100644 index 0000000..227c901 --- /dev/null +++ b/inputs/day13.txt @@ -0,0 +1,804 @@ +95,196 +500,420 +681,633 +31,581 +694,348 +438,824 +408,555 +1277,380 +472,129 +80,779 +1290,159 +823,626 +1191,668 +1005,334 +676,742 +1191,500 +264,658 +1238,756 +192,201 +241,673 +366,236 +354,518 +1255,691 +567,511 +703,102 +816,660 +1128,183 +647,238 +319,348 +1173,434 +805,665 +1131,362 +681,806 +129,178 +974,329 +927,546 +743,442 +537,33 +1205,544 +1243,868 +264,276 +621,604 +355,140 +396,452 +694,546 +169,159 +373,36 +991,686 +422,872 +1208,574 +1081,728 +470,291 +587,26 +731,868 +505,82 +1164,598 +1022,236 +334,3 +371,838 +3,402 +991,432 +226,459 +323,268 +1303,656 +408,885 +507,876 +629,485 +592,658 +105,350 +1141,733 +105,110 +470,63 +1279,49 +689,634 +1203,333 +323,178 +43,672 +694,98 +493,380 +1113,44 +894,320 +159,385 +333,698 +792,366 +982,428 +674,61 +487,11 +980,506 +718,658 +987,64 +835,140 +246,488 +792,528 +791,868 +792,864 +957,826 +718,210 +283,0 +1148,786 +1027,166 +1104,147 +984,681 +803,707 +364,215 +415,190 +87,511 +897,353 +628,376 +552,11 +487,152 +167,298 +1275,642 +659,600 +710,740 +731,623 +1203,561 +1170,166 +1036,33 +320,3 +32,721 +920,724 +700,33 +813,161 +33,260 +222,72 +334,891 +956,854 +689,311 +218,96 +151,296 +1046,434 +924,744 +1113,472 +912,513 +164,213 +1039,427 +483,432 +21,702 +907,847 +35,642 +681,460 +990,891 +661,250 +175,150 +1248,771 +234,211 +487,600 +1230,794 +415,638 +554,52 +517,766 +835,466 +179,532 +659,742 +681,261 +842,614 +827,880 +74,259 +495,807 +1266,84 +487,61 +1071,480 +682,376 +105,558 +731,26 +288,658 +721,863 +853,828 +572,488 +1151,61 +917,415 +1084,50 +827,462 +26,201 +7,238 +689,310 +229,280 +125,399 +1250,291 +447,674 +838,129 +179,588 +323,492 +927,427 +159,197 +927,318 +1089,126 +1163,187 +472,765 +1183,372 +55,691 +1150,10 +85,550 +63,150 +607,558 +155,98 +1078,213 +336,631 +492,108 +1064,885 +1036,714 +102,883 +1235,868 +536,33 +791,26 +651,152 +1151,659 +238,574 +504,39 +938,215 +1046,276 +815,87 +1058,411 +1290,600 +497,285 +542,344 +146,598 +577,438 +703,571 +987,659 +874,96 +146,856 +528,161 +561,856 +818,876 +300,210 +505,665 +267,590 +306,742 +577,736 +1121,688 +793,882 +179,259 +1275,638 +127,397 +1022,348 +1151,309 +971,864 +26,357 +877,560 +308,114 +1225,669 +361,534 +813,285 +147,466 +803,428 +1029,410 +196,362 +984,817 +840,63 +514,728 +1298,87 +293,492 +600,154 +271,585 +536,609 +45,726 +935,683 +939,0 +864,493 +189,688 +964,711 +711,875 +231,644 +79,402 +823,61 +629,261 +417,668 +1282,614 +816,100 +803,635 +1275,229 +962,742 +1166,294 +75,666 +733,886 +878,822 +364,679 +1285,429 +110,824 +774,705 +294,721 +279,344 +464,264 +1109,847 +267,486 +1091,266 +20,735 +1011,866 +888,479 +1205,110 +26,89 +105,544 +822,234 +497,161 +31,357 +463,103 +537,484 +279,550 +525,780 +232,661 +443,883 +20,607 +1278,621 +386,296 +197,472 +957,460 +246,339 +267,366 +1260,438 +1099,435 +305,334 +644,582 +238,747 +311,458 +1174,366 +1082,394 +1073,500 +75,175 +1120,264 +902,324 +77,606 +741,344 +323,626 +733,438 +201,847 +756,883 +8,777 +610,455 +1251,38 +507,259 +768,550 +967,716 +980,10 +32,621 +272,126 +299,866 +1175,474 +1272,595 +264,618 +927,602 +281,278 +800,394 +1026,249 +159,61 +552,834 +621,220 +422,479 +977,196 +1292,562 +600,740 +810,420 +264,434 +323,659 +1002,780 +602,852 +1027,894 +417,21 +621,584 +1295,803 +472,487 +976,891 +793,12 +89,674 +1081,166 +736,884 +140,614 +537,861 +939,838 +1141,159 +1072,299 +572,739 +219,628 +1099,11 +383,467 +572,155 +808,700 +798,735 +1078,479 +552,399 +349,560 +197,102 +1028,663 +140,280 +514,838 +359,805 +169,733 +610,138 +62,236 +1275,256 +1208,847 +642,87 +167,294 +117,96 +182,711 +1166,855 +206,299 +674,71 +974,397 +499,572 +408,100 +1135,150 +863,614 +288,819 +110,768 +382,96 +646,777 +708,42 +169,161 +992,117 +499,98 +150,71 +18,52 +1143,509 +1113,828 +293,422 +383,322 +125,847 +807,150 +335,546 +186,280 +383,602 +1134,199 +1134,695 +735,514 +403,847 +135,252 +554,842 +35,126 +832,487 +274,180 +422,239 +21,192 +294,49 +388,689 +226,50 +890,435 +1031,344 +1113,850 +1002,375 +555,710 +1284,133 +951,537 +949,534 +1285,485 +294,621 +415,862 +838,173 +1110,187 +182,183 +211,446 +927,539 +961,192 +1198,886 +1126,63 +1042,598 +517,576 +766,481 +1210,724 +251,271 +835,428 +1027,614 +708,33 +1307,95 +89,425 +336,117 +984,77 +1284,805 +105,784 +646,845 +75,868 +773,120 +1046,771 +85,102 +100,571 +835,259 +344,374 +1173,460 +1298,15 +311,436 +44,33 +472,686 +815,61 +300,854 +579,175 +1084,396 +392,394 +465,688 +1233,158 +691,574 +823,152 +967,268 +793,409 +949,583 +495,659 +715,408 +498,227 +1275,252 +967,436 +918,500 +290,679 +848,693 +159,509 +636,610 +211,435 +232,479 +238,320 +112,120 +1305,196 +827,14 +611,670 +738,406 +457,850 +823,742 +2,212 +629,633 +805,82 +1233,456 +371,614 +331,659 +847,103 +336,329 +703,558 +1118,425 +396,4 +35,574 +972,203 +1143,298 +1225,792 +371,504 +507,466 +1043,486 +445,33 +750,470 +160,122 +966,520 +408,324 +283,614 +812,227 +472,407 +718,877 +217,94 +589,165 +634,287 +197,344 +282,663 +907,495 +986,234 +336,341 +699,224 +602,42 +691,480 +1113,344 +1221,674 +343,763 +387,366 +957,68 +619,344 +306,511 +457,828 +1061,886 +1181,178 +1146,213 +504,743 +93,856 +112,8 +353,429 +1255,796 +982,681 +457,8 +420,556 +1233,509 +939,446 +1290,63 +924,150 +1285,409 +544,126 +380,628 +136,366 +196,810 +536,717 +687,89 +1052,4 +806,599 +470,831 +633,558 +293,583 +1038,126 +838,721 +599,1 +494,660 +1284,537 +417,469 +758,462 +383,427 +619,32 +927,322 +422,22 +92,883 +237,873 +1198,774 +1093,94 +211,459 +582,138 +1300,243 +102,856 +846,21 +7,656 +318,777 +567,442 +1307,229 +927,501 +390,170 +661,644 +465,651 +647,656 +493,514 +627,28 +89,469 +1089,320 +1017,135 +27,530 +967,10 +35,256 +1039,124 +820,892 +847,791 +636,61 +1223,511 +574,772 +691,677 +636,833 +1128,711 +689,738 +514,504 +65,367 +927,585 +700,756 +1160,599 +1277,514 +865,33 +636,562 +629,409 +1174,30 +492,18 +1282,147 +893,226 +1284,201 +447,280 +1208,856 +355,306 +10,651 +60,603 +504,855 +488,436 +398,625 +863,728 +258,890 +857,47 +33,514 +974,631 +238,443 +264,771 +423,250 +758,11 +293,759 +982,466 +992,777 +495,665 +519,26 +55,484 +197,646 +629,434 +783,820 +1073,469 +354,854 +775,730 +504,599 +1263,344 +432,822 +433,560 +319,208 +1208,435 +1017,402 +1183,397 +1027,418 +239,480 +495,742 +565,668 +1005,478 +905,61 +623,537 +902,772 +651,809 +966,374 +1091,355 +346,780 +1079,250 +535,730 +800,500 +127,497 +1078,22 +1010,684 +1114,362 +241,120 +462,537 +408,122 +1113,8 +494,100 +709,352 +241,497 +701,168 +922,607 +920,276 +127,845 +44,84 +371,418 +827,432 +1072,707 +838,487 +293,135 +873,878 +237,425 +72,756 +1139,14 +1012,299 +855,596 +621,634 +1131,84 +1263,323 +773,774 +979,659 +609,168 +420,435 +505,229 +815,833 +1164,856 +917,479 +602,490 +301,68 +179,84 +589,729 +50,456 +554,883 +507,18 +967,178 +510,500 +845,243 +475,428 +1265,278 +1078,661 +544,861 +621,738 +1005,560 +1072,443 +875,355 +18,114 +361,583 +1102,493 +954,280 +221,574 +428,571 +623,89 +1114,84 +749,262 +1267,224 +803,876 +1121,542 +510,208 +1150,548 +321,698 +820,786 +627,812 +1205,784 +92,694 +1307,117 +1192,625 +363,798 +10,203 +216,152 +817,514 +308,375 +378,886 +592,877 +1285,261 +311,716 +335,686 +514,56 +610,756 +1059,26 +774,285 + +fold along x=655 +fold along y=447 +fold along x=327 +fold along y=223 +fold along x=163 +fold along y=111 +fold along x=81 +fold along y=55 +fold along x=40 +fold along y=27 +fold along y=13 +fold along y=6 diff --git a/src/day13.rs b/src/day13.rs new file mode 100644 index 0000000..ebdb40b --- /dev/null +++ b/src/day13.rs @@ -0,0 +1,91 @@ +use std::collections::HashSet; +use std::str::FromStr; + +enum Axis { + X, + Y, +} + +struct FoldInstruction { + axis: Axis, + position: usize, +} + +impl FoldInstruction { + fn wrap(&self, dot: (usize, usize)) -> (usize, usize) { + match self.axis { + Axis::X if dot.0 >= self.position => (2 * self.position - dot.0, dot.1), + Axis::Y if dot.1 >= self.position => (dot.0, 2 * self.position - dot.1), + _ => (dot.0, dot.1), + } + } +} + +impl FromStr for FoldInstruction { + type Err = (); + + fn from_str(s: &str) -> Result { + let (a, p) = s[11..].split_once('=').unwrap(); + let axis = match a { + "x" => Axis::X, + "y" => Axis::Y, + _ => return Err(()), + }; + let position = p.parse().unwrap(); + Ok(FoldInstruction { axis, position }) + } +} + +fn main() { + const INPUT: &str = include_str!("../inputs/day13.txt"); + let sections = INPUT.split_once("\n\n").unwrap(); + let mut dots = sections + .0 + .lines() + .map(|line| { + let (a, b) = line.split_once(',').unwrap(); + (a.parse().unwrap(), b.parse().unwrap()) + }) + .collect::>(); + let instructions = sections + .1 + .lines() + .map(|line| line.parse().unwrap()) + .collect::>(); + + let mut all_dots: HashSet<(usize, usize)> = HashSet::new(); + for dot in dots.iter() { + all_dots.insert(instructions[0].wrap(*dot)); + } + println!("solution {}", all_dots.len()); + + all_dots.drain(); + for instr in instructions { + for dot in dots.iter() { + all_dots.insert(instr.wrap(*dot)); + } + dots = all_dots.drain().collect(); + } + for y in 0..6 { + println!( + "{}", + (0..40) + .map(|x| if dots.contains(&(x, y)) { + '🟪' + } else { + '⬛' + }) + .collect::() + ); + } +} + +#[test] +fn test_wrap_instruction() { + let instruction = FoldInstruction { + axis: Axis::Y, + position: 7, + }; + assert_eq!(instruction.wrap((3, 0)), (3, 0)); + assert_eq!(instruction.wrap((1, 10)), (1, 4)); +}