This commit is contained in:
Shadowfacts 2021-12-13 10:49:58 -05:00
parent 1bf6e92261
commit 7b313769db
3 changed files with 997 additions and 1 deletions

840
input/day13.txt Normal file
View File

@ -0,0 +1,840 @@
602,683
1297,593
1118,603
534,850
221,383
524,835
735,194
117,872
1233,648
1160,509
278,558
20,697
622,675
60,217
120,460
296,505
206,29
20,131
73,791
1014,535
708,473
1042,371
1243,672
1197,798
1218,586
62,885
48,843
880,170
788,729
816,401
969,47
1119,567
335,721
160,45
718,120
666,427
295,815
850,109
1208,618
388,427
177,481
149,481
676,197
216,708
646,696
206,546
641,749
242,353
1250,421
520,568
234,614
1218,308
817,579
934,586
84,737
244,676
1208,564
1220,409
473,665
426,740
1118,792
928,201
1061,213
577,200
862,641
892,492
150,201
898,322
641,847
848,703
392,329
1266,341
228,712
494,493
838,271
1148,778
1146,86
493,91
1126,105
576,551
234,651
838,267
561,502
606,801
1062,521
1074,402
166,45
460,480
1308,103
1067,476
1250,116
15,592
865,358
979,121
1014,743
848,102
1198,311
120,253
358,599
316,299
520,326
1116,148
1160,201
880,778
736,295
335,173
438,809
1178,544
74,565
902,659
831,859
564,185
776,44
376,275
125,812
922,330
130,205
671,317
92,308
295,303
771,744
492,534
380,840
902,683
65,86
887,448
1032,765
120,641
488,339
1293,23
733,470
952,595
909,831
99,642
166,555
922,841
1118,352
191,719
401,735
652,791
131,255
18,827
1000,488
1178,527
850,480
474,712
232,562
294,185
539,744
395,105
776,64
231,253
1292,382
954,408
488,401
382,649
842,427
634,131
150,649
1302,827
462,792
1148,116
262,742
716,217
729,159
425,366
463,175
1044,427
1228,703
668,61
1294,854
1062,373
79,194
296,702
659,243
773,872
806,833
671,302
686,299
480,722
88,75
92,243
882,607
850,253
1012,856
1032,250
410,71
142,789
1262,597
647,397
622,877
160,712
646,205
1108,885
582,575
771,150
624,299
411,726
132,445
505,861
376,558
974,56
132,320
877,635
624,254
629,689
746,402
1114,129
815,848
298,67
1056,716
356,255
356,827
624,147
216,123
142,341
900,218
910,709
1168,789
954,827
818,330
492,360
629,17
664,319
602,435
196,241
974,491
970,285
350,75
1299,894
1285,205
884,488
72,166
1168,322
370,178
1280,819
1220,485
321,844
95,357
90,485
77,470
8,67
298,603
934,308
644,369
192,191
248,359
575,334
298,291
62,120
100,288
903,642
147,543
898,775
1178,599
719,476
1062,7
1118,827
1118,219
708,883
401,831
1295,78
917,178
472,715
885,528
102,134
1116,752
3,46
1118,675
1081,156
22,371
184,789
1015,591
644,427
417,343
1307,46
755,866
954,255
325,159
151,213
32,877
1210,288
1119,623
82,683
60,778
989,50
348,322
708,683
278,765
336,295
733,694
560,292
564,402
1049,758
1094,771
412,322
1044,521
900,659
1245,30
1290,770
509,247
576,103
686,75
705,574
110,339
934,619
676,763
294,644
192,827
124,457
231,701
1076,243
102,842
192,281
1193,134
788,165
736,31
1084,607
1114,241
1009,205
1094,533
336,765
102,564
458,241
838,715
214,815
684,774
175,859
676,131
838,579
406,821
249,213
855,806
848,227
1222,75
1116,253
214,729
32,628
1168,372
295,79
393,30
1114,404
1033,320
1245,86
734,343
1216,292
602,473
807,651
520,165
1140,397
151,651
504,649
1213,366
918,565
263,777
600,693
326,723
336,129
571,642
1232,723
130,59
783,582
107,757
1057,894
1218,651
1250,217
167,885
1033,126
562,19
191,175
848,542
181,199
1280,864
769,684
1193,872
1098,653
1198,79
1159,651
562,586
458,404
1168,329
248,439
624,523
1200,339
406,535
544,85
584,301
435,842
571,54
512,372
651,243
1034,600
132,747
756,158
771,879
537,648
922,229
227,700
676,770
1180,689
769,798
1068,289
310,488
130,891
1047,441
401,63
1089,383
952,702
749,392
134,326
410,676
1101,694
18,830
594,105
440,815
82,703
460,871
1176,326
758,564
818,116
160,849
807,203
102,276
221,831
1145,222
8,632
965,871
1175,175
577,718
60,116
1036,449
790,568
52,446
1012,603
1089,259
766,533
1048,742
602,11
880,676
716,116
569,437
94,12
975,173
102,341
509,329
360,560
718,774
142,249
329,735
12,61
316,859
84,775
838,495
43,117
492,116
592,774
787,527
488,567
534,460
932,294
606,93
227,553
602,448
458,852
248,887
1250,373
602,813
1233,669
524,675
1232,171
1096,135
328,310
917,30
1278,266
20,763
644,817
1278,877
1036,445
462,227
1226,775
594,169
1036,813
472,271
433,259
918,329
962,315
840,789
595,35
1280,613
194,253
962,572
733,1
1175,551
726,301
52,11
1295,592
885,156
214,135
1144,45
676,579
552,217
639,86
716,677
458,42
294,485
425,290
594,725
1068,353
728,575
719,418
1302,487
974,151
527,582
987,121
333,498
212,138
1064,843
1226,723
900,823
209,200
969,369
1116,673
175,707
32,516
1268,121
880,249
1133,145
875,894
1150,45
1077,726
1091,757
1238,268
221,187
571,28
77,225
219,620
1086,329
102,330
984,275
156,721
95,607
1232,383
728,709
271,506
160,182
97,696
296,432
167,9
224,106
1128,579
740,801
974,743
1101,200
196,490
705,350
818,267
209,694
1292,399
837,665
1303,845
107,836
428,793
733,893
440,576
323,773
162,778
734,882
296,417
1150,182
1280,371
268,523
1211,252
472,267
1213,528
786,891
629,205
985,159
428,607
852,490
82,647
231,0
25,689
459,703
1208,330
28,588
1057,103
199,88
430,170
395,789
805,861
62,437
788,126
130,577
338,747
904,551
733,718
816,600
195,703
974,407
356,603
1193,806
602,367
674,67
1262,51
1203,757
773,22
887,446
1044,467
2,551
591,453
102,106
581,287
723,131
651,651
716,105
242,373
308,719
1208,665
1036,459
710,649
664,696
1076,84
1216,602
1178,449
828,408
54,245
554,549
10,525
728,319
734,103
30,96
1228,683
960,75
768,583
1144,339
331,121
298,186
1160,833
472,623
341,47
624,371
433,635
1066,683
1042,523
1298,61
90,492
1039,670
626,120
1278,516
112,79
54,201
410,659
996,285
524,219
60,617
1086,149
830,620
534,512
805,33
1290,197
410,218
1196,821
129,15
430,676
986,847
1303,49
1210,345
1154,397
1067,418
321,50
740,871
1268,684
816,787
534,64
750,292
412,775
376,308
301,205
482,486
1079,253
1104,796
534,253
1159,691
151,233
740,775
1126,789
535,588
296,295
1258,473
192,352
410,255
336,39
1220,709
1079,894
1111,88
473,199
1056,166
166,742
494,271
1248,550
1089,187
1054,476
1208,553
1215,565
1044,564
160,555
132,893
681,877
831,187
577,1
8,262
398,749
892,402
1082,182
1156,498
984,723
318,151
423,448
391,441
806,385
1133,481
298,197
594,217
1292,64
418,492
1238,136
1226,23
1215,537
147,95
1237,791
1036,670
684,421
1280,23
266,521
1091,274
589,86
1208,394
1119,719
78,723
135,343
1261,126
80,683
940,268
65,850
234,691
400,716
1048,210
32,334
927,388
821,222
758,106
196,129
1135,651
890,467
93,392
1158,620
8,39
715,35
806,201
97,366
997,847
438,634
246,683
102,394
60,169
10,77
1082,712
488,493
370,268
156,397
1218,728
30,595
77,246
107,137
904,535
987,773
344,406
1308,551
324,847
348,315
1118,281
1278,334
658,103
426,236
462,165
426,658
659,46
1168,645
972,77
406,73
42,684
1014,505
226,607
8,407
728,185
313,847
1178,893
1166,399
848,729
452,9
542,352
1280,595
524,689
105,259
1068,745
1081,80
639,368
756,233
639,317
92,586
1116,221
112,718
624,747
1233,1
1215,607
1039,388
382,245
1295,86
582,185
952,599
440,191
458,689
62,550
186,467
1084,287
1245,416
790,820
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

