Day 13
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julius 2021-12-13 10:54:03 +01:00
parent b3434735e6
commit ce13c17b1e
Signed by: j00lz
GPG key ID: AF241B0AA237BBA2
4 changed files with 1105 additions and 14 deletions

936
input/day13 Normal file
View file

@ -0,0 +1,936 @@
1148,688
1020,159
857,707
1176,415
388,275
50,849
544,520
1,418
1119,280
217,26
358,110
1302,684
910,791
45,287
544,806
423,859
529,866
1150,686
977,668
666,245
380,603
892,343
437,483
1109,411
296,534
763,676
711,218
139,774
923,397
1202,341
664,12
416,89
522,54
524,401
541,796
1153,523
1101,427
977,308
102,856
769,546
966,523
1218,820
833,682
872,806
457,542
333,586
238,537
528,103
169,840
321,44
102,408
825,346
1220,791
1223,584
907,597
785,766
147,654
201,707
775,674
746,651
221,354
213,460
843,105
59,110
224,773
654,368
666,21
1134,736
475,121
445,799
1141,411
835,688
810,12
810,882
1064,51
1240,805
266,723
125,812
1048,299
213,68
129,738
100,256
666,551
1004,632
1250,505
119,674
565,264
937,220
387,497
1235,259
8,658
902,294
159,884
222,159
184,645
700,646
1251,110
53,469
1228,282
157,779
862,759
694,31
10,876
652,110
1104,505
554,582
574,742
985,642
410,830
873,420
623,42
70,89
922,395
627,446
724,848
610,534
1042,283
852,731
370,193
109,420
1275,239
582,546
88,537
1133,682
584,847
835,654
299,649
735,82
467,547
408,51
1278,233
758,325
1044,171
233,364
490,16
326,187
455,114
1231,504
1300,876
403,830
1185,418
731,187
256,354
1200,725
1101,338
781,399
1207,147
1153,502
335,752
716,389
811,747
619,311
865,95
221,443
567,310
503,112
646,658
296,617
314,40
813,483
1223,252
813,502
566,724
445,95
832,437
191,614
268,611
162,78
303,5
865,114
373,108
688,9
692,511
403,696
1071,724
786,401
544,164
150,457
75,198
1006,607
441,68
300,873
160,686
1059,646
53,47
850,847
458,182
1265,259
869,826
571,241
408,395
1007,889
515,95
458,163
154,761
642,774
571,653
199,266
364,719
109,474
700,360
160,14
671,345
448,583
711,666
296,277
984,187
1096,824
381,469
649,624
1310,635
1206,607
392,35
381,698
520,479
482,145
498,444
239,170
601,21
522,840
1067,770
415,259
827,595
52,725
251,597
0,10
150,736
194,840
504,630
586,275
140,387
716,284
619,127
1066,178
827,299
858,634
652,336
738,612
1016,233
431,595
760,656
909,803
960,679
171,376
45,194
687,852
923,385
1116,840
112,696
1193,575
119,786
290,159
401,803
515,662
708,110
1054,354
1019,351
1103,408
1250,610
979,430
213,434
574,395
1153,859
654,635
671,526
912,873
664,236
1126,697
157,819
1129,271
460,46
494,196
604,849
1131,206
975,80
1111,655
798,473
716,262
493,644
378,607
1087,285
490,526
914,400
485,444
938,584
735,812
639,549
408,724
244,178
934,166
739,241
325,700
139,120
1067,124
950,681
356,863
639,345
618,735
1242,270
848,302
222,107
663,142
1302,658
441,98
850,495
759,733
386,516
488,250
892,691
869,684
1136,886
1067,572
671,318
890,560
82,53
692,735
1061,234
1097,460
922,275
1006,679
631,341
100,424
1032,166
959,553
1046,22
422,64
537,266
340,830
945,562
190,357
1206,516
541,546
656,78
756,190
146,411
723,15
1148,816
602,744
602,560
1171,351
262,859
761,578
410,206
923,110
223,285
554,246
420,425
251,248
1287,229
90,651
691,311
879,161
445,418
32,9
97,681
1032,280
782,551
1228,53
479,334
869,98
1221,264
959,889
57,120
795,120
435,452
750,830
529,674
666,721
996,152
596,740
1072,203
1141,252
937,200
825,444
192,262
117,266
969,10
45,700
202,634
909,539
930,291
852,289
1113,459
447,668
1174,145
766,806
234,493
497,168
725,479
243,572
36,308
231,35
653,700
1155,742
967,352
90,567
1210,256
26,133
164,430
852,182
550,835
575,278
1096,518
408,618
1126,645
291,203
843,547
393,589
251,646
403,597
956,873
504,158
438,730
1042,611
36,383
622,233
954,255
686,336
1072,89
882,312
1012,184
1056,231
1240,89
639,368
1185,226
37,530
1240,133
1033,619
1116,847
624,336
1227,770
298,38
418,243
60,877
817,112
554,638
1148,766
495,94
363,239
171,518
167,884
192,632
1227,124
119,332
1279,42
1275,655
387,385
843,211
145,271
416,203
1042,560
560,275
378,597
199,239
1242,723
1200,54
684,255
649,270
709,425
234,137
52,560
36,159
1298,264
1201,420
1143,570
769,796
773,19
1181,128
474,712
535,184
713,120
1273,635
267,385
768,849
848,592
94,718
1101,767
1054,21
156,819
827,518
1258,560
243,770
1043,497
1278,9
234,849
962,262
602,150
720,35
1054,522
758,38
1287,341
1207,733
1114,849
254,572
1114,493
836,712
951,751
410,64
1282,255
818,414
776,759
813,392
1198,824
150,158
850,757
489,334
1272,415
23,344
1043,833
110,840
104,656
750,512
766,730
1116,137
594,610
358,534
474,182
408,276
282,870
172,749
619,799
418,19
653,418
795,792
1034,119
1191,786
1255,228
562,220
410,688
139,214
806,630
683,446
57,827
508,147
1193,266
766,520
147,78
186,835
441,826
756,638
80,499
1141,483
1089,443
932,597
1044,569
1185,28
937,108
915,687
165,334
954,863
468,835
246,499
671,121
89,264
745,30
542,824
311,352
395,207
878,159
981,252
104,387
623,303
333,859
972,469
1225,155
364,287
1216,830
668,264
560,718
902,276
766,390
528,551
445,875
606,54
1242,171
1305,668
268,507
1118,144
1076,493
112,198
773,394
403,259
1203,228
1148,78
276,735
22,378
1029,694
1124,835
162,206
169,483
467,120
900,830
668,64
49,367
1037,308
760,59
736,742
820,480
411,770
663,752
500,425
500,469
104,159
756,582
103,147
68,270
873,698
1146,318
1310,240
105,764
642,64
102,38
1148,592
890,21
497,194
145,308
266,171
873,411
258,714
870,719
500,882
1253,120
261,81
744,651
1258,520
542,70
1298,885
1115,518
813,75
852,163
587,15
460,399
552,653
89,712
806,158
88,651
381,420
726,47
547,676
925,67
1205,92
716,610
281,786
537,319
1268,247
1208,856
497,726
460,847
181,271
981,194
1171,571
592,200
154,133
594,505
687,303
1200,358
1255,666
159,772
535,108
453,476
1170,112
542,268
281,694
705,815
117,866
746,203
1253,67
1191,75
119,75
328,411
850,46
1088,607
44,646
682,761
1231,726
1200,838
445,114
176,158
335,682
129,290
1265,194
639,318
1059,597
1116,54
45,259
701,252
644,873
1037,831
832,736
738,393
131,70
1205,316
239,259
828,761
529,259
1086,241
1179,824
1011,245
1295,325
791,425
282,24
792,313
879,285
441,684
1150,219
140,283
49,115
256,873
504,264
668,824
552,508
858,260
1042,287
1146,32
594,465
431,285
1266,646
278,166
1280,336
447,226
704,840
438,806
1298,233
687,591
1114,737
294,885
1179,70
776,135
459,518
1144,219
666,873
1014,617
60,389
602,179
1160,437
1153,115
915,207
100,190
1148,480
920,263
199,655
10,245
485,848
1071,635
290,271
1054,746
387,397
177,682
586,270
658,336
1163,654
282,758
1094,515
94,830
623,852
38,522
549,92
564,327
744,243
1200,851
495,800
1240,668
775,786
1298,70
1200,374
835,121
186,465
415,892
851,518
564,203
1056,663
709,873
837,623
246,395
756,424
398,204
587,187
1266,310
628,133
924,334
455,655
398,873
320,222
1170,334
83,124
739,124
959,540
1220,679
390,631
544,88
290,607
102,632
820,576
278,280
1310,187
80,395
105,443
70,668
1044,395
251,382
68,848
267,61
1111,332
127,556
1258,179
1044,505
738,282
82,612
934,614
1054,204
72,348
254,663
1043,397
85,291
877,155
417,91
460,137
565,579
564,567
1041,550
565,30
1000,859
1056,791
688,233
595,336
1012,479
140,334
256,148
1220,651
542,178
209,767
534,759
1287,547
1213,681
1076,849
508,747
273,831
276,149
256,298
194,54
872,88
441,656
214,175
266,569
865,875
12,630
1000,523
1242,494
873,196
157,35
420,469
445,780
551,733
760,835
1118,750
551,161
709,831
448,311
1275,319
1057,192
136,749
628,761
365,332
1153,75
846,117
546,133
1288,378
900,512
223,609
350,159
1287,344
1103,668
125,418
440,719
832,826
36,586
776,57
1171,214
480,456
668,120
646,12
947,95
378,159
31,852
1170,387
535,786
572,612
619,583
1086,101
432,159
560,619
2,320
1143,10
53,847
256,220
894,805
152,186
136,817
32,233
1101,786
579,288
117,351
1043,845
731,707
666,18
1208,408
32,885
268,283
929,698
326,570
590,35
104,607
654,436
139,571
619,655
403,417
1076,401
1220,243
990,672
1308,574
1109,707
1032,614
499,152
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

