diff options
-rw-r--r-- | 13/Cargo.toml | 11 | ||||
-rw-r--r-- | 13/input | 735 | ||||
-rw-r--r-- | 13/src/main.rs | 151 | ||||
-rw-r--r-- | 13/tests/main.rs | 22 | ||||
-rw-r--r-- | Cargo.lock | 8 | ||||
-rw-r--r-- | Cargo.toml | 1 |
6 files changed, 928 insertions, 0 deletions
diff --git a/13/Cargo.toml b/13/Cargo.toml new file mode 100644 index 0000000..f69c5e1 --- /dev/null +++ b/13/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "advent_13" +version = "0.1.0" +authors = ["Irene Knapp <ireneista@gmail.com>"] +edition = "2018" + +[dependencies] +advent_lib = { path = "../lib" } + +[dev-dependencies] +assert_cmd = "0.10" diff --git a/13/input b/13/input new file mode 100644 index 0000000..29fa7d5 --- /dev/null +++ b/13/input @@ -0,0 +1,735 @@ +797,352 +70,595 +1049,372 +887,710 +774,607 +581,348 +827,238 +341,1 +1295,858 +1017,854 +370,430 +937,465 +592,51 +604,850 +1169,766 +754,565 +577,28 +534,128 +925,119 +985,217 +917,522 +1275,64 +897,74 +239,723 +547,450 +1039,676 +606,158 +313,561 +1275,463 +513,352 +278,494 +634,436 +909,31 +480,176 +18,100 +417,807 +1250,882 +1158,485 +1158,24 +587,716 +112,68 +704,46 +887,632 +127,158 +706,32 +967,680 +922,523 +524,121 +579,499 +1268,654 +475,662 +20,516 +1073,843 +570,742 +89,415 +423,520 +11,219 +1125,784 +585,614 +1079,374 +768,826 +1216,840 +1014,719 +808,409 +95,683 +1221,418 +1111,460 +159,869 +1159,404 +5,395 +1295,410 +1111,879 +233,555 +156,238 +1166,660 +952,66 +289,651 +35,207 +723,716 +540,568 +542,826 +75,723 +175,852 +671,484 +565,759 +363,210 +621,602 +922,259 +293,152 +1287,348 +703,469 +1292,794 +1005,884 +423,268 +514,301 +862,800 +35,324 +219,864 +70,299 +800,611 +937,669 +187,236 +512,187 +464,810 +92,147 +5,445 +35,239 +233,339 +1275,324 +952,380 +360,267 +159,473 +1275,100 +530,375 +1061,171 +171,656 +1001,768 +648,861 +586,840 +1153,365 +1136,494 +800,283 +55,226 +1289,682 +1203,478 +763,444 +244,50 +401,31 +284,661 +1201,56 +1221,479 +239,781 +1049,522 +629,768 +25,451 +989,893 +224,329 +541,542 +68,168 +159,82 +962,660 +1169,234 +1026,849 +127,736 +50,131 +335,42 +745,639 +18,709 +547,478 +701,17 +975,852 +733,85 +70,747 +823,28 +89,476 +186,278 +329,229 +681,126 +206,849 +1238,145 +1101,684 +887,235 +897,820 +504,135 +141,128 +898,152 +673,819 +725,614 +537,333 +107,528 +1144,75 +652,656 +206,781 +510,611 +701,877 +363,684 +796,301 +120,576 +475,232 +1043,301 +694,382 +468,168 +1121,523 +341,721 +5,1 +251,753 +641,781 +704,158 +472,212 +952,175 +518,254 +17,712 +997,561 +1168,687 +1290,572 +102,341 +769,800 +54,250 +1183,417 +430,599 +1265,17 +271,39 +889,224 +87,723 +18,185 +565,387 +1043,794 +107,2 +179,232 +1078,392 +1174,196 +577,85 +5,51 +746,257 +867,649 +992,3 +1039,218 +117,882 +373,221 +922,399 +633,682 +519,268 +77,866 +100,886 +909,583 +510,548 +1221,415 +1186,519 +254,667 +846,278 +467,658 +306,719 +619,512 +1297,445 +776,128 +1275,431 +293,516 +405,723 +513,766 +361,264 +319,208 +423,632 +1034,651 +1292,548 +483,691 +1133,173 +426,553 +401,194 +724,597 +1285,451 +956,868 +423,626 +1256,644 +798,682 +488,75 +214,785 +937,429 +430,565 +1139,875 +20,458 +584,267 +1210,529 +226,857 +1069,820 +1216,54 +1084,37 +355,392 +370,799 +159,418 +997,51 +341,273 +1019,658 +827,19 +395,389 +552,151 +1263,469 +378,31 +467,796 +1272,512 +388,135 +1009,891 +661,144 +1144,819 +731,499 +405,843 +415,171 +87,611 +353,219 +637,819 +483,19 +743,723 +361,630 +75,171 +1099,693 +1077,339 +186,481 +925,215 +950,179 +1135,852 +541,352 +676,436 +616,382 +385,215 +141,234 +547,814 +370,213 +791,626 +775,187 +1151,812 +987,320 +212,774 +510,794 +291,158 +512,458 +1168,436 +763,864 +233,670 +373,673 +400,774 +1102,607 +284,337 +681,768 +1169,10 +724,54 +48,46 +981,665 +1305,472 +746,705 +964,182 +1183,736 +716,311 +226,275 +1151,418 +25,320 +1233,866 +1056,151 +947,210 +1044,75 +266,47 +109,56 +107,668 +1129,350 +348,10 +1260,315 +413,74 +1190,781 +964,570 +78,35 +956,250 +141,766 +276,651 +535,707 +1089,887 +1141,141 +189,523 +1044,819 +159,476 +800,100 +33,154 +1240,299 +318,891 +440,785 +1057,759 +1073,866 +376,95 +1014,66 +45,877 +703,425 +382,80 +846,250 +554,171 +565,59 +1285,574 +388,399 +636,100 +1203,416 +186,726 +962,98 +268,290 +358,66 +117,689 +835,438 +176,45 +45,17 +713,567 +1026,233 +271,666 +1014,842 +1232,35 +592,843 +1196,239 +934,95 +576,35 +1242,726 +354,250 +239,113 +171,19 +189,14 +1280,623 +842,413 +1240,267 +109,420 +306,175 +889,448 +740,742 +1190,576 +920,51 +910,774 +1059,753 +475,456 +522,724 +141,10 +1169,110 +554,843 +513,318 +977,638 +997,621 +1280,445 +73,311 +358,380 +1210,886 +477,273 +1151,476 +370,109 +977,893 +734,35 +423,710 +1088,10 +832,583 +537,176 +383,814 +843,684 +403,499 +1026,9 +468,413 +855,158 +728,145 +388,759 +109,26 +77,332 +1305,499 +383,855 +1280,604 +33,264 +470,660 +671,410 +893,807 +1073,807 +1241,280 +873,56 +674,633 +1232,411 +100,365 +691,830 +639,484 +895,171 +378,863 +1292,709 +142,207 +341,497 +50,602 +385,775 +512,660 +624,604 +1237,583 +343,662 +1069,522 +674,634 +838,212 +835,456 +354,644 +987,574 +187,348 +554,227 +1169,576 +109,474 +5,449 +955,502 +691,684 +293,488 +17,768 +570,152 +962,12 +281,127 +897,332 +1138,207 +475,438 +850,78 +634,567 +1139,238 +1218,747 +376,239 +100,456 +464,616 +536,287 +355,884 +825,499 +1262,400 +472,186 +882,37 +21,112 +1193,15 +634,458 +296,828 +870,785 +609,669 +1059,141 +587,178 +1151,82 +636,633 +305,884 +231,626 +565,863 +1044,707 +330,75 +905,275 +1201,474 +865,176 +937,673 +325,217 +82,50 +786,491 +1056,667 +84,600 +435,30 +346,261 +1124,278 +827,875 +586,493 +706,490 +783,288 +1026,213 +969,173 +35,548 +1017,488 +537,431 +800,794 +370,457 +1201,420 +791,420 +540,326 +1218,299 +815,389 +1039,855 +997,397 +25,443 +1098,774 +637,75 +651,768 +1168,682 +397,838 +77,85 +306,255 +269,663 +597,327 +825,589 +745,835 +18,794 +189,595 +1001,126 +643,219 +1304,413 +1223,723 +437,502 +100,723 +889,672 +1215,683 +1059,499 +1262,494 +1282,784 +870,557 +1102,287 +997,358 +893,87 +1032,494 +373,429 +387,301 +1237,642 +388,595 +535,187 +1288,660 +1046,51 +1093,838 +1044,495 +1084,619 +440,557 +1293,187 +346,570 +792,644 +413,332 +832,54 +1299,219 +383,666 +1044,135 +934,400 +763,814 +940,437 +661,445 +1026,337 +1305,445 +910,227 +682,308 +42,688 +370,95 +191,268 +1002,772 +343,214 +1210,723 +306,799 +1029,575 +738,844 +1290,119 +199,879 +485,395 +1228,50 +455,158 +157,365 +57,567 +1104,849 +1121,747 +1158,171 +1272,96 +78,411 +264,51 +539,89 +237,843 +979,499 +331,395 +1277,740 +445,210 +141,318 +94,54 +464,250 +991,686 +1260,131 +341,358 +142,212 +1169,128 +1056,326 +684,112 +949,264 +840,660 +733,361 +644,633 +57,119 +884,553 +341,536 +882,409 +1151,25 +30,333 +512,682 +47,425 +261,522 +1123,658 +686,604 +633,212 +706,315 +171,238 +950,627 +53,756 +1124,369 +1289,782 +313,273 +233,672 +2,432 +437,56 +141,576 +221,887 +564,705 +385,679 +576,859 +594,12 +1166,731 +1275,239 +770,326 +552,105 +278,400 +1124,616 +1123,236 +940,457 +976,481 +678,861 +1004,95 +1168,234 +662,861 +512,212 +1206,10 +284,457 +676,660 +1203,668 +211,693 +923,301 +800,548 +775,707 +1026,557 +922,495 +294,267 +370,437 +604,490 +1029,109 +181,350 +1119,268 +1086,329 +321,1 +666,633 +791,474 +691,64 +793,226 +1039,39 +428,37 +667,227 +358,445 +554,723 +689,602 +388,259 +400,227 +826,768 +20,322 +567,171 +1153,529 +301,798 +1168,484 + +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/13/src/main.rs b/13/src/main.rs new file mode 100644 index 0000000..121fae4 --- /dev/null +++ b/13/src/main.rs @@ -0,0 +1,151 @@ +use advent_lib::prelude::*; + +use std::collections::HashSet; + + +#[derive(Debug,Clone,Copy,PartialEq,Eq)] +enum Axis { + X, + Y, +} + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::group_lines_by_blanks( + advent_lib::read_lines_file(&filename)?); + + let mut dots: HashSet<(u64,u64)> = HashSet::new(); + for line in &input[0] { + let words: Vec<&str> = line.split(',').collect(); + let x = words[0].parse::<u64>()?; + let y = words[1].parse::<u64>()?; + dots.insert((x, y)); + } + + let mut folds: Vec<(Axis,u64)> = Vec::new(); + for line in &input[1]{ + let words: Vec<&str> = line.split(' ').collect(); + let subwords: Vec<&str> = words[2].split('=').collect(); + let axis = match subwords[0] { + "x" => Axis::X, + "y" => Axis::Y, + _ => panic!("hm"), + }; + let value = subwords[1].parse::<u64>()?; + folds.push((axis, value)); + } + + let (width, height) = bounds(&dots); + + let mut first_fold = Vec::new(); + first_fold.push(folds[0].clone()); + + let rendering = render(width, height, &dots, &first_fold); + println!("{}", rendering.len()); + + let rendering = render(width, height, &dots, &folds); + let (width, height) = bounds(&rendering); + for y in 0 .. height { + for x in 0 .. width { + if rendering.contains(&(x, y)) { + print!("#"); + } else { + print!("."); + } + } + println!(""); + } + + Ok(()) +} + + +fn bounds(dots: &HashSet<(u64, u64)>) -> (u64, u64) { + let mut max_x = None; + let mut max_y = None; + for (x, y) in dots { + max_x = match max_x { + None => Some(x), + Some(old_x) => if x > old_x { + Some(x) + } else { + Some(old_x) + } + }; + + max_y = match max_y { + None => Some(y), + Some(old_y) => if y > old_y { + Some(y) + } else { + Some(old_y) + } + }; + } + let width = max_x.unwrap() + 1; + let height = max_y.unwrap() + 1; + + (width, height) +} + + +fn render(width: u64, height: u64, dots: &HashSet<(u64, u64)>, + folds: &Vec<(Axis, u64)>) + -> HashSet<(u64, u64)> +{ + let mut result = HashSet::new(); + + for y in 0 .. height { + for x in 0 .. width { + if dots.contains(&(x, y)) { + let (new_x, new_y) = apply_folds(x, y, folds); + result.insert((new_x, new_y)); + } + } + } + + result +} + + +fn apply_folds(x: u64, y: u64, folds: &Vec<(Axis, u64)>) -> (u64, u64) { + let mut transformed_x = x; + let mut transformed_y = y; + + for fold in folds { + let (new_x, new_y) = apply_fold(transformed_x, transformed_y, fold); + transformed_x = new_x; + transformed_y = new_y; + } + + (transformed_x, transformed_y) +} + + +fn apply_fold(x: u64, y: u64, fold: &(Axis, u64)) -> (u64, u64) { + let (axis, fold_value) = fold; + match axis { + Axis::X => { + if x > *fold_value { + (2 * *fold_value - x, y) + } else { + (x, y) + } + } + Axis::Y => { + if y > *fold_value { + (x, 2 * *fold_value - y) + } else { + (x, y) + } + } + } +} + diff --git a/13/tests/main.rs b/13/tests/main.rs new file mode 100644 index 0000000..69437e9 --- /dev/null +++ b/13/tests/main.rs @@ -0,0 +1,22 @@ +use assert_cmd::prelude::*; +//use predicates::prelude::*; +use std::process::Command; + + +#[test] +fn personal_input() -> Result<(), Box<dyn std::error::Error>> { + let mut command = Command::cargo_bin("advent_13")?; + + command.arg("input"); + command.assert().success().stdout( + "592\n\ + ..##..##...##....##.####.####.#..#.#..#\n\ + ...#.#..#.#..#....#.#....#....#.#..#..#\n\ + ...#.#....#..#....#.###..###..##...#..#\n\ + ...#.#.##.####....#.#....#....#.#..#..#\n\ + #..#.#..#.#..#.#..#.#....#....#.#..#..#\n\ + .##...###.#..#..##..####.#....#..#..##.\n"); + + Ok(()) +} + diff --git a/Cargo.lock b/Cargo.lock index 91ae245..d6b5403 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -97,6 +97,14 @@ dependencies = [ ] [[package]] +name = "advent_13" +version = "0.1.0" +dependencies = [ + "advent_lib", + "assert_cmd", +] + +[[package]] name = "advent_lib" version = "0.1.0" dependencies = [ diff --git a/Cargo.toml b/Cargo.toml index 4eac77e..60b1b36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ members = [ "10", "11", "12", + "13", ] |