summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2021-12-09 12:14:19 +0000
committerFederico Igne <git@federicoigne.com>2021-12-09 12:14:19 +0000
commite8ad18ed7b9de796208a2a55133c80430add2fb9 (patch)
tree9dd5074266e2c38f6bdd729d33a8426e2dae18d9
parent5100744c95260c026468d486b2625fedc90d0426 (diff)
downloadaoc-e8ad18ed7b9de796208a2a55133c80430add2fb9.tar.gz
aoc-e8ad18ed7b9de796208a2a55133c80430add2fb9.zip
Day 5
-rw-r--r--day5/Cargo.toml11
-rw-r--r--day5/resources/input.txt500
-rw-r--r--day5/src/main.rs136
3 files changed, 647 insertions, 0 deletions
diff --git a/day5/Cargo.toml b/day5/Cargo.toml
new file mode 100644
index 0000000..f52d4d4
--- /dev/null
+++ b/day5/Cargo.toml
@@ -0,0 +1,11 @@
1[package]
2name = "day5"
3version = "0.1.0"
4authors = ["Federico Igne <git@federicoigne.com>"]
5edition = "2018"
6
7# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
9[dependencies]
10lazy_static = "1.4"
11regex = "1.5"
diff --git a/day5/resources/input.txt b/day5/resources/input.txt
new file mode 100644
index 0000000..a97a0f8
--- /dev/null
+++ b/day5/resources/input.txt
@@ -0,0 +1,500 @@
1445,187 -> 912,654
2820,46 -> 25,841
3216,621 -> 458,379
4955,898 -> 67,10
5549,572 -> 549,520
6796,107 -> 109,794
7729,698 -> 338,698
811,987 -> 968,30
9381,840 -> 381,409
1080,467 -> 80,48
11132,197 -> 132,92
12343,96 -> 343,710
1342,854 -> 346,550
14503,56 -> 804,56
15599,206 -> 60,206
16702,920 -> 474,920
17496,790 -> 223,517
18969,579 -> 583,579
19897,66 -> 604,66
20484,754 -> 640,910
21330,49 -> 949,49
22908,132 -> 714,132
23517,153 -> 97,573
24317,865 -> 678,504
25800,61 -> 800,51
26179,242 -> 179,202
27529,757 -> 529,838
28288,953 -> 393,953
29372,15 -> 925,568
30100,85 -> 654,639
31663,562 -> 737,636
32576,981 -> 245,981
33347,240 -> 347,928
3491,119 -> 413,441
35637,397 -> 330,90
36534,257 -> 950,257
37155,636 -> 694,97
38539,274 -> 539,327
39329,795 -> 824,300
40103,51 -> 961,909
4187,868 -> 168,787
4221,250 -> 157,386
43591,316 -> 670,395
44722,670 -> 630,670
4528,167 -> 28,489
46480,210 -> 68,622
47573,700 -> 354,919
48186,59 -> 700,59
49121,186 -> 670,186
50797,985 -> 671,985
51836,804 -> 335,804
52701,835 -> 104,238
53456,718 -> 456,964
54694,891 -> 694,839
55205,637 -> 205,318
5640,958 -> 773,225
57151,391 -> 151,779
58693,894 -> 417,894
59418,700 -> 656,700
60196,985 -> 896,985
61357,509 -> 608,258
62552,558 -> 552,482
63184,412 -> 170,412
64122,746 -> 643,225
65268,930 -> 247,930
66979,820 -> 407,248
67755,893 -> 845,893
68706,843 -> 706,225
69162,726 -> 895,726
70140,888 -> 289,888
71614,432 -> 903,721
72272,725 -> 272,598
73529,672 -> 967,234
74903,989 -> 785,871
75422,355 -> 422,360
76313,722 -> 713,322
77460,121 -> 460,151
7855,944 -> 946,944
79795,744 -> 221,744
80816,953 -> 471,953
81865,186 -> 557,186
8294,976 -> 747,323
83302,961 -> 811,452
84361,966 -> 921,406
85197,988 -> 571,988
86310,905 -> 722,493
87699,91 -> 272,518
88295,306 -> 84,95
89220,116 -> 395,291
90183,364 -> 523,364
9116,986 -> 16,319
9254,980 -> 635,399
93340,110 -> 651,421
94788,76 -> 788,635
95933,375 -> 458,375
9612,434 -> 494,916
97253,892 -> 962,183
98240,508 -> 240,234
99763,934 -> 506,677
100308,135 -> 239,66
101117,649 -> 751,15
10295,535 -> 428,868
10316,937 -> 902,51
104547,404 -> 547,830
105128,581 -> 970,581
106959,810 -> 564,415
107971,738 -> 378,145
108919,210 -> 295,210
109737,43 -> 231,43
11082,577 -> 455,204
111821,337 -> 570,337
112688,753 -> 538,753
113891,844 -> 124,844
11474,957 -> 946,85
11543,942 -> 43,210
116100,391 -> 100,142
117975,527 -> 175,527
118510,844 -> 395,959
119558,231 -> 558,858
120839,915 -> 262,338
121784,290 -> 875,199
122644,824 -> 812,824
123899,657 -> 500,657
124263,668 -> 263,964
125157,374 -> 820,374
126530,301 -> 530,67
12715,688 -> 15,572
128216,844 -> 479,581
129973,59 -> 68,964
130104,92 -> 104,547
131421,472 -> 421,176
132887,805 -> 231,149
133140,980 -> 852,980
134248,602 -> 346,602
135334,961 -> 334,471
136892,892 -> 958,958
137270,83 -> 270,135
138950,105 -> 404,651
139979,476 -> 930,427
140416,430 -> 879,430
141796,937 -> 796,415
142670,679 -> 72,679
143733,884 -> 733,302
144745,196 -> 306,196
145174,353 -> 667,846
146285,978 -> 254,978
14710,63 -> 936,989
148242,107 -> 242,725
149238,341 -> 238,800
150975,102 -> 174,903
151530,474 -> 530,853
152931,47 -> 467,47
15386,141 -> 821,141
154263,15 -> 654,15
155688,542 -> 378,232
156826,793 -> 989,793
157729,285 -> 729,192
158587,915 -> 587,79
159548,667 -> 877,667
16015,836 -> 783,68
161662,673 -> 71,82
162312,681 -> 910,83
163760,418 -> 491,418
164175,502 -> 443,502
165817,878 -> 29,90
166798,569 -> 811,582
167703,141 -> 743,181
168941,849 -> 941,778
16963,24 -> 500,461
170697,183 -> 119,761
171705,672 -> 152,672
172150,567 -> 656,567
173158,411 -> 965,411
174702,872 -> 276,446
175141,179 -> 741,779
176533,886 -> 817,886
177569,949 -> 285,949
178699,764 -> 699,780
179333,863 -> 805,391
180861,804 -> 524,467
181791,501 -> 718,501
182976,265 -> 976,713
183129,342 -> 339,132
184322,738 -> 212,738
185700,534 -> 622,456
18668,314 -> 14,314
187146,112 -> 215,181
188170,211 -> 482,211
189159,412 -> 159,32
190312,939 -> 312,95
191232,18 -> 912,698
192950,114 -> 950,826
193620,848 -> 620,11
194624,288 -> 544,208
195752,479 -> 752,577
196784,796 -> 784,872
197130,55 -> 974,899
198434,82 -> 434,481
199988,230 -> 892,134
200159,252 -> 159,291
201462,14 -> 462,977
202553,981 -> 553,390
203231,936 -> 51,936
20458,759 -> 60,759
205572,891 -> 584,891
206705,303 -> 124,303
207144,894 -> 970,68
208865,275 -> 865,956
209492,491 -> 470,491
210971,15 -> 977,15
211750,521 -> 33,521
212913,947 -> 387,421
213368,677 -> 570,677
214795,186 -> 882,186
215404,840 -> 678,840
216187,488 -> 403,488
217824,706 -> 642,706
218330,541 -> 330,195
219564,531 -> 774,531
220271,857 -> 20,606
221976,975 -> 976,843
222323,341 -> 21,39
223575,643 -> 267,643
224827,295 -> 827,854
225749,486 -> 749,780
226656,716 -> 656,470
227635,187 -> 417,187
228503,488 -> 503,393
229592,688 -> 592,567
230515,408 -> 128,795
231608,158 -> 780,158
232677,96 -> 11,762
233127,452 -> 339,452
234117,985 -> 291,811
235157,371 -> 157,916
236640,758 -> 983,758
237906,413 -> 906,776
238224,842 -> 627,439
239903,728 -> 903,459
240358,138 -> 822,602
24130,16 -> 929,915
242440,900 -> 294,900
243809,73 -> 987,73
24455,410 -> 304,161
245441,672 -> 315,672
246939,40 -> 234,40
247334,698 -> 309,698
248572,738 -> 572,226
249445,71 -> 445,468
250225,660 -> 427,458
251390,320 -> 449,320
252507,635 -> 507,169
25347,116 -> 738,807
254127,14 -> 689,14
255316,760 -> 316,432
256831,101 -> 250,682
257370,807 -> 370,898
258678,186 -> 491,186
259866,83 -> 539,83
260518,848 -> 518,962
261188,135 -> 81,28
262378,226 -> 597,226
263646,534 -> 141,534
264275,672 -> 275,854
26567,421 -> 676,421
266386,323 -> 988,323
267903,984 -> 10,91
26837,348 -> 529,840
269872,134 -> 358,648
27042,826 -> 42,822
271688,922 -> 21,922
27247,539 -> 942,539
273739,483 -> 375,847
27423,217 -> 800,217
275589,512 -> 589,953
276292,229 -> 107,229
277873,678 -> 873,770
278794,295 -> 739,240
279464,559 -> 936,559
280685,736 -> 368,736
281114,941 -> 114,307
282571,643 -> 74,643
283281,185 -> 273,177
284497,937 -> 497,469
285152,815 -> 702,815
28676,43 -> 980,947
287272,149 -> 101,149
288934,945 -> 107,118
289532,476 -> 759,476
290955,942 -> 397,942
29131,918 -> 931,18
292790,420 -> 389,420
29336,496 -> 215,317
294252,209 -> 139,209
295704,148 -> 719,133
296413,571 -> 165,571
297690,433 -> 864,607
298976,417 -> 517,876
299803,568 -> 443,568
300335,558 -> 335,334
301405,807 -> 691,521
302194,482 -> 486,190
303377,856 -> 377,802
304313,842 -> 313,254
305449,961 -> 198,710
306197,916 -> 197,797
30782,965 -> 959,88
308371,239 -> 829,697
309471,70 -> 596,70
310835,144 -> 835,950
311283,486 -> 506,486
312147,29 -> 147,747
313187,485 -> 187,195
314781,144 -> 480,144
315801,839 -> 925,715
316415,960 -> 415,442
317877,939 -> 29,91
31822,118 -> 22,439
319460,315 -> 450,315
320982,960 -> 71,49
321105,231 -> 105,331
32298,174 -> 551,174
323721,978 -> 38,295
324167,290 -> 167,133
325218,158 -> 218,908
326819,812 -> 758,812
327123,92 -> 123,132
32866,721 -> 66,906
329478,441 -> 967,930
330284,58 -> 464,58
331958,15 -> 37,936
332310,337 -> 359,288
333212,763 -> 212,373
334101,279 -> 101,267
335622,409 -> 106,925
336318,657 -> 318,432
337938,631 -> 938,650
338142,881 -> 254,881
339848,987 -> 848,451
340686,223 -> 481,223
341124,248 -> 812,248
342246,267 -> 246,148
34396,670 -> 324,442
344645,888 -> 385,628
345417,555 -> 417,858
346543,495 -> 543,150
34773,350 -> 440,717
348459,704 -> 459,179
349871,493 -> 871,764
350911,34 -> 64,881
351544,791 -> 703,791
352447,218 -> 62,218
353202,649 -> 396,649
354935,916 -> 55,36
355124,408 -> 477,761
356608,850 -> 484,850
357935,876 -> 582,876
358377,612 -> 269,612
359413,727 -> 365,679
36064,451 -> 850,451
361684,807 -> 357,807
362323,364 -> 372,364
363887,300 -> 419,300
364837,831 -> 837,927
365294,255 -> 768,729
366878,23 -> 141,760
36736,627 -> 157,627
368824,703 -> 824,968
369356,109 -> 657,109
370799,266 -> 313,752
37171,600 -> 650,21
372564,863 -> 564,54
37336,720 -> 109,720
374318,488 -> 682,488
375249,350 -> 979,350
376560,502 -> 255,502
377132,327 -> 132,246
378287,906 -> 791,906
379818,110 -> 818,882
380937,17 -> 113,841
38150,710 -> 673,87
382702,952 -> 702,533
383666,552 -> 611,552
384612,962 -> 112,462
385260,529 -> 351,529
386440,313 -> 440,663
387605,341 -> 405,141
388277,287 -> 461,287
389268,890 -> 268,92
390764,526 -> 877,639
391165,697 -> 832,697
392240,716 -> 801,155
393872,429 -> 578,429
39488,816 -> 338,816
395981,881 -> 981,138
396457,351 -> 457,679
397850,526 -> 850,447
398139,449 -> 165,449
399127,544 -> 127,934
400160,890 -> 745,305
401526,113 -> 303,336
40217,500 -> 17,621
403796,311 -> 181,926
404260,218 -> 787,218
405536,989 -> 536,261
406257,826 -> 257,180
407531,37 -> 531,493
408961,942 -> 206,187
409536,668 -> 536,868
410154,967 -> 154,931
411808,317 -> 808,873
412487,258 -> 599,258
41359,962 -> 802,219
414322,945 -> 322,837
415378,973 -> 33,628
416668,556 -> 691,556
417819,728 -> 787,728
418484,261 -> 484,874
419333,271 -> 278,271
420733,515 -> 741,523
421775,854 -> 523,602
42267,215 -> 616,215
423951,685 -> 951,433
424372,105 -> 372,494
425917,788 -> 917,23
426890,584 -> 245,584
427748,276 -> 893,276
428733,721 -> 733,747
429225,908 -> 897,908
430437,140 -> 423,140
431456,513 -> 136,833
432413,135 -> 413,596
433143,245 -> 879,981
434870,639 -> 942,639
43528,175 -> 696,843
436393,303 -> 393,197
437169,986 -> 458,986
43843,44 -> 952,953
439236,405 -> 60,229
440266,845 -> 292,845
441529,98 -> 95,532
44295,658 -> 695,658
443368,454 -> 112,710
444506,776 -> 662,776
445928,494 -> 604,170
446179,138 -> 900,859
44745,560 -> 408,197
448655,654 -> 37,36
44956,432 -> 56,456
450844,614 -> 844,898
451240,191 -> 240,112
452639,911 -> 213,911
45347,887 -> 830,104
45457,50 -> 977,970
455899,928 -> 111,928
456962,676 -> 962,518
457129,585 -> 469,245
458988,775 -> 988,553
459417,344 -> 842,769
460468,110 -> 506,72
461687,204 -> 687,345
462828,553 -> 765,490
46375,894 -> 75,93
46426,798 -> 11,783
465967,44 -> 967,478
466240,481 -> 947,481
467794,254 -> 162,254
468502,944 -> 812,944
469331,417 -> 410,417
470850,275 -> 850,980
471671,130 -> 671,941
472240,99 -> 240,381
473771,399 -> 318,399
474946,11 -> 28,929
475731,939 -> 824,846
476268,71 -> 832,635
477968,37 -> 968,642
478935,365 -> 515,365
479199,792 -> 932,792
48032,116 -> 371,116
481324,67 -> 941,67
482453,181 -> 453,128
483958,982 -> 115,139
484962,168 -> 154,976
485474,215 -> 333,215
486458,675 -> 458,315
487577,302 -> 300,302
488704,493 -> 704,876
489887,549 -> 887,439
49081,328 -> 724,328
491575,490 -> 670,490
492576,17 -> 576,218
49321,46 -> 963,988
494532,235 -> 532,615
495796,213 -> 796,407
49655,948 -> 980,23
497775,471 -> 272,471
49826,138 -> 344,138
499635,518 -> 915,518
500727,365 -> 727,216
diff --git a/day5/src/main.rs b/day5/src/main.rs
new file mode 100644
index 0000000..e55b244
--- /dev/null
+++ b/day5/src/main.rs
@@ -0,0 +1,136 @@
1use std::fs;
2use std::path::Path;
3use regex::Regex;
4use lazy_static::lazy_static;
5
6#[derive(Debug,PartialEq)]
7struct Segment(u32,u32,u32,u32);
8
9impl Segment {
10 fn is_hor(&self) -> bool {
11 self.1 == self.3
12 }
13 fn is_ver(&self) -> bool {
14 self.0 == self.2
15 }
16}
17
18/* AOC21 Day 7: https://adventofcode.com/2021/day/7 */
19fn main() {
20 let input = Path::new("resources").join("input.txt");
21 let content = fs::read_to_string(input).expect("Unable to read input file");
22 let (size, segments) = parse_input(&content);
23 let area = radar(&segments, size, false);
24 println!("Ex1: The result is: {}", area.iter().filter(|&&c| c > 1).count());
25 let area = radar(&segments, size, true);
26 println!("Ex2: The result is: {}", area.iter().filter(|&&c| c > 1).count());
27}
28
29fn radar(segments: &[Segment], size: u32, diagonals: bool) -> Vec<u32> {
30 let mut area = vec![0;(size+1).pow(2) as usize];
31 segments.iter().for_each(
32 |s| match s {
33 s if s.is_hor() => {
34 range(s.0, s.2).iter().for_each(|x| area[((size+1)*s.1 + x) as usize] += 1)
35 },
36 s if s.is_ver() => {
37 range(s.1, s.3).iter().for_each(|y| area[((size+1)*y + s.0) as usize] += 1)
38 },
39 s => {
40 if diagonals {
41 range(s.0,s.2).iter().zip(range(s.1,s.3)).for_each(|(x,y)| area[((size+1)*y + x) as usize] += 1)
42 }
43 }
44 }
45 );
46 area
47}
48
49fn parse_input(s: &str) -> (u32, Vec<Segment>) {
50 lazy_static! {
51 static ref RE: Regex = Regex::new(r"^(\d+),(\d+) -> (\d+),(\d+)$").unwrap();
52 }
53 let mut max = 0;
54 let segs = s.lines().map(|l| {
55 let caps = RE.captures(l).expect("Malformed input");
56 let x1 = caps.get(1).unwrap().as_str().parse().expect("Malformed input");
57 let y1 = caps.get(2).unwrap().as_str().parse().expect("Malformed input");
58 let x2 = caps.get(3).unwrap().as_str().parse().expect("Malformed input");
59 let y2 = caps.get(4).unwrap().as_str().parse().expect("Malformed input");
60 max = *[x1, x2, y1, y2, max].iter().max().unwrap();
61 Segment(x1, y1, x2, y2)
62 }).collect();
63 (max, segs)
64}
65
66fn range(from: u32, to: u32) -> Vec<u32> {
67 if from < to {
68 (from..=to).collect()
69 } else {
70 (to..=from).rev().collect()
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 const INPUT: &str = "0,9 -> 5,9
798,0 -> 0,8
809,4 -> 3,4
812,2 -> 2,1
827,0 -> 7,4
836,4 -> 2,0
840,9 -> 2,9
853,4 -> 1,4
860,0 -> 8,8
875,5 -> 8,2";
88
89 #[test]
90 fn input_parsing() {
91 let (size, segments) = parse_input(INPUT);
92 assert_eq!(9,size);
93 assert_eq!(vec![
94 Segment(0,9,5,9),
95 Segment(8,0,0,8),
96 Segment(9,4,3,4),
97 Segment(2,2,2,1),
98 Segment(7,0,7,4),
99 Segment(6,4,2,0),
100 Segment(0,9,2,9),
101 Segment(3,4,1,4),
102 Segment(0,0,8,8),
103 Segment(5,5,8,2),
104 ],segments);
105 }
106
107 #[test]
108 fn examples() {
109 let (size, segments) = parse_input(INPUT);
110 assert_eq!(vec![
111 0,0,0,0,0,0,0,1,0,0,
112 0,0,1,0,0,0,0,1,0,0,
113 0,0,1,0,0,0,0,1,0,0,
114 0,0,0,0,0,0,0,1,0,0,
115 0,1,1,2,1,1,1,2,1,1,
116 0,0,0,0,0,0,0,0,0,0,
117 0,0,0,0,0,0,0,0,0,0,
118 0,0,0,0,0,0,0,0,0,0,
119 0,0,0,0,0,0,0,0,0,0,
120 2,2,2,1,1,1,0,0,0,0
121 ], radar(&segments, size, false));
122 assert_eq!(vec![
123 1,0,1,0,0,0,0,1,1,0,
124 0,1,1,1,0,0,0,2,0,0,
125 0,0,2,0,1,0,1,1,1,0,
126 0,0,0,1,0,2,0,2,0,0,
127 0,1,1,2,3,1,3,2,1,1,
128 0,0,0,1,0,2,0,0,0,0,
129 0,0,1,0,0,0,1,0,0,0,
130 0,1,0,0,0,0,0,1,0,0,
131 1,0,0,0,0,0,0,0,1,0,
132 2,2,2,1,1,1,0,0,0,0
133 ], radar(&segments, size, true))
134 }
135
136}