155
src/day13.rs Normal file
View File

@ -0,0 +1,155 @@
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
pub fn day13() {
// 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
// "#;
let input = include_str!("../input/day13.txt");
let (grid, folds) = parse(input);
println!("{}x{}", grid.width, grid.height);
// dbg!(&grid);
// dbg!(&folds);
// dbg!(grid.fold(&folds[0]));
// dbg!(grid.fold(&folds[0]).fold(&folds[1]));
let grid_after_one_fold = grid.fold(&folds[0]);
// println!(
// "{}x{}",
// grid_after_one_fold.width, grid_after_one_fold.height
// );
// // dbg!(&grid_after_one_fold);
println!("dots visible: {}", grid_after_one_fold.dots_visible());
dbg!(folds.iter().fold(grid, |g, f| g.fold(f)));
}
fn parse(input: &str) -> (Grid, Vec<Fold>) {
let (grid, folds) = input.trim().split_once("\n\n").unwrap();
let folds = folds
.lines()
.map(|l| {
let (s, n) = l.trim().split_once("=").unwrap();
let coord = n.parse::<usize>().unwrap();
match s.chars().last() {
Some('x') => Fold::X(coord),
Some('y') => Fold::Y(coord),
_ => panic!(),
}
})
.collect::<Vec<_>>();
let points = grid
.lines()
.map(|l| {
let (x, y) = l.trim().split_once(",").unwrap();
let x = x.parse::<usize>().unwrap();
let y = y.parse::<usize>().unwrap();
(x, y)
})
.collect::<HashSet<_>>();
let width = points.iter().map(|(x, _)| x).max().unwrap() + 1;
let height = points.iter().map(|(_, y)| y).max().unwrap() + 1;
(
Grid {
width,
height,
points,
},
folds,
)
}
struct Grid {
width: usize,
height: usize,
points: HashSet<(usize, usize)>,
}
impl Debug for Grid {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}x{}\n", self.width, self.height).expect("write");
for y in 0..self.height {
for x in 0..self.width {
if self.points.contains(&(x, y)) {
write!(f, "#").expect("write");
} else {
write!(f, ".").expect("write");
}
}
write!(f, "\n").expect("write");
}
write!(f, "\n")
}
}
impl Grid {
fn fold(&self, fold: &Fold) -> Grid {
match fold {
Fold::X(n) => {
assert!(self.width % 2 == 1);
let new_points = self
.points
.iter()
.map(|&(x, y)| {
let new_x = if x > *n { n - (x - n) } else { x };
(new_x, y)
})
.collect::<HashSet<_>>();
Grid {
width: *n,
height: self.height,
points: new_points,
}
}
Fold::Y(n) => {
assert!(self.height % 2 == 1);
let new_points = self
.points
.iter()
.map(|&(x, y)| {
let new_y = if y > *n { n - (y - n) } else { y };
(x, new_y)
})
.collect::<HashSet<_>>();
Grid {
width: self.width,
height: *n,
points: new_points,
}
}
}
}
fn dots_visible(&self) -> usize {
self.points.len()
}
}
#[derive(Debug)]
enum Fold {
X(usize),
Y(usize),
}

View File

@ -11,7 +11,8 @@ mod day09;
mod day10; mod day10;
mod day11; mod day11;
mod day12; mod day12;
mod day13;
fn main() { fn main() {
day12::day12(); day13::day13();
} }