Compare commits

..

2 commits

Author SHA1 Message Date
b0821bc945 2021 day13/rust: add solution 2022-04-10 16:09:05 +02:00
3d52da6074 2021 day13: add data 2022-04-10 16:08:28 +02:00
4 changed files with 951 additions and 0 deletions

7
2021/data/day13.expected Normal file
View file

@ -0,0 +1,7 @@
704
#..#..##...##....##.###..####.#..#..##.
#..#.#..#.#..#....#.#..#.#....#..#.#..#
####.#....#..#....#.###..###..####.#...
#..#.#.##.####....#.#..#.#....#..#.#...
#..#.#..#.#..#.#..#.#..#.#....#..#.#..#
#..#..###.#..#..##..###..####.#..#..##.

852
2021/data/day13.input Normal file
View file

@ -0,0 +1,852 @@
43,593
1139,385
1178,51
216,679
743,185
1241,250
197,60
479,700
565,845
1022,330
744,714
440,535
137,81
35,766
482,249
1120,280
484,98
301,823
928,616
601,7
60,835
1092,371
1033,584
1079,833
44,665
469,777
915,526
759,290
843,891
1225,464
326,831
723,333
1099,362
962,285
1069,474
315,731
716,506
741,430
1228,320
1099,878
485,522
328,333
1289,432
1173,506
395,526
47,625
1155,273
114,856
914,826
1263,849
1294,693
607,289
1225,138
25,519
1051,564
738,747
67,493
1178,619
984,828
21,100
1086,241
644,563
164,287
738,131
1116,312
21,796
423,66
395,816
846,614
823,63
249,542
858,162
1273,448
684,623
139,443
184,388
977,71
499,621
810,133
1169,346
249,791
47,772
1170,128
530,162
594,387
972,752
1309,752
26,707
211,362
821,640
502,722
872,63
93,82
895,709
1113,445
567,338
592,448
567,556
552,277
815,735
10,470
400,120
807,835
1289,602
470,442
167,562
1094,735
848,467
1275,568
1009,868
264,829
907,95
894,714
807,59
768,199
470,218
141,346
644,22
85,430
693,670
489,640
748,555
535,560
1056,597
617,266
445,149
751,269
535,782
1046,494
372,677
62,848
1111,291
1048,313
1069,868
480,387
487,640
1151,463
725,194
1031,528
139,675
994,304
223,604
823,679
383,793
313,189
463,357
952,275
38,477
127,155
1156,245
1277,371
572,315
279,810
908,322
436,397
594,835
1031,93
1173,813
733,327
982,828
495,640
703,737
1017,299
390,0
99,327
1084,458
202,732
551,604
259,816
85,464
1083,525
301,26
822,352
703,157
539,434
550,194
868,710
197,893
758,586
736,176
633,889
115,460
256,80
651,672
1064,409
872,7
669,745
231,604
88,628
759,604
1287,694
171,826
1222,852
1250,836
1092,54
85,196
388,205
907,543
326,561
264,381
775,560
1069,71
60,836
277,584
375,801
117,813
1181,673
1150,128
54,278
718,446
577,327
1004,733
1084,794
909,497
975,807
561,171
244,819
505,441
393,64
502,172
517,872
268,28
1113,210
141,490
185,826
301,250
354,654
234,471
403,95
438,887
693,166
189,709
1243,493
87,543
1086,777
338,752
49,584
574,148
1039,607
1051,116
569,375
254,326
554,212
1255,271
1042,866
30,119
218,632
674,372
10,582
596,194
194,582
880,450
587,828
723,666
109,290
85,711
915,116
259,564
259,609
189,721
383,607
666,22
326,828
164,152
709,362
755,145
108,483
657,640
716,507
1,752
1272,788
664,46
316,470
882,458
693,266
1089,326
1121,519
1120,483
499,68
547,719
1066,355
1014,757
971,163
868,856
161,702
75,2
566,516
1178,760
266,651
1099,464
863,201
224,53
1113,1
811,273
177,453
775,245
1297,694
764,408
137,813
716,429
908,469
1225,127
1031,794
428,458
925,305
581,721
306,733
1258,854
241,138
1181,834
987,3
796,94
218,497
100,682
1180,122
184,218
1218,23
569,16
1250,725
1202,49
736,829
1059,674
684,735
428,346
1195,434
1064,485
430,450
224,761
749,364
711,549
276,7
403,367
855,291
375,541
85,586
353,644
467,667
50,617
1225,308
751,161
751,340
703,289
422,457
442,710
85,127
1307,670
1079,526
639,820
1217,171
378,613
952,585
731,450
402,693
847,357
195,696
25,362
1274,460
160,766
231,302
1084,548
927,773
1280,438
487,679
261,88
1054,80
129,221
1171,107
1195,12
920,0
851,248
828,649
1307,137
710,168
50,56
555,749
345,712
266,329
830,611
296,760
633,432
232,457
184,429
611,749
37,224
402,469
666,779
1217,364
423,340
296,7
443,878
378,10
1139,509
1228,635
629,698
539,205
910,297
500,133
684,271
703,605
21,453
1110,476
841,68
351,648
1252,780
234,646
780,641
308,715
416,180
957,250
935,532
761,863
401,397
1252,642
827,826
874,262
244,406
572,579
338,53
58,642
1307,56
771,84
465,501
1151,675
1151,735
981,752
1222,714
218,845
1088,707
249,775
54,341
423,246
1161,246
1181,508
644,116
58,114
160,128
843,667
872,831
202,477
909,397
1292,522
33,371
669,200
1265,409
529,672
395,337
512,282
331,786
1048,581
1161,453
261,64
587,333
443,654
703,177
1156,645
1289,100
692,434
141,565
517,282
436,840
679,648
825,372
174,75
249,352
438,7
3,670
169,522
331,108
766,231
1014,585
1133,453
965,40
887,246
1289,292
716,835
421,619
1263,772
226,794
500,649
962,609
1079,66
731,255
1156,494
677,100
175,700
423,648
10,115
1014,887
577,343
218,859
60,59
760,114
93,364
607,177
224,241
800,746
1126,218
617,670
1210,100
358,119
159,159
932,247
1201,290
115,834
10,779
1274,12
1250,506
259,285
428,436
565,497
333,708
979,672
587,66
1221,815
289,448
1290,733
130,772
503,835
1300,331
792,56
604,455
259,330
749,723
191,761
1094,511
189,185
1043,154
474,469
793,612
246,298
383,569
276,199
537,37
467,227
795,219
1273,180
1091,793
907,479
1280,775
917,830
495,431
868,184
1235,450
1309,142
157,820
402,322
3,137
470,429
167,780
1195,882
1069,250
775,782
1256,616
428,100
333,71
663,845
797,100
604,838
390,381
738,579
174,584
88,42
768,23
1222,266
1169,789
887,648
681,196
325,388
238,378
316,106
43,693
551,154
984,63
1260,617
430,138
671,820
918,357
1094,383
1069,756
75,639
599,246
856,476
117,81
546,75
1292,346
1059,220
8,400
1225,644
279,93
979,338
626,719
1203,623
594,220
1248,848
325,864
995,163
874,840
1049,88
954,175
282,217
132,51
269,358
1180,72
10,563
821,254
872,511
137,282
820,717
296,134
1078,457
634,733
21,546
1121,424
26,614
599,648
1133,441
110,131
604,199
1094,215
565,602
490,717
748,198
132,843
279,528
874,54
257,17
254,597
666,312
552,418
518,56
1223,351
273,532
1302,400
858,865
1001,854
483,826
82,717
1181,221
792,35
592,446
1130,469
741,308
649,260
552,308
100,100
706,838
211,464
1051,816
60,58
234,198
880,756
927,325
115,434
487,791
971,507
1136,584
736,746
341,109
845,424
1069,519
985,416
599,394
845,173
708,406
915,330
1151,159
393,382
1052,826
279,308
633,348
1042,28
952,619
36,882
306,218
565,815
308,179
1235,703
241,519
624,887
306,161
1248,46
907,220
47,810
917,64
895,556
489,254
132,275
530,641
887,340
1149,192
100,222
656,635
515,443
47,177
216,831
910,597
594,28
1004,161
691,128
555,567
815,667
947,570
50,166
703,625
882,100
1066,406
629,196
1307,222
542,23
1284,614
681,532
744,516
1178,582
729,721
979,891
987,168
1123,3
438,35
1143,562
1136,620
518,635
867,878
1056,326
1222,42
1223,543
684,719
301,644
1141,522
975,87
288,564
231,66
517,43
1146,152
1034,393
117,37
585,250
218,840
751,609
706,473
686,327
99,637
1310,738
361,660
853,834
1146,287
1031,756
189,173
760,259
470,836
495,735
995,387
53,7
897,644
1169,565
915,609
241,71
922,243
3,56
644,115
756,212
841,117
1071,108
927,121
758,418
1061,119
577,256
607,756
913,877
413,263
1284,240
440,507
378,647
331,215
743,338
132,619
887,514
18,148
353,810
805,246
1077,619
1284,707
773,81
279,756
1051,778
1086,620
36,12
452,865
512,612
87,351
865,633
761,568
600,553
1161,694
433,886
810,693
1072,516
971,731
915,816
82,635
1158,143
438,63
1235,655
1300,470
1228,574
390,894
331,891
1051,557
1126,465
136,56
677,5
62,46
189,823
189,424
502,303
216,859
1014,134
607,737
69,17
915,557
107,719
1171,675
569,308
304,707
539,834
510,298
405,352
1125,826
544,231
1019,772
574,381
88,714
1169,548
830,283
231,609
249,750
874,397
601,362
599,500
154,245
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