@ -175,17 +175,17 @@ start-RW";
}
// These are very slow, so they are commented out.
#[test]
fn part2_test_3() -> anyhow::Result<()> {
let d = Day12::init(INPUT_3.to_string())?;
assert_eq!("3509", d.part2()?);
Ok(())
}
#[test]
fn part2_real() -> anyhow::Result<()> {
let d = Day12::init(crate::load_input("12")?)?;
assert_eq!("117095", d.part2()?);
Ok(())
}
// #[test]
// fn part2_test_3() -> anyhow::Result<()> {
// let d = Day12::init(INPUT_3.to_string())?;
// assert_eq!("3509", d.part2()?);
// Ok(())
// }
//
// #[test]
// fn part2_real() -> anyhow::Result<()> {
// let d = Day12::init(crate::load_input("12")?)?;
// assert_eq!("117095", d.part2()?);
// Ok(())
// }
}

152
src/day13.rs Normal file
View file

@ -0,0 +1,152 @@
use std::collections::HashSet;
use std::str::FromStr;
use crate::Day;
pub struct Day13 {
points: HashSet<(i32, i32)>,
folds: Vec<Fold>,
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug)]
enum Fold {
X(i32),
Y(i32),
}
impl FromStr for Fold {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let r = s.rsplit(' ').nth(0).ok_or_else(|| ())?;
let z = r.split('=').collect::<Vec<_>>();
match z[0] {
"x" => Ok(Fold::X(z[1].parse().map_err(|_| ())?)),
"y" => Ok(Fold::Y(z[1].parse().map_err(|_| ())?)),
_ => Err(()),
}
}
}
impl Day for Day13 {
fn init(content: String) -> anyhow::Result<Self> {
let l = content.lines();
let points = l.clone().take_while(|&x| x != "").map(|st| st.split(',')
.map(str::parse).flatten().collect::<Vec<i32>>()).map(|v| (v[0], v[1])).collect::<HashSet<_>>();
Ok(Self {
points,
folds: l.filter(|st| st.starts_with("fold along")).map(str::parse).flatten().collect::<Vec<Fold>>(),
})
}
fn part1(&self) -> anyhow::Result<String> {
let fold = self.folds[0];
let map = self.points.clone();
Ok(format!("{}", flip(map, fold).len()))
}
fn part2(&self) -> anyhow::Result<String> {
let r = self.folds.iter().fold(self.points.clone(), |a, &b| flip(a, b));
let max_x = r.iter().max_by(|&(xm, _), &(xn, _)| xm.cmp(xn)).ok_or_else(|| anyhow::Error::msg("X is empty!"))?;
let max_y = r.iter().max_by(|&(_, xm), &(_, xn)| xm.cmp(xn)).ok_or_else(|| anyhow::Error::msg("Y is empty!"))?;
let mut z = String::new();
for y in 0..=max_y.1 {
for x in 0..=max_x.0 {
if r.contains(&(x, y)) {
z += "";
} else {
z += " ";
}
}
z += "\n";
}
Ok(format!("\n{}", z))
}
}
fn flip(set: HashSet<(i32, i32)>, f: Fold) -> HashSet<(i32, i32)> {
set.iter().map(|&e| flip_one(e, f)).collect()
}
fn flip_one((x, y): (i32, i32), f: Fold) -> (i32, i32) {
match f {
Fold::X(pos) => {
let new_x = if x > pos {
pos - x + pos
} else {
x
};
(new_x, y)
}
Fold::Y(pos) => {
let new_y = if y > pos {
pos - y + pos
} else {
y
};
(x, new_y)
}
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use crate::day13::{Day13, flip, flip_one, Fold};
use crate::day::Day;
const INPUT: &str = 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";
#[test]
fn flip_test() {
assert_eq!(flip_one((0, 0), Fold::X(3)), (0, 0));
assert_eq!(flip_one((3, 0), Fold::X(3)), (3, 0));
assert_eq!(flip_one((6, 0), Fold::X(3)), (0, 0));
}
#[test]
fn flip_set_test() {
let set = HashSet::from([(0, 3), (3, 2), (6, 3)]);
let r = flip(set, Fold::X(3));
assert!(r.contains(&(0, 3)));
assert!(r.contains(&(3, 2)));
assert!(!r.contains(&(6, 3)));
assert_eq!(r.len(), 2);
}
#[test]
fn part1_test() -> anyhow::Result<()> {
let d = Day13::init(INPUT.to_string())?;
assert_eq!("17", d.part1()?);
Ok(())
}
#[test]
fn part1_real() -> anyhow::Result<()> {
let d = Day13::init(crate::load_input("13")?)?;
assert_eq!("781", d.part1()?);
Ok(())
}
}

View file

@ -15,6 +15,7 @@ use crate::day09::Day09;
use crate::day10::Day10;
use crate::day11::Day11;
use crate::day12::Day12;
use crate::day13::Day13;
mod day;
mod day01;
@ -29,6 +30,7 @@ mod day09;
mod day10;
mod day11;
mod day12;
mod day13;
fn load_input(day: &str) -> Result<String> {
read_to_string(format!("./input/day{}", day)).map_err(|x| x.into())
@ -75,7 +77,8 @@ fn main() -> anyhow::Result<()> {
Box::new(Day09::init(load_input("09")?)?),
Box::new(Day10::init(load_input("10")?)?),
Box::new(Day11::init(load_input("11")?)?),
Box::new(Day12::init(load_input("12")?)?),];
Box::new(Day12::init(load_input("12")?)?),
Box::new(Day13::init(load_input("13")?)?),];
let _verbosity = matches.occurrences_of("v");
if matches.is_present("all") {