From f2821aac4cd9618bc56ee01b2f62e27148c0386e Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Tue, 11 Jan 2022 09:27:40 +0100 Subject: [PATCH] Solve 2021/day13 --- 2021/day13/Cargo.lock | 7 + 2021/day13/Cargo.toml | 8 + 2021/day13/README.md | 169 ++++++++ 2021/day13/input/example0 | 2 + 2021/day13/input/example1 | 21 + 2021/day13/input/example2 | 15 + 2021/day13/input/example3 | 15 + 2021/day13/input/example4 | 7 + 2021/day13/input/example5 | 7 + 2021/day13/input/example6 | 7 + 2021/day13/input/input | 812 ++++++++++++++++++++++++++++++++++++++ 2021/day13/src/main.rs | 81 ++++ 12 files changed, 1151 insertions(+) create mode 100644 2021/day13/Cargo.lock create mode 100644 2021/day13/Cargo.toml create mode 100644 2021/day13/README.md create mode 100644 2021/day13/input/example0 create mode 100644 2021/day13/input/example1 create mode 100644 2021/day13/input/example2 create mode 100644 2021/day13/input/example3 create mode 100644 2021/day13/input/example4 create mode 100644 2021/day13/input/example5 create mode 100644 2021/day13/input/example6 create mode 100644 2021/day13/input/input create mode 100644 2021/day13/src/main.rs diff --git a/2021/day13/Cargo.lock b/2021/day13/Cargo.lock new file mode 100644 index 0000000..74e24a1 --- /dev/null +++ b/2021/day13/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day13" +version = "0.1.0" diff --git a/2021/day13/Cargo.toml b/2021/day13/Cargo.toml new file mode 100644 index 0000000..36c9510 --- /dev/null +++ b/2021/day13/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day13" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2021/day13/README.md b/2021/day13/README.md new file mode 100644 index 0000000..f3f41f9 --- /dev/null +++ b/2021/day13/README.md @@ -0,0 +1,169 @@ +https://adventofcode.com/2021/day/13 + +## \--- Day 13: Transparent Origami --- + +You reach another volcanically active part of the cave. It would be nice if +you could do some kind of thermal imaging so you could tell ahead of time +which caves are too hot to safely enter. + +Fortunately, the submarine seems to be equipped with a thermal camera! When +you activate it, you are greeted with: + +[code] + + Congratulations on your purchase! To activate this infrared thermal imaging + camera system, please enter the code found on page 1 of the manual. + +[/code] + +Apparently, the Elves have never used this feature. To your surprise, you +manage to find the manual; as you go to open it, page 1 falls out. It's a +large sheet of [transparent +paper](https://en.wikipedia.org/wiki/Transparency_\(projection\))! The +transparent paper is marked with random dots and includes instructions on how +to fold it up (your puzzle input). For example: + +[code] + + 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 + +[/code] + +The first section is a list of dots on the transparent paper. `0,0` represents +the top-left coordinate. The first value, `x`, increases to the right. The +second value, `y`, increases downward. So, the coordinate `3,0` is to the +right of `0,0`, and the coordinate `0,7` is below `0,0`. The coordinates in +this example form the following pattern, where `#` is a dot on the paper and +`.` is an empty, unmarked position: + +[code] + + ...#..#..#. + ....#...... + ........... + #.......... + ...#....#.# + ........... + ........... + ........... + ........... + ........... + .#....#.##. + ....#...... + ......#...# + #.......... + #.#........ + +[/code] + +Then, there is a list of _fold instructions_. Each instruction indicates a +line on the transparent paper and wants you to fold the paper _up_ (for +horizontal `y=...` lines) or _left_ (for vertical `x=...` lines). In this +example, the first fold instruction is `fold along y=7`, which designates the +line formed by all of the positions where `y` is `7` (marked here with `-`): + +[code] + + ...#..#..#. + ....#...... + ........... + #.......... + ...#....#.# + ........... + ........... + ----------- + ........... + ........... + .#....#.##. + ....#...... + ......#...# + #.......... + #.#........ + +[/code] + +Because this is a horizontal line, fold the bottom half _up_. Some of the dots +might end up overlapping after the fold is complete, but dots will never +appear exactly on a fold line. The result of doing this fold looks like this: + +[code] + + #.##..#..#. + #...#...... + ......#...# + #...#...... + .#.#..#.### + ........... + ........... + +[/code] + +Now, only `17` dots are visible. + +Notice, for example, the two dots in the bottom left corner before the +transparent paper is folded; after the fold is complete, those dots appear in +the top left corner (at `0,0` and `0,1`). Because the paper is transparent, +the dot just below them in the result (at `0,3`) remains visible, as it can be +seen through the transparent paper. + +Also notice that some dots can end up _overlapping_ ; in this case, the dots +merge together and become a single dot. + +The second fold instruction is `fold along x=5`, which indicates this line: + +[code] + + #.##.|#..#. + #...#|..... + .....|#...# + #...#|..... + .#.#.|#.### + .....|..... + .....|..... + +[/code] + +Because this is a vertical line, fold _left_ : + +[code] + + ##### + #...# + #...# + #...# + ##### + ..... + ..... + +[/code] + +The instructions made a square! + +The transparent paper is pretty big, so for now, focus on just completing the +first fold. After the first fold in the example above, `_17_` dots are visible +- dots that end up overlapping after the fold is completed count as a single +dot. + +_How many dots are visible after completing just the first fold instruction on +your transparent paper?_ + diff --git a/2021/day13/input/example0 b/2021/day13/input/example0 new file mode 100644 index 0000000..bc63ff8 --- /dev/null +++ b/2021/day13/input/example0 @@ -0,0 +1,2 @@ +Congratulations on your purchase! To activate this infrared thermal imaging +camera system, please enter the code found on page 1 of the manual. diff --git a/2021/day13/input/example1 b/2021/day13/input/example1 new file mode 100644 index 0000000..282114c --- /dev/null +++ b/2021/day13/input/example1 @@ -0,0 +1,21 @@ +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 diff --git a/2021/day13/input/example2 b/2021/day13/input/example2 new file mode 100644 index 0000000..4a6d6fe --- /dev/null +++ b/2021/day13/input/example2 @@ -0,0 +1,15 @@ +...#..#..#. +....#...... +........... +#.......... +...#....#.# +........... +........... +........... +........... +........... +.#....#.##. +....#...... +......#...# +#.......... +#.#........ diff --git a/2021/day13/input/example3 b/2021/day13/input/example3 new file mode 100644 index 0000000..eb2caa0 --- /dev/null +++ b/2021/day13/input/example3 @@ -0,0 +1,15 @@ +...#..#..#. +....#...... +........... +#.......... +...#....#.# +........... +........... +----------- +........... +........... +.#....#.##. +....#...... +......#...# +#.......... +#.#........ diff --git a/2021/day13/input/example4 b/2021/day13/input/example4 new file mode 100644 index 0000000..8d475a3 --- /dev/null +++ b/2021/day13/input/example4 @@ -0,0 +1,7 @@ +#.##..#..#. +#...#...... +......#...# +#...#...... +.#.#..#.### +........... +........... diff --git a/2021/day13/input/example5 b/2021/day13/input/example5 new file mode 100644 index 0000000..bfcca03 --- /dev/null +++ b/2021/day13/input/example5 @@ -0,0 +1,7 @@ +#.##.|#..#. +#...#|..... +.....|#...# +#...#|..... +.#.#.|#.### +.....|..... +.....|..... diff --git a/2021/day13/input/example6 b/2021/day13/input/example6 new file mode 100644 index 0000000..d59120b --- /dev/null +++ b/2021/day13/input/example6 @@ -0,0 +1,7 @@ +##### +#...# +#...# +#...# +##### +..... +..... diff --git a/2021/day13/input/input b/2021/day13/input/input new file mode 100644 index 0000000..556b07a --- /dev/null +++ b/2021/day13/input/input @@ -0,0 +1,812 @@ +726,774 +246,695 +579,249 +691,724 +246,820 +738,887 +1088,75 +264,887 +704,775 +907,625 +676,117 +507,658 +1009,24 +547,735 +157,126 +599,113 +445,226 +363,691 +918,794 +927,113 +999,400 +443,305 +654,729 +408,767 +1066,863 +1148,473 +321,35 +1093,803 +1044,718 +202,889 +262,164 +378,541 +619,662 +1034,849 +432,595 +1145,656 +1295,668 +1125,705 +1161,529 +759,619 +1170,147 +688,742 +328,729 +718,439 +935,701 +246,647 +594,110 +845,495 +160,189 +1225,315 +580,486 +469,481 +440,401 +584,774 +897,719 +1007,516 +547,159 +112,117 +982,645 +62,439 +192,441 +631,211 +654,16 +113,698 +378,865 +373,19 +441,777 +390,7 +1263,312 +1121,610 +509,582 +893,352 +44,131 +1092,19 +592,719 +918,100 +326,820 +62,719 +520,889 +718,103 +571,579 +1165,267 +1208,334 +525,415 +268,588 +769,464 +596,716 +734,436 +1283,747 +35,457 +982,94 +1235,849 +932,752 +1265,243 +262,752 +99,704 +547,732 +1096,829 +791,329 +1222,320 +199,568 +671,494 +1079,93 +569,315 +129,415 +868,878 +788,803 +1175,889 +965,676 +904,660 +552,560 +619,484 +507,236 +566,768 +1215,30 +45,410 +885,441 +478,336 +945,397 +596,306 +1145,686 +189,495 +1153,126 +79,444 +719,403 +903,91 +27,105 +441,329 +768,565 +671,400 +507,205 +224,390 +867,753 +425,441 +1123,67 +833,14 +793,278 +1237,724 +238,808 +1099,32 +411,243 +1,574 +726,120 +862,441 +212,94 +80,199 +1047,312 +164,389 +440,773 +185,189 +412,114 +537,792 +403,697 +1208,768 +408,527 +1135,831 +746,826 +1064,110 +15,226 +102,574 +1001,442 +769,16 +1236,408 +440,121 +1309,131 +771,473 +1064,527 +189,284 +427,704 +276,865 +986,593 +1009,870 +745,276 +965,291 +1190,5 +1084,768 +313,750 +976,544 +1222,658 +223,619 +718,791 +976,574 +705,822 +785,863 +388,390 +1235,716 +579,645 +90,378 +497,849 +1308,145 +658,301 +648,94 +1150,189 +231,129 +408,639 +390,119 +132,556 +639,220 +885,453 +413,651 +639,68 +177,544 +552,782 +239,130 +867,645 +187,67 +1163,494 +1227,19 +249,658 +132,270 +740,82 +73,206 +1166,525 +262,506 +1092,875 +923,509 +967,400 +813,849 +734,364 +604,541 +566,758 +90,852 +190,831 +1047,65 +313,144 +542,157 +720,304 +129,863 +1280,149 +448,441 +947,484 +1048,506 +408,863 +657,803 +480,834 +201,369 +489,208 +1275,36 +1064,784 +781,845 +1071,152 +1251,61 +455,500 +564,68 +758,798 +922,390 +443,421 +505,792 +60,784 +1059,645 +741,315 +89,164 +142,637 +348,705 +763,735 +165,686 +711,781 +1220,852 +455,120 +1205,859 +1208,413 +1098,240 +33,505 +821,208 +35,289 +1251,833 +1131,226 +745,730 +75,625 +905,75 +738,7 +1223,316 +923,395 +7,884 +1153,299 +552,96 +1047,134 +642,266 +537,698 +1211,190 +959,235 +1235,625 +711,614 +510,798 +73,306 +47,582 +657,875 +468,75 +638,346 +144,525 +612,665 +917,57 +1235,402 +211,249 +831,63 +1197,250 +493,686 +887,801 +85,315 +263,65 +924,306 +140,595 +997,141 +657,173 +579,850 +489,686 +448,5 +572,7 +1248,103 +445,674 +711,113 +1232,884 +1121,278 +845,732 +786,630 +114,712 +691,662 +140,147 +1088,299 +408,191 +897,691 +92,662 +904,754 +1309,621 +102,768 +7,10 +33,429 +903,803 +2,749 +1083,704 +157,819 +325,91 +830,386 +763,284 +175,63 +902,199 +887,129 +1285,297 +1287,683 +590,304 +714,716 +6,686 +1136,834 +452,889 +653,315 +135,145 +683,329 +251,473 +1110,455 +959,516 +1091,838 +407,220 +654,800 +549,239 +765,355 +113,250 +771,93 +194,270 +864,803 +517,417 +345,291 +253,724 +365,145 +522,624 +692,495 +830,834 +1021,301 +825,595 +145,890 +1125,481 +48,119 +82,7 +965,666 +540,630 +542,121 +731,290 +85,763 +957,297 +1277,617 +1089,239 +619,612 +62,175 +427,190 +401,724 +1133,798 +475,882 +1062,140 +246,784 +1001,515 +244,31 +759,171 +1246,282 +249,236 +919,816 +907,25 +755,403 +557,725 +15,332 +840,861 +1031,760 +965,218 +813,45 +440,829 +885,457 +45,243 +351,435 +191,565 +984,820 +715,275 +689,641 +289,845 +575,892 +605,822 +1136,508 +137,278 +870,513 +59,621 +79,539 +89,276 +1,621 +753,725 +1119,301 +1,763 +1159,539 +1059,25 +267,843 +1072,236 +1203,607 +425,453 +62,551 +788,624 +576,884 +326,430 +345,452 +539,473 +1195,337 +981,432 +23,683 +455,394 +1098,654 +1118,441 +1277,429 +567,625 +773,698 +23,459 +441,464 +870,488 +567,269 +517,399 +935,302 +436,875 +309,442 +900,191 +1277,277 +1310,165 +383,781 +149,792 +492,578 +88,658 +736,500 +103,275 +731,44 +720,794 +1278,600 +981,462 +1020,623 +329,462 +333,483 +977,880 +1181,863 +509,890 +846,831 +246,527 +570,754 +841,481 +78,884 +1231,355 +618,47 +413,719 +850,786 +174,386 +364,175 +1193,196 +162,421 +221,239 +373,875 +718,7 +1231,539 +1218,662 +333,868 +885,9 +30,149 +1299,511 +227,190 +1197,698 +1150,705 +657,238 +783,717 +1159,383 +212,688 +241,719 +618,495 +1232,458 +985,432 +27,75 +918,506 +328,800 +145,582 +387,395 +90,516 +1019,773 +334,574 +1285,149 +716,558 +413,236 +353,477 +607,193 +845,844 +874,875 +1099,648 +264,63 +99,491 +263,312 +303,67 +463,565 +433,239 +902,527 +132,355 +1300,550 +606,119 +291,849 +25,86 +301,865 +1046,831 +805,102 +408,598 +1101,693 +1215,864 +947,691 +64,730 +1237,170 +970,628 +1136,60 +947,730 +334,544 +1149,686 +1009,198 +691,282 +691,457 +378,142 +599,614 +648,320 +507,400 +1178,803 +478,558 +267,51 +279,701 +965,452 +691,232 +401,170 +219,838 +1292,413 +296,373 +246,127 +446,91 +894,86 +115,480 +1287,155 +433,655 +263,134 +87,630 +965,403 +107,623 +189,610 +330,749 +1121,271 +965,603 +135,749 +1059,473 +328,165 +27,147 +443,134 +1210,320 +1211,470 +415,824 +835,882 +405,819 +957,870 +493,721 +1246,164 +935,591 +895,824 +264,455 +99,470 +408,296 +803,400 +1084,126 +1135,63 +835,46 +830,60 +653,721 +1104,453 +525,863 +102,334 +1235,45 +870,121 +375,591 +60,336 +348,880 +895,600 +517,477 +427,526 +100,551 +37,301 +477,880 +0,94 +383,390 +266,718 +212,240 +634,329 +291,493 +976,320 +751,30 +1119,593 +443,753 +1099,645 +79,450 +189,278 +358,745 +870,355 +1064,820 +353,597 +770,630 +157,75 +22,371 +214,493 +465,726 +1205,655 +740,469 +125,49 +1019,849 +735,556 +1148,421 +832,336 +803,338 +848,441 +946,175 +301,149 +115,305 +528,215 +6,208 +6,320 +691,410 +238,658 +16,346 +735,892 +689,725 +661,320 +1136,386 +1153,810 +363,730 +326,464 +321,819 +885,885 +1048,388 +425,9 +127,809 +656,16 +599,280 +74,408 +387,509 +73,724 +293,877 +557,687 +656,878 +1304,320 +1034,865 +704,119 +126,663 +141,656 +914,859 +1230,647 +340,266 +33,501 +1262,352 +505,344 +1283,105 +1198,289 +855,500 +574,871 +540,598 +465,844 +1210,343 +825,96 +290,623 +174,508 +132,539 +867,93 +1101,201 +392,142 +472,126 +552,320 +631,739 +867,473 +763,758 +467,67 +251,269 +1273,301 +619,282 +1148,130 +801,4 +566,136 +1098,94 +1274,47 +333,411 +401,82 +48,352 +1230,522 +907,269 +870,829 +631,683 +406,660 +440,488 +800,798 +244,765 +657,768 +662,94 +33,465 +785,415 +83,450 +1159,355 +653,686 +392,730 +417,352 +691,829 +902,598 +947,282 +242,469 +135,301 +174,834 +816,189 +914,894 +671,562 +460,786 +1079,765 +125,525 +213,430 +480,60 +1086,390 +1111,809 +375,302 +1227,390 +1072,684 +343,494 +443,473 +904,234 +485,45 +1285,86 +977,299 +758,782 +242,425 +460,718 +37,593 +730,486 +559,877 +505,102 +401,812 +231,254 +403,269 +493,238 +1181,479 +189,29 +545,355 +264,439 +547,60 +900,695 +689,393 +1079,254 +408,199 +846,383 +1223,630 +1121,284 +345,666 +441,117 +237,800 +191,525 +1066,255 +552,768 +639,494 +798,705 +1004,189 +1169,861 +686,745 +1121,29 +554,126 +1277,465 +977,432 +711,399 +12,628 + +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/day13/src/main.rs b/2021/day13/src/main.rs new file mode 100644 index 0000000..d9ce07f --- /dev/null +++ b/2021/day13/src/main.rs @@ -0,0 +1,81 @@ +use std::collections::HashSet; +use std::io::stdin; + +fn main() { + let mut buffer = String::new(); + let stdin = stdin(); + + stdin.read_line(&mut buffer).ok(); + + let mut points: Vec<(i32, i32)> = Vec::new(); + let mut instructions: Vec<(char, i32)> = Vec::new(); + + // parse points + while !buffer.is_empty() { + // parse + let mut numbers = buffer.trim().split(',').map(|s| s.parse().unwrap()); + let x = numbers.next().unwrap(); + let y = numbers.next().unwrap(); + points.push((x, y)); + + // read next + buffer.clear(); + stdin.read_line(&mut buffer).ok(); + + if buffer == "\n" { + break; + } + } + + stdin.read_line(&mut buffer).ok(); + + while !buffer.is_empty() { + let instruction = buffer.trim().split(' ').nth(2).unwrap(); + let mut parts = instruction.split('='); + let axis = parts.next().unwrap().chars().next().unwrap(); + let line = parts.next().unwrap().parse().unwrap(); + instructions.push((axis, line)); + buffer.clear(); + stdin.read_line(&mut buffer).ok(); + } + + println!("Points: {:?}", points); + println!("Instructions: {:?}", instructions); + + let part1 = apply_instructions(&points, &instructions[..1]); + let part2 = apply_instructions(&points, &instructions); + + dbg!(part1.len()); + + let width = *part2.iter().map(|(x, _)| x).max().unwrap() + 1; + let height = *part2.iter().map(|(_, y)| y).max().unwrap() + 1; + + for y in 0..height { + for x in 0..width { + if part2.contains(&(x, y)) { + print!("#"); + } else { + print!("."); + } + } + println!(); + } +} + +fn apply_instructions(points: &[(i32, i32)], instructions: &[(char, i32)]) -> HashSet<(i32, i32)> { + let mut translated_points = HashSet::new(); + + for point in points { + let (mut x, mut y) = *point; + for (axis, line) in instructions { + if *axis == 'x' && x > *line { + x = 2 * line - x; + } else if *axis == 'y' && y > *line { + y = 2 * line - y; + } + } + translated_points.insert((x, y)); + } + + translated_points +}