From 1a427b7a1f416dead02e3c88756bee3fc9d5ad5a Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Tue, 14 Dec 2021 22:07:40 -0800 Subject: [PATCH] Day 13 part 1 --- 2021/input/2021/day13.txt | 910 ++++++++++++++++++++++++++++++++++++++ 2021/src/day13.rs | 244 ++++++++++ 2021/src/lib.rs | 1 + 3 files changed, 1155 insertions(+) create mode 100644 2021/input/2021/day13.txt create mode 100644 2021/src/day13.rs diff --git a/2021/input/2021/day13.txt b/2021/input/2021/day13.txt new file mode 100644 index 0000000..3a90b91 --- /dev/null +++ b/2021/input/2021/day13.txt @@ -0,0 +1,910 @@ +724,201 +596,511 +883,560 +637,47 +869,537 +251,505 +851,618 +980,544 +818,661 +1093,390 +641,95 +1006,621 +447,434 +820,865 +475,847 +788,72 +1066,829 +1303,882 +1165,696 +164,229 +202,333 +244,485 +261,840 +1116,581 +976,513 +550,635 +1151,728 +1211,570 +228,422 +223,539 +406,369 +306,758 +1247,894 +760,859 +826,737 +340,637 +1293,47 +99,187 +264,553 +927,523 +940,189 +586,313 +525,67 +296,581 +353,694 +55,264 +582,491 +264,154 +512,394 +1101,225 +555,187 +1021,535 +805,772 +783,891 +403,402 +1243,94 +495,593 +1031,695 +13,472 +705,637 +459,618 +939,322 +251,82 +899,523 +301,131 +873,891 +880,374 +894,737 +306,593 +1257,280 +88,885 +621,655 +38,640 +514,539 +1222,885 +1257,135 +1191,303 +1034,715 +649,738 +719,103 +1131,367 +907,402 +169,297 +482,408 +977,866 +1280,432 +1087,803 +473,810 +182,546 +785,155 +1257,871 +1091,483 +310,52 +758,712 +870,715 +1211,772 +1173,40 +73,460 +117,312 +228,243 +918,822 +475,50 +765,3 +1113,410 +127,544 +880,422 +291,675 +1088,187 +957,506 +591,791 +566,437 +1178,593 +159,728 +1237,684 +490,665 +929,311 +641,267 +966,766 +1081,253 +735,649 +857,750 +999,367 +989,221 +0,185 +445,395 +108,168 +586,761 +1203,595 +1115,89 +838,665 +487,301 +289,647 +903,133 +251,53 +887,800 +1093,443 +775,684 +1026,481 +601,311 +661,476 +1198,627 +0,753 +1231,786 +410,821 +358,742 +1179,780 +763,435 +1077,133 +927,182 +681,144 +497,597 +1088,483 +649,476 +1202,415 +440,583 +1198,14 +104,775 +619,443 +410,660 +1034,656 +115,575 +601,728 +338,222 +84,301 +1111,891 +1211,122 +653,614 +1290,282 +209,582 +440,43 +929,583 +53,166 +1004,301 +263,572 +818,233 +1183,319 +228,472 +1257,166 +656,329 +53,135 +669,267 +815,593 +1111,767 +802,576 +1226,660 +1215,800 +341,218 +181,422 +1046,565 +159,280 +119,303 +1027,303 +147,417 +571,859 +1173,261 +1054,885 +239,302 +228,131 +85,217 +1231,226 +137,709 +21,131 +1290,612 +718,845 +837,782 +164,141 +632,467 +749,313 +525,379 +858,561 +851,786 +1228,857 +33,239 +868,670 +1183,3 +248,548 +328,345 +898,436 +438,893 +823,301 +820,589 +937,771 +875,58 +790,617 +64,504 +95,675 +508,576 +729,434 +186,91 +1092,392 +403,166 +87,514 +1261,114 +1141,597 +217,390 +689,151 +457,515 +585,805 +1131,360 +1212,472 +669,464 +318,682 +485,73 +1154,644 +738,37 +1297,870 +361,207 +932,238 +880,264 +376,738 +87,380 +174,94 +411,819 +845,633 +1288,721 +189,261 +1294,374 +72,267 +649,642 +156,644 +517,406 +705,562 +1059,614 +813,856 +623,702 +619,462 +403,728 +344,52 +1178,634 +1275,238 +656,47 +1176,169 +244,821 +328,121 +1092,255 +872,1 +845,185 +251,707 +79,674 +659,705 +1048,520 +1121,709 +107,768 +427,334 +1257,311 +112,14 +997,351 +552,429 +1064,159 +1114,453 +398,306 +1208,182 +187,175 +654,719 +1225,665 +735,245 +1078,245 +544,598 +691,238 +1168,164 +264,740 +855,833 +1260,267 +661,156 +333,194 +1212,870 +555,803 +445,11 +1275,443 +1067,35 +783,787 +785,379 +162,141 +661,866 +873,3 +321,476 +1136,800 +673,47 +723,800 +1129,84 +127,891 +927,371 +781,647 +1099,247 +1275,3 +691,891 +218,639 +731,626 +1093,504 +550,735 +1019,880 +477,142 +147,508 +392,822 +542,432 +209,235 +410,234 +895,113 +107,798 +7,23 +119,591 +709,364 +1059,340 +1198,148 +179,360 +226,49 +43,98 +195,640 +452,561 +49,52 +359,761 +1307,749 +1235,392 +556,355 +547,58 +790,725 +8,683 +1261,724 +301,185 +69,89 +545,339 +957,759 +509,84 +1235,564 +663,12 +1017,743 +739,332 +1213,392 +1067,444 +291,616 +969,851 +353,819 +669,95 +902,436 +127,575 +887,143 +671,79 +796,539 +865,11 +289,247 +465,709 +605,724 +194,581 +1223,788 +810,771 +932,208 +333,225 +1308,96 +1257,23 +709,311 +87,123 +845,807 +845,574 +552,630 +1226,234 +16,374 +1247,0 +381,311 +957,11 +393,392 +545,219 +523,332 +820,661 +472,229 +1079,542 +99,570 +311,847 +1130,500 +519,135 +207,434 +435,235 +373,499 +731,368 +800,348 +373,771 +904,861 +687,192 +1297,24 +681,131 +545,555 +705,170 +1009,200 +63,828 +1193,582 +211,647 +982,495 +1206,159 +1014,581 +284,481 +1294,394 +5,156 +641,627 +744,885 +900,212 +557,887 +908,289 +786,856 +785,515 +527,61 +73,684 +698,637 +522,294 +79,226 +843,175 +754,315 +798,606 +25,730 +790,169 +947,686 +509,646 +157,38 +994,451 +31,702 +622,453 +390,742 +256,558 +1274,448 +950,833 +619,238 +723,94 +67,94 +490,383 +447,658 +654,686 +407,313 +1054,9 +982,175 +1215,766 +1198,880 +1054,306 +1305,156 +407,245 +328,208 +273,787 +693,807 +939,751 +232,245 +1047,770 +1063,133 +87,828 +1285,409 +787,803 +1056,715 +654,701 +1201,56 +865,728 +1000,318 +546,31 +671,63 +490,29 +863,658 +234,841 +940,481 +7,460 +1213,495 +411,75 +1280,856 +949,599 +743,106 +301,435 +390,497 +416,157 +411,523 +1057,12 +907,492 +1138,486 +132,301 +383,571 +833,304 +271,323 +788,238 +726,112 +30,432 +1288,313 +688,441 +605,649 +982,345 +174,652 +527,721 +284,257 +927,571 +623,254 +1082,243 +977,669 +472,665 +705,724 +947,768 +559,800 +951,133 +207,460 +279,247 +360,833 +1223,514 +63,0 +182,5 +522,520 +17,495 +555,324 +390,397 +827,882 +107,683 +430,856 +1150,656 +579,788 +769,170 +164,677 +1082,651 +883,894 +982,656 +641,198 +576,320 +863,210 +857,144 +984,549 +672,766 +67,800 +35,443 +412,682 +326,529 +304,621 +556,539 +117,669 +918,72 +654,47 +219,388 +654,753 +1246,56 +654,641 +1250,848 +562,681 +72,403 +1228,305 +641,422 +758,856 +1082,472 +701,256 +1077,761 +820,511 +755,324 +658,509 +960,733 +801,10 +769,378 +430,38 +647,12 +129,291 +201,750 +522,374 +837,422 +75,564 +977,225 +609,256 +855,350 +1128,5 +1245,495 +1240,530 +591,551 +555,154 +45,275 +381,359 +922,253 +1026,257 +251,280 +629,798 +739,371 +492,708 +719,551 +508,318 +678,427 +1109,302 +754,539 +884,472 +705,245 +654,453 +256,530 +1184,793 +79,201 +731,788 +1076,53 +430,264 +999,54 +691,432 +837,810 +669,696 +181,810 +132,520 +885,641 +117,480 +729,460 +437,891 +242,212 +1263,507 +353,75 +316,787 +179,24 +967,815 +490,305 +214,583 +801,646 +1278,441 +1163,392 +485,521 +845,709 +104,159 +276,715 +85,229 +522,145 +214,851 +377,257 +490,661 +1285,485 +278,828 +492,661 +363,686 +709,82 +970,705 +535,882 +256,364 +131,114 +427,635 +440,179 +890,681 +1029,571 +360,61 +78,413 +746,179 +1128,889 +1288,173 +1213,47 +321,700 +137,40 +720,315 +1241,89 +137,807 +99,539 +104,287 +527,891 +415,116 +229,253 +574,605 +276,686 +329,838 +1309,728 +95,667 +899,819 +561,805 +669,430 +1206,735 +1006,313 +231,542 +438,669 +1211,355 +947,880 +783,168 +93,581 +415,302 +195,254 +231,784 +442,670 +872,225 +800,98 +817,58 +1079,94 +341,340 +131,780 +426,315 +1211,826 +1247,707 +363,219 +406,817 +820,383 +465,633 +547,459 +334,605 +1115,254 +927,164 +5,866 +239,778 +97,847 +271,730 +75,392 +438,225 +326,686 +570,301 +564,179 +195,192 +1225,217 +751,367 +477,116 +1116,805 +801,84 +247,674 +189,126 +1101,499 +1161,570 +989,418 +400,491 +810,123 +468,397 +1235,523 +803,607 +247,133 +0,847 +1146,305 +884,315 +164,569 +913,313 +412,212 +112,627 +209,312 +711,523 +427,894 +1235,140 +131,803 +1232,413 +199,891 +49,724 +947,14 +313,351 +898,10 +1277,655 +887,334 +982,719 +1230,829 +316,222 +209,728 +592,845 +1285,803 +490,589 +903,245 +283,591 +1197,516 +197,827 +619,451 +509,884 +1297,43 +1141,73 +1131,63 +584,530 +820,29 +107,836 +251,841 +529,247 +400,715 +607,387 +301,759 +1048,374 +227,707 +281,771 +711,607 +1049,392 +304,313 +1235,626 +209,883 +452,333 +30,408 +403,492 +510,98 +933,68 +161,58 +1242,693 +251,389 +1191,322 +1131,527 +1213,439 +586,201 +904,369 +783,319 +929,359 +838,348 +878,411 +109,443 +1082,131 +350,621 +1048,889 +326,208 +763,96 +132,593 +84,350 +1059,82 +678,467 +1091,836 +1235,371 +582,715 +641,464 +55,795 +293,687 +723,336 +341,472 +371,322 +555,602 +840,500 +199,833 +455,833 +169,73 +97,392 +783,385 +623,640 +1111,840 +765,891 +1046,553 +383,164 +907,812 +1131,870 +1067,892 +1129,472 +246,436 +644,670 +624,38 +353,135 +666,227 +835,399 +112,148 +247,859 +383,99 +535,684 +338,555 +552,805 +594,17 +880,472 +801,436 +997,472 +559,667 +341,676 +247,35 +1193,225 +1,56 +545,3 +1149,58 +455,710 +753,887 +687,640 +1220,379 +1115,724 +1203,574 +465,320 +84,772 +1273,359 +873,339 +783,3 +199,54 +1193,669 +264,329 +246,513 +33,655 +1293,495 +432,187 +601,871 +803,47 +825,373 +259,110 +586,245 +412,436 +927,347 +345,775 +970,257 +870,311 + +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/2021/src/day13.rs b/2021/src/day13.rs new file mode 100644 index 0000000..b025e0f --- /dev/null +++ b/2021/src/day13.rs @@ -0,0 +1,244 @@ +use std::{ + fmt::{Debug, Error, Formatter}, + num::ParseIntError, + ops::{Index, IndexMut}, + str::FromStr, +}; + +use anyhow::Result; +use aoc_runner_derive::{aoc, aoc_generator}; +use thiserror::Error; + +struct Image { + width: usize, + height: usize, + pixels: Vec, +} + +impl Image { + fn new(width: usize, height: usize) -> Image { + let pixels = vec![0; width * height]; + Image { + width, + height, + pixels, + } + } + fn new_with_pts(width: usize, height: usize, pts: &[(usize, usize)]) -> Image { + let pixels = vec![0; width * height]; + let mut im = Image { + width, + height, + pixels, + }; + dbg!(&width, &height); + pts.iter().for_each(|xy| im[*xy] = 1); + im + } + fn fold_y(&self, y_axis: usize) -> Image { + println!("fold_y @ {}", y_axis); + let mut im = Image::new(self.width, y_axis); + let odd = self.height % 2; + for y in 0..self.height { + for x in 0..self.width { + //dbg!( self.width, self.height, x, y, y_axis, (y % y_axis), self.pixels.len(), im.pixels.len()); + if self[(x, y)] > 0 { + if y > y_axis { + im[(x, self.height - y - odd)] = self[(x, y)]; + } else { + im[(x, y)] = self[(x, y)]; + } + } + } + } + im + } + fn fold_x(&self, x_axis: usize) -> Image { + let odd = self.width % 2; + println!("fold_x @ {}", x_axis); + for y in 0..self.height { + assert_eq!( + self[(x_axis, y)], + 0, + "w,h {},{} x_axis {}", + self.width, + self.height, + x_axis, + ); + } + let mut im = Image::new(x_axis, self.height); + for y in 0..self.height { + for x in 0..self.width { + if self[(x, y)] > 0 { + if x > x_axis { + im[(self.width - x - odd, y)] = self[(x, y)]; + } else { + im[(x, y)] = self[(x, y)]; + } + } + } + } + im + } + + fn count(&self) -> usize { + self.pixels.iter().filter(|&n| *n != 0).count() + } +} + +impl Debug for Image { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + writeln!(f)?; + for y in 0..self.height { + for x in 0..self.width { + if self[(x, y)] > 0 { + write!(f, "#")?; + } else { + write!(f, ".")?; + } + } + writeln!(f)?; + } + Ok(()) + } +} + +impl Index<(usize, usize)> for Image { + type Output = u8; + fn index(&self, (x, y): (usize, usize)) -> &Self::Output { + &self.pixels[x + y * self.width] + } +} + +impl IndexMut<(usize, usize)> for Image { + fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Self::Output { + //dbg!(self.width, self.height, x, y, self.pixels.len()); + &mut self.pixels[x + y * self.width] + } +} + +#[aoc(day13, part1)] +fn part1(input: &str) -> Result { + let (pts, folds) = input.split_once("\n\n").unwrap(); + let pts: Vec<(usize, usize)> = pts + .lines() + .map(|l| l.split_once(',').unwrap()) + .map(|(x, y)| (x.parse().unwrap(), y.parse().unwrap())) + .collect(); + let folds: Vec<_> = folds + .lines() + .map(|l| l.split(' ').nth(2).unwrap().split_once('=').unwrap()) + .map(|(axis, idx)| (axis, idx.parse().unwrap())) + .collect(); + let (maxx, maxy) = pts + .iter() + .fold((0, 0), |(maxx, maxy), (x, y)| (maxx.max(*x), maxy.max(*y))); + let mut im = Image::new_with_pts(maxx + 1, maxy + 1, &pts); + //dbg!(&im); + + for (axis, idx) in folds.iter().take(1) { + im = if *axis == "y" { + im.fold_y(*idx) + } else { + im.fold_x(*idx) + }; + } + //assert!(im.count() < 896); + dbg!(&im); + Ok(im.count()) +} + +/* +#[aoc(day13, part2)] +fn part2(input: &str) -> Result { +todo!("part2"); +Ok(0) +} +*/ + +#[cfg(test)] +mod tests { + use super::*; + + //#[test] + fn test_part1() -> Result<()> { + let input = r#" +6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +fold along y=7 +fold along x=5 + +"# + .trim(); + assert_eq!(part1(input)?, 17); + Ok(()) + } + + #[test] + fn test_fold_x() -> Result<()> { + let input = r#" +0,0 +1,1 +3,3 +4,4 + +fold along x=2 +fold along y=2 + +"# + .trim(); + let (pts, folds) = input.split_once("\n\n").unwrap(); + let pts: Vec<(usize, usize)> = pts + .lines() + .map(|l| l.split_once(',').unwrap()) + .map(|(x, y)| (x.parse().unwrap(), y.parse().unwrap())) + .collect(); + let folds: Vec<_> = folds + .lines() + .map(|l| l.split(' ').nth(2).unwrap().split_once('=').unwrap()) + .map(|(axis, idx)| (axis, idx.parse().unwrap())) + .collect(); + let (maxx, maxy) = pts + .iter() + .fold((0, 0), |(maxx, maxy), (x, y)| (maxx.max(*x), maxy.max(*y))); + let mut im = Image::new_with_pts(maxx + 1, maxy + 1, &pts); + dbg!(&im); + for (axis, idx) in folds.iter() { + im = if *axis == "y" { + im.fold_y(*idx) + } else { + im.fold_x(*idx) + }; + } + dbg!(&im); + //assert_eq!(im.count(), 17); + Ok(()) + } + /* + #[test] + fn test_part2()->Result<()> { + let input = r#" + "# + .trim(); + assert_eq!(part2(input)?, usize::MAX); + Ok(()) + } + */ +} diff --git a/2021/src/lib.rs b/2021/src/lib.rs index 8f95c8a..81b387a 100644 --- a/2021/src/lib.rs +++ b/2021/src/lib.rs @@ -2,6 +2,7 @@ pub mod day1; pub mod day10; pub mod day11; pub mod day12; +pub mod day13; pub mod day14; pub mod day2; pub mod day3;