commit 4558ff0cfc17b0e9b8b2c94001a5f65714cc65e3
parent db0cddff1d0b71add22acede3da743b6083139f3
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Mon, 13 Dec 2021 21:13:53 -0600
Add 2021 day 13
Diffstat:
A | 2021/day13/input.txt | | | 1035 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2021/day13/main.rs | | | 101 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 1136 insertions(+), 0 deletions(-)
diff --git a/2021/day13/input.txt b/2021/day13/input.txt
@@ -0,0 +1,1035 @@
+388,758
+82,197
+689,684
+140,677
+616,14
+206,661
+1110,215
+408,700
+830,773
+808,257
+1221,824
+7,632
+575,208
+792,847
+264,427
+555,443
+12,141
+1153,52
+350,105
+832,200
+1196,133
+371,427
+606,56
+1136,113
+50,803
+110,352
+773,572
+243,103
+213,835
+340,701
+114,761
+1149,831
+1150,873
+1293,42
+288,266
+246,252
+644,781
+75,254
+619,130
+1280,98
+704,254
+291,421
+199,309
+5,528
+95,586
+835,464
+647,822
+131,763
+52,872
+388,337
+387,173
+398,815
+221,25
+1163,240
+1206,448
+60,337
+990,570
+1295,206
+609,36
+224,396
+330,220
+480,513
+1278,856
+82,553
+48,56
+22,889
+1288,889
+534,42
+142,3
+167,801
+10,593
+1104,714
+1006,677
+766,266
+162,4
+1228,197
+765,0
+120,39
+977,528
+1101,808
+1176,514
+960,784
+42,359
+661,891
+1073,294
+798,386
+684,695
+676,310
+324,674
+510,525
+967,686
+1289,880
+586,872
+885,704
+858,487
+803,317
+687,355
+1046,147
+1113,700
+862,320
+3,60
+743,665
+1022,722
+1113,194
+49,112
+1111,197
+38,284
+1257,490
+221,869
+316,469
+15,240
+130,840
+689,418
+470,834
+689,847
+82,777
+592,355
+1,357
+950,89
+1240,805
+701,877
+537,572
+264,747
+735,215
+895,757
+917,654
+1218,714
+1190,437
+758,225
+721,486
+120,478
+373,262
+1034,243
+915,385
+1280,616
+398,761
+388,115
+1058,226
+1019,322
+134,254
+808,637
+276,131
+869,143
+1210,604
+338,301
+1109,691
+132,355
+472,525
+803,179
+562,77
+200,215
+990,324
+1263,642
+1113,343
+110,28
+1128,369
+652,873
+903,446
+462,276
+47,642
+1029,317
+718,355
+224,758
+523,579
+626,806
+305,96
+1049,38
+572,697
+231,756
+1062,817
+167,162
+858,266
+551,397
+576,77
+890,716
+202,855
+798,60
+227,219
+904,77
+606,640
+42,631
+954,619
+199,36
+1141,516
+92,180
+398,481
+639,602
+1242,694
+806,709
+840,219
+462,23
+706,68
+152,749
+1200,423
+554,290
+45,877
+1141,305
+644,749
+304,341
+798,743
+589,38
+162,61
+130,189
+1305,366
+932,378
+776,437
+454,679
+1218,77
+1034,875
+623,275
+360,89
+192,254
+256,514
+1036,135
+380,135
+262,31
+427,219
+304,105
+477,98
+922,337
+490,115
+1006,789
+950,486
+1261,334
+554,604
+743,89
+142,443
+497,141
+216,807
+1166,315
+586,22
+1197,507
+452,822
+534,437
+887,21
+808,325
+1180,189
+760,448
+1300,717
+663,710
+872,632
+1118,393
+758,669
+283,19
+1295,715
+445,506
+579,873
+755,443
+35,714
+557,190
+825,141
+890,357
+181,714
+579,469
+872,362
+1158,738
+1277,684
+206,350
+1241,338
+425,704
+393,486
+69,492
+704,702
+197,194
+1305,528
+375,642
+227,675
+989,753
+157,814
+907,229
+169,516
+427,667
+813,585
+856,215
+920,595
+676,445
+1143,610
+666,849
+142,71
+1190,478
+403,404
+288,432
+261,480
+324,220
+986,674
+1168,584
+773,632
+858,407
+830,369
+858,628
+706,602
+878,89
+661,305
+1241,492
+962,487
+522,539
+1019,473
+493,63
+907,665
+872,490
+70,626
+813,18
+80,700
+616,598
+954,863
+75,192
+912,305
+87,677
+616,180
+117,434
+1260,740
+606,254
+454,7
+199,137
+589,654
+1228,341
+811,38
+398,79
+94,37
+574,529
+1081,190
+79,740
+0,252
+1231,679
+830,781
+933,499
+721,78
+1094,418
+1111,309
+330,21
+45,240
+377,334
+288,682
+373,673
+721,856
+570,408
+602,501
+1228,553
+120,855
+480,121
+15,206
+572,562
+1089,25
+213,59
+738,102
+1064,866
+1108,520
+438,535
+323,529
+129,253
+1158,156
+537,460
+822,194
+192,200
+110,476
+10,85
+1046,299
+109,894
+980,220
+1283,467
+150,185
+329,724
+976,570
+1002,315
+1111,36
+830,121
+102,225
+962,407
+298,619
+182,91
+1017,418
+842,229
+288,880
+790,730
+1094,476
+684,88
+237,602
+825,826
+1014,796
+520,730
+75,425
+42,487
+567,142
+422,760
+1048,863
+209,684
+350,784
+1064,2
+1309,537
+237,826
+1111,757
+510,763
+977,590
+333,690
+202,39
+671,179
+820,844
+870,154
+261,38
+502,637
+869,591
+787,171
+982,179
+972,693
+893,294
+970,701
+579,453
+415,757
+830,829
+1200,418
+1183,445
+169,68
+262,863
+570,800
+462,254
+1153,814
+1062,301
+366,180
+589,856
+684,724
+403,665
+664,686
+798,823
+1228,777
+740,184
+1022,880
+512,732
+1129,602
+565,70
+738,197
+642,417
+298,863
+202,72
+599,816
+1098,814
+209,808
+848,276
+890,292
+972,85
+512,823
+32,632
+1158,290
+1170,677
+1063,446
+560,37
+858,72
+507,577
+1146,844
+982,4
+82,697
+256,305
+862,233
+1268,535
+398,849
+785,894
+1146,50
+234,740
+791,427
+202,520
+441,59
+134,871
+979,305
+1216,857
+78,525
+1126,893
+475,627
+552,3
+626,88
+468,665
+206,768
+1248,539
+1141,640
+1022,628
+1094,87
+825,533
+937,572
+507,403
+475,688
+1076,357
+462,871
+1168,151
+294,581
+671,878
+1181,103
+922,785
+356,479
+994,578
+1094,212
+1129,714
+649,891
+6,548
+944,722
+164,498
+291,572
+259,609
+82,341
+262,656
+214,147
+1216,417
+1298,589
+748,817
+410,556
+792,47
+688,301
+1145,495
+99,78
+209,86
+848,501
+232,477
+80,866
+1136,145
+880,808
+388,86
+330,674
+1101,388
+20,86
+212,894
+1111,137
+294,352
+808,290
+115,383
+668,365
+753,38
+994,757
+688,262
+691,578
+1016,352
+704,646
+1310,642
+1305,814
+903,448
+256,66
+869,255
+261,341
+1241,556
+441,303
+1084,403
+1108,498
+865,506
+65,253
+579,889
+647,374
+837,826
+1231,663
+1160,709
+616,374
+341,98
+288,714
+616,618
+31,171
+457,194
+775,203
+644,593
+1002,579
+830,513
+790,619
+589,266
+493,556
+487,284
+378,378
+623,355
+537,322
+738,117
+602,22
+1288,217
+803,184
+134,514
+622,133
+1262,56
+1059,192
+1208,808
+1029,770
+246,700
+32,128
+1310,252
+1190,39
+654,700
+885,477
+731,565
+1126,227
+678,285
+1153,466
+512,443
+110,184
+314,305
+539,460
+212,313
+666,101
+890,537
+587,138
+512,60
+174,113
+388,785
+756,604
+264,355
+1302,14
+247,441
+544,756
+1268,631
+273,222
+244,33
+951,453
+1141,68
+497,309
+1104,96
+206,96
+644,145
+556,415
+356,514
+1231,602
+510,131
+393,273
+1067,880
+674,333
+1086,808
+62,539
+1113,399
+1153,500
+647,72
+743,581
+1049,592
+129,791
+750,470
+933,334
+987,529
+1245,78
+504,207
+162,833
+589,553
+883,219
+321,141
+1113,495
+902,642
+1288,5
+1310,267
+216,164
+102,274
+1118,276
+92,525
+197,399
+783,893
+1108,479
+1174,159
+1126,360
+1290,808
+360,581
+1083,219
+574,642
+858,863
+1166,561
+1098,894
+398,753
+179,206
+441,751
+1163,715
+520,66
+1101,210
+770,366
+823,610
+721,408
+537,758
+869,751
+209,210
+922,86
+1230,642
+653,226
+1272,284
+1235,665
+346,579
+782,679
+144,315
+735,208
+338,85
+1160,687
+403,814
+622,486
+1078,477
+1216,865
+869,415
+216,642
+1002,333
+903,14
+1250,337
+726,268
+1268,359
+333,304
+825,753
+15,878
+1168,443
+490,844
+1235,254
+448,238
+1193,460
+243,14
+736,642
+1158,705
+97,486
+209,360
+89,742
+448,296
+572,341
+120,416
+1104,855
+765,894
+818,331
+216,58
+311,715
+237,376
+165,495
+677,798
+206,208
+838,707
+261,240
+933,789
+243,343
+535,243
+552,172
+99,667
+1193,322
+853,103
+321,761
+276,875
+604,763
+705,782
+197,551
+621,476
+169,254
+261,592
+1044,360
+494,873
+816,469
+1063,448
+430,136
+1300,289
+1272,724
+734,598
+438,252
+1063,292
+994,469
+1094,164
+676,893
+820,115
+986,220
+169,305
+525,558
+484,168
+1148,509
+304,553
+1268,855
+803,240
+562,714
+808,604
+907,404
+442,56
+69,338
+552,669
+1067,103
+1004,758
+462,393
+388,557
+28,247
+703,831
+1193,710
+348,39
+495,541
+1282,170
+848,871
+1062,632
+1141,204
+935,642
+281,770
+1029,184
+1310,4
+83,880
+1143,801
+1198,358
+109,0
+1067,14
+606,248
+1303,486
+393,592
+1183,449
+112,222
+570,352
+242,800
+338,177
+21,880
+653,21
+826,726
+1176,171
+644,301
+817,63
+119,243
+994,809
+402,220
+770,390
+117,710
+895,137
+646,686
+1200,476
+813,141
+644,849
+502,604
+1298,141
+320,324
+981,537
+644,101
+994,593
+181,826
+402,668
+706,131
+721,273
+291,473
+790,207
+169,640
+157,309
+1096,747
+771,831
+964,561
+53,84
+684,199
+633,798
+8,686
+740,408
+605,334
+490,442
+761,694
+209,460
+1143,732
+475,533
+887,362
+981,170
+817,556
+248,262
+1006,105
+1211,227
+688,593
+825,690
+549,694
+1190,855
+161,831
+706,378
+512,386
+835,516
+179,267
+1165,180
+264,299
+1037,490
+181,68
+206,180
+888,760
+440,740
+552,225
+276,763
+420,537
+232,29
+972,301
+960,609
+1304,548
+21,677
+114,133
+308,333
+15,715
+880,225
+333,133
+1034,763
+94,857
+833,770
+1022,14
+1168,162
+783,130
+1211,78
+1300,301
+32,766
+248,301
+606,838
+1037,222
+545,446
+293,399
+443,12
+1012,754
+912,761
+961,798
+1232,296
+1046,595
+785,641
+682,287
+1245,553
+1096,147
+485,578
+1088,112
+535,203
+62,355
+738,777
+1200,710
+735,740
+304,110
+766,660
+694,714
+735,154
+1180,840
+167,732
+329,805
+209,388
+420,178
+1118,514
+278,374
+1178,91
+1200,471
+555,451
+567,581
+688,133
+1036,140
+330,873
+549,200
+915,61
+1153,585
+557,710
+216,490
+457,103
+706,740
+704,640
+306,136
+1118,694
+340,193
+35,124
+721,553
+800,259
+960,105
+634,893
+157,85
+484,726
+539,63
+900,786
+10,301
+570,497
+1126,534
+706,826
+1278,390
+622,856
+136,47
+604,740
+438,642
+1096,292
+2,642
+1193,584
+1272,276
+1073,68
+1216,134
+279,194
+619,578
+813,813
+1148,833
+222,112
+908,220
+0,890
+769,507
+951,5
+1216,477
+540,504
+900,645
+2,252
+427,3
+487,162
+415,889
+152,829
+1104,544
+858,822
+1083,667
+823,93
+574,252
+68,246
+206,574
+234,357
+527,893
+1048,656
+1153,309
+601,404
+944,14
+869,59
+1062,262
+716,758
+452,844
+1054,33
+356,619
+621,418
+922,115
+1062,77
+1272,732
+251,254
+950,408
+755,451
+113,507
+1126,1
+247,448
+462,501
+406,525
+12,589
+731,192
+1118,619
+954,514
+708,393
+338,501
+420,716
+1223,752
+251,192
+114,528
+10,605
+359,441
+1260,803
+1022,714
+621,847
+1213,408
+1066,504
+644,641
+1131,267
+1231,154
+32,537
+579,329
+766,138
+1017,866
+798,732
+
+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/main.rs b/2021/day13/main.rs
@@ -0,0 +1,101 @@
+use std::collections::HashSet;
+
+#[derive(Debug, Copy, Clone)]
+enum FoldInstruction {
+ X(i32),
+ Y(i32),
+}
+
+impl FoldInstruction {
+ fn new(s: &str) -> FoldInstruction {
+ let mut it = s.split("=");
+ let axis = it.next().unwrap();
+ let coord = it.next().unwrap().parse::<i32>().unwrap();
+ match axis.chars().last() {
+ Some('x') => FoldInstruction::X(coord),
+ Some('y') => FoldInstruction::Y(coord),
+ _ => unreachable!(),
+ }
+ }
+
+ fn fold(&self, paper: &HashSet<(i32, i32)>) -> HashSet<(i32, i32)> {
+ let mut folded_paper = HashSet::new();
+ for (x, y) in paper {
+ match self {
+ Self::X(x_fold) => match x.cmp(x_fold) {
+ std::cmp::Ordering::Less => {
+ folded_paper.insert((*x, *y));
+ }
+ std::cmp::Ordering::Greater => {
+ folded_paper.insert((x_fold - (*x - x_fold), *y));
+ }
+ // folded axis is discarded
+ std::cmp::Ordering::Equal => (),
+ },
+ Self::Y(y_fold) => match y.cmp(y_fold) {
+ std::cmp::Ordering::Less => {
+ folded_paper.insert((*x, *y));
+ }
+ std::cmp::Ordering::Greater => {
+ folded_paper.insert((*x, y_fold - (*y - y_fold)));
+ }
+ std::cmp::Ordering::Equal => (),
+ },
+ }
+ }
+ folded_paper
+ }
+}
+
+fn main() {
+ let input = std::fs::read_to_string("input.txt")
+ .unwrap()
+ .trim()
+ .split('\n')
+ .map(|s| s.to_string())
+ .collect::<Vec<String>>();
+ let split = input.iter().position(|s| s == "").unwrap();
+ let paper = input[..split]
+ .iter()
+ .map(|s| {
+ let mut it = s.split(",");
+ (
+ it.next().unwrap().parse::<i32>().unwrap(),
+ it.next().unwrap().parse::<i32>().unwrap(),
+ )
+ })
+ .collect::<HashSet<_>>();
+ let fold_instructions = input[(split + 1)..]
+ .iter()
+ .map(|s| FoldInstruction::new(s))
+ .collect::<Vec<_>>();
+ // 842
+ println!("Part 1: {}", part_1(&paper, &fold_instructions));
+ // BFKRCJZU
+ println!("Part 2:");
+ part_2(&paper, &fold_instructions);
+}
+
+fn part_1(paper: &HashSet<(i32, i32)>, fold_instructions: &Vec<FoldInstruction>) -> usize {
+ fold_instructions[0].fold(paper).len()
+}
+
+fn plot(paper: &HashSet<(i32, i32)>) {
+ let x_max = *paper.iter().map(|(x, _)| x).max().unwrap();
+ let y_max = *paper.iter().map(|(_, y)| y).max().unwrap();
+ // need to flip x and y for letters to show up
+ for y in 0..=y_max {
+ for x in 0..=x_max {
+ print!("{}", if paper.contains(&(x, y)) { '#' } else { '.' });
+ }
+ println!();
+ }
+}
+
+fn part_2(paper: &HashSet<(i32, i32)>, fold_instructions: &Vec<FoldInstruction>) {
+ let mut folded_paper = paper.clone();
+ for fi in fold_instructions {
+ folded_paper = fi.fold(&folded_paper);
+ }
+ plot(&folded_paper);
+}