View file

@ -0,0 +1,9 @@
[package]
name = "day13_rs"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom = "7.1.0"

View file

@ -0,0 +1,83 @@
#![warn(clippy::pedantic)]
use std::collections::HashSet;
use std::io::{stdin, Read};
use nom::bytes::complete::tag;
use nom::character::complete::{anychar, newline, u32};
use nom::combinator::{map, map_opt};
use nom::multi::many1;
use nom::sequence::{preceded, separated_pair, terminated};
type Input<'a> = &'a str;
type IResult<'a, T> = nom::IResult<Input<'a>, T>;
type Point = (usize, usize);
fn point(i: Input) -> IResult<Point> {
separated_pair(map(u32, |i| i as usize), tag(","), map(u32, |i| i as usize))(i)
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Fold {
X(usize),
Y(usize),
}
fn fold(i: Input) -> IResult<Fold> {
map_opt(
preceded(
tag("fold along "),
separated_pair(anychar, tag("="), map(u32, |i| i as usize)),
),
|(dimension, position)| match dimension {
'x' => Some(Fold::X(position)),
'y' => Some(Fold::Y(position)),
_ => None,
},
)(i)
}
fn input(i: Input) -> IResult<(Vec<Point>, Vec<Fold>)> {
separated_pair(
many1(terminated(point, newline)),
newline,
many1(terminated(fold, newline)),
)(i)
}
fn do_fold(points: impl Iterator<Item = Point>, fold: Fold) -> HashSet<Point> {
let transform = |x, n| if x < n { x } else { n - (x - n) };
points
.map(|(x, y)| match fold {
Fold::X(n) => (transform(x, n), y),
Fold::Y(n) => (x, transform(y, n)),
})
.collect()
}
fn main() {
let mut data = String::new();
stdin().lock().read_to_string(&mut data).unwrap();
let (points, folds) = input(&data).unwrap().1;
let mut points: HashSet<_> = points.into_iter().collect();
let mut folds = folds.into_iter();
points = do_fold(points.into_iter(), folds.next().unwrap());
println!("{}", points.len());
for fold in folds {
points = do_fold(points.into_iter(), fold);
}
for row in 0..=points.iter().map(|&(_, y)| y).max().unwrap() {
for col in 0..=points.iter().map(|&(x, _)| x).max().unwrap() {
if points.contains(&(col, row)) {
print!("#");
} else {
print!(".");
}
}
println!();
}
}