From 5293bf274e3198fd12a90809545698b1dea0e4f0 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Sun, 19 Dec 2021 00:49:30 +0100 Subject: [PATCH] Added day 13 code for part 1 and 2 Signed-off-by: Louis Vallat --- .gitlab-ci.yml | 5 + day13/Cargo.toml | 8 + day13/input | 934 ++++++++++++++++++++++++++++++++++++++++++++++ day13/src/main.rs | 71 ++++ 4 files changed, 1018 insertions(+) create mode 100644 day13/Cargo.toml create mode 100644 day13/input create mode 100644 day13/src/main.rs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a472820..a8a7fbf 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -63,3 +63,8 @@ day-12: stage: build script: - cd day12; cargo run --release ./input + +day-13: + stage: build + script: + - cd day13; cargo run --release ./input diff --git a/day13/Cargo.toml b/day13/Cargo.toml new file mode 100644 index 0000000..36c9510 --- /dev/null +++ b/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/day13/input b/day13/input new file mode 100644 index 0000000..b2b2c61 --- /dev/null +++ b/day13/input @@ -0,0 +1,934 @@ +60,140 +1044,50 +878,203 +641,606 +669,606 +947,686 +370,534 +251,703 +1215,432 +1280,542 +1263,481 +701,703 +47,257 +1173,820 +1082,387 +1220,217 +152,796 +624,799 +522,147 +951,584 +1255,144 +1179,534 +224,296 +488,527 +99,838 +189,857 +440,766 +1148,302 +492,190 +82,530 +1019,514 +75,707 +1084,589 +710,429 +1123,666 +1192,810 +755,56 +791,866 +788,876 +363,42 +1290,429 +676,17 +208,362 +976,492 +1290,744 +237,705 +961,831 +137,596 +1218,312 +228,3 +795,4 +1275,798 +1098,93 +649,234 +266,844 +410,187 +291,852 +597,598 +212,801 +131,360 +1260,607 +1069,184 +438,198 +763,210 +1272,529 +55,592 +751,822 +383,358 +228,611 +746,217 +219,358 +713,282 +556,443 +957,78 +363,14 +1203,28 +542,409 +787,422 +788,147 +845,526 +109,826 +90,677 +1091,358 +90,217 +382,40 +764,518 +944,618 +835,819 +156,672 +653,289 +1253,737 +912,236 +75,365 +320,784 +284,729 +649,794 +167,341 +408,635 +835,75 +599,21 +92,358 +557,113 +62,417 +597,150 +1099,257 +610,586 +393,522 +1203,600 +875,403 +35,686 +1275,422 +1141,872 +184,362 +730,390 +726,809 +234,689 +87,259 +1223,22 +100,346 +992,236 +1064,584 +947,42 +182,178 +1215,74 +316,368 +95,462 +1203,236 +1034,261 +547,600 +181,320 +1212,611 +1171,628 +1150,284 +982,669 +1266,756 +694,305 +803,724 +445,551 +1173,688 +1173,686 +542,428 +537,750 +567,299 +676,196 +594,476 +539,413 +989,800 +773,103 +195,472 +887,24 +1014,411 +72,236 +1103,250 +195,422 +16,82 +395,696 +812,217 +392,731 +925,494 +980,79 +537,103 +219,403 +1154,670 +1250,306 +927,536 +832,131 +1228,513 +609,191 +937,78 +612,646 +264,809 +1190,197 +57,109 +701,191 +1082,3 +416,266 +940,534 +3,117 +1237,593 +437,686 +933,808 +408,859 +1039,536 +788,828 +681,126 +55,693 +1073,441 +795,101 +1131,170 +1256,854 +179,75 +330,815 +131,870 +64,443 +510,828 +1111,96 +895,807 +1016,857 +199,239 +1240,210 +710,465 +873,686 +765,675 +1253,785 +1163,147 +1014,595 +443,861 +1196,353 +719,121 +194,707 +306,660 +348,822 +624,859 +1190,151 +559,500 +1012,421 +756,548 +600,465 +53,798 +1212,731 +442,9 +30,156 +1231,502 +1171,345 +1102,362 +276,358 +786,373 +272,40 +448,295 +154,124 +1297,493 +306,698 +269,176 +1089,292 +791,28 +584,460 +1014,483 +179,724 +495,501 +947,852 +420,660 +1218,358 +75,463 +850,633 +663,254 +602,696 +887,276 +32,37 +358,231 +619,514 +115,481 +1071,87 +1215,522 +137,52 +547,206 +547,210 +579,871 +1203,654 +1126,851 +1266,138 +1183,696 +768,18 +997,717 +536,829 +373,302 +986,392 +199,605 +137,368 +373,592 +1156,124 +890,533 +803,500 +211,257 +648,850 +1004,38 +522,66 +269,381 +1072,714 +1250,588 +902,259 +803,595 +735,502 +1215,822 +333,94 +118,810 +1203,866 +711,715 +423,618 +656,858 +154,546 +577,617 +1310,858 +1223,747 +1150,232 +999,842 +30,409 +435,627 +818,750 +997,24 +241,262 +587,618 +75,879 +597,282 +925,16 +79,392 +644,93 +79,54 +360,37 +656,652 +1248,477 +48,312 +470,250 +848,366 +1146,876 +1054,877 +115,413 +455,15 +1092,373 +376,39 +982,225 +875,267 +492,893 +731,707 +162,704 +508,812 +641,292 +251,191 +328,669 +192,752 +808,212 +182,268 +485,882 +455,798 +231,170 +559,72 +855,798 +546,518 +179,453 +402,166 +979,789 +1158,796 +495,841 +42,565 +1019,42 +1116,822 +1074,434 +604,485 +599,715 +579,891 +475,371 +647,786 +1297,736 +398,809 +1064,785 +798,868 +1158,462 +716,844 +440,128 +1079,432 +616,305 +546,297 +160,610 +850,261 +417,441 +199,646 +221,292 +555,345 +803,394 +54,40 +55,302 +811,196 +810,348 +1098,765 +870,576 +1168,610 +1186,198 +557,186 +969,870 +1231,726 +1193,567 +1235,15 +962,682 +495,5 +483,868 +118,65 +1049,522 +13,288 +50,607 +1302,658 +1096,766 +147,441 +298,197 +152,656 +366,618 +907,324 +547,152 +850,185 +408,259 +98,3 +348,212 +711,179 +1192,829 +1218,582 +1125,63 +1255,302 +470,436 +187,676 +1039,201 +764,737 +480,436 +167,497 +793,746 +139,628 +723,46 +927,302 +773,343 +517,746 +264,85 +920,253 +237,413 +880,744 +1076,358 +600,37 +634,474 +132,847 +398,85 +147,453 +1245,113 +184,868 +1310,312 +944,365 +947,462 +529,233 +144,66 +169,458 +376,407 +659,565 +928,406 +928,488 +378,234 +167,273 +619,96 +127,463 +616,589 +875,491 +1016,373 +234,88 +288,141 +154,796 +492,592 +206,295 +189,37 +120,487 +851,192 +1158,210 +1136,458 +465,609 +64,451 +338,882 +426,3 +599,742 +1116,707 +326,225 +1310,36 +827,644 +877,740 +823,341 +701,367 +591,121 +107,206 +127,737 +1297,323 +1154,672 +226,137 +862,422 +1039,21 +199,248 +666,93 +788,187 +808,236 +980,815 +452,618 +634,420 +75,187 +540,353 +508,364 +1213,840 +13,401 +934,487 +442,801 +1144,647 +20,429 +830,10 +962,822 +527,96 +1064,635 +927,469 +1278,93 +763,476 +162,381 +884,387 +843,101 +666,98 +1163,441 +1253,598 +1111,289 +137,688 +507,724 +628,93 +893,441 +669,323 +937,592 +348,38 +1305,772 +382,37 +335,674 +781,233 +316,436 +763,152 +98,731 +1255,750 +1012,697 +112,474 +850,374 +194,72 +30,738 +60,306 +912,85 +152,210 +950,521 +408,707 +554,361 +1185,233 +50,633 +1084,305 +401,817 +485,22 +360,521 +1236,504 +753,759 +787,702 +577,169 +415,807 +1039,469 +1257,798 +313,870 +786,521 +67,724 +349,831 +830,458 +1176,473 +843,666 +917,522 +540,317 +554,346 +189,78 +589,217 +124,170 +873,667 +494,284 +60,418 +875,851 +691,870 +1299,89 +950,821 +524,521 +1228,364 +641,323 +1198,585 +915,500 +765,826 +65,217 +770,353 +1268,565 +30,95 +199,319 +969,269 +60,476 +649,772 +798,610 +845,609 +676,474 +1215,72 +900,707 +1031,481 +985,829 +1081,113 +559,432 +1041,612 +1168,436 +1091,627 +259,842 +644,25 +48,821 +276,234 +822,247 +170,222 +462,80 +60,364 +763,236 +475,523 +435,267 +663,786 +984,225 +49,86 +564,574 +512,610 +295,310 +470,644 +226,753 +981,381 +825,12 +868,885 +850,66 +114,200 +1034,660 +1034,536 +1084,757 +356,645 +134,697 +663,168 +1280,794 +154,616 +219,43 +669,602 +882,305 +912,877 +902,707 +80,147 +333,22 +475,724 +114,423 +179,441 +353,816 +373,78 +1126,362 +378,633 +1091,267 +127,605 +1004,698 +1115,422 +152,124 +152,238 +840,436 +37,596 +209,103 +574,859 +1038,406 +423,870 +329,829 +835,5 +947,880 +599,40 +546,597 +704,373 +107,654 +325,829 +1143,497 +1231,392 +783,463 +132,299 +199,289 +199,575 +604,760 +1196,200 +882,589 +599,179 +355,555 +803,170 +1190,487 +296,540 +622,373 +0,134 +494,732 +599,88 +1039,267 +580,504 +1118,142 +1076,458 +1225,122 +698,248 +171,26 +748,361 +1054,448 +1009,302 +1091,806 +868,801 +816,732 +816,367 +634,17 +873,227 +711,854 +1131,75 +488,303 +1173,526 +405,49 +565,33 +156,782 +401,705 +169,882 +977,648 +1215,462 +515,852 +716,50 +1310,582 +1146,18 +1220,798 +256,448 +1173,746 +328,225 +5,682 +810,546 +518,37 +825,22 +73,593 +928,40 +48,582 +1001,196 +1143,341 +1262,760 +1235,707 +1044,844 +835,276 +319,474 +1136,686 +893,840 +601,798 +1144,695 +256,85 +502,236 +1179,360 +319,226 +1230,411 +830,436 +909,705 +972,78 +875,627 +507,500 +490,876 +95,432 +430,296 +284,117 +741,772 +815,393 +1255,693 +713,765 +152,18 +771,413 +1163,394 +62,346 +783,157 +862,295 +127,289 +902,310 +950,73 +940,86 +751,598 +1014,354 +1166,66 +770,149 +1014,299 +160,284 +1307,777 +602,24 +437,667 +403,324 +237,481 +1019,380 +1039,592 +597,737 +383,21 +612,777 +994,353 +420,434 +1303,786 +475,819 +1096,16 +320,492 +1215,820 +1148,381 +1173,74 +50,756 +3,777 +649,212 +291,490 +273,292 +195,254 +55,806 +1227,726 +626,421 +75,529 +0,652 +502,682 +1029,367 +470,868 +1101,103 +835,371 +97,840 +686,95 +98,611 +1026,729 +517,368 +172,124 +545,219 +238,266 +572,866 +1278,37 +731,871 +1255,152 +35,422 +1101,343 +691,798 +1091,536 +763,742 +57,737 +738,866 +1084,753 +120,645 +1175,866 +783,96 +1243,618 +972,423 +6,558 +604,16 +887,618 +214,128 +1253,150 +1026,777 +234,458 +793,526 +1088,203 +117,327 +1218,88 +27,675 +994,126 +1054,533 +492,302 +1290,598 +562,689 +137,746 +1158,434 +1290,868 +1126,868 +62,253 +244,707 +88,833 +324,79 +1034,633 +1091,403 +335,726 +694,589 +420,436 +1129,320 +1268,613 +1111,319 +268,691 +108,821 +591,773 +547,294 +502,212 +530,276 +72,889 +1300,796 +808,434 +435,206 +1220,677 +326,673 +1201,675 +1086,296 +962,196 +1256,653 +16,812 +845,543 +11,89 +701,815 +166,695 +1222,490 +730,504 +319,420 +502,695 +174,10 +38,529 +1246,443 +644,210 +783,320 +798,162 +353,509 +793,368 +1150,214 +95,208 +984,381 +1300,93 +390,473 +594,844 +935,481 +494,527 +920,473 +1300,126 +303,176 +348,408 +284,777 +609,703 +929,292 +1198,757 +990,558 +1123,676 +607,376 +1230,35 +1029,302 +13,323 +1059,703 +669,571 +1190,743 +686,156 +498,728 +721,677 +1029,527 +986,502 +1294,812 +606,582 +1235,529 + +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/day13/src/main.rs b/day13/src/main.rs new file mode 100644 index 0000000..b9be444 --- /dev/null +++ b/day13/src/main.rs @@ -0,0 +1,71 @@ +use std::{fs, env, collections::HashSet}; + +fn read_input(path: &str) -> String { + return fs::read_to_string(path).expect("Cannot read file."); +} + +fn parse_input(s: &str) -> (HashSet<(i32, i32)>, Vec<(char, i32)>) { + let mut is_fold = false; + let mut t = HashSet::new(); + let mut i = vec![]; + for l in s.lines() { + if l == "" { is_fold = true; continue; } + if !is_fold { + let x = l.split_once(",").unwrap(); + t.insert((x.0.parse().unwrap(), x.1.parse().unwrap())); + } else { + let x = l.split_once("=").unwrap(); + i.push((x.0.chars().last().unwrap(), x.1.parse().unwrap())); + } + } + return (t, i); +} + +fn apply_n_folds(i: &HashSet<(i32, i32)>, f: &Vec<(char, i32)>, n: usize) -> HashSet<(i32, i32)> { + let mut h = i.clone(); + for x in 0..n { + for c in h.clone().iter() { + if f[x].0 == 'y' { + h.insert((c.0, if c.1 > f[x].1 { c.1 - (c.1 - f[x].1) * 2 } else { c.1 })); + if c.1 > f[x].1 { + h.remove(c); + } + } else { + h.insert((if c.0 > f[x].1 { c.0 - (c.0 - f[x].1) * 2 } else { c.0 }, c.1)); + if c.0 > f[x].1 { + h.remove(c); + } + } + } + } + return h; +} + +fn show_array(i: &HashSet<(i32, i32)>) { + let m = i.iter().map(|e| e.0).max().unwrap(); + let n = i.iter().map(|e| e.1).max().unwrap(); + for y in 0..=n { + print!("\t\t"); + for x in 0..=m { + if i.contains(&(x, y)) { + print!("{}", '#'); + } else { + print!(" "); + } + } + println!(); + } +} + +fn main() { + let args: Vec = env::args().collect(); + for arg in args.iter().skip(1) { + let input = read_input(&arg); + let vec_in = parse_input(&input); + println!("[{}]", &arg); + println!("\t[Part 1] => Answer is '{}'.", apply_n_folds(&vec_in.0, &vec_in.1, 1).len()); + println!("\t[Part 2] => Answer is:"); + show_array(&apply_n_folds(&vec_in.0, &vec_in.1, vec_in.1.len())); + } +} +