summaryrefslogtreecommitdiffstats
path: root/2021
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 10:38:11 +0300
committerYigit Sever2021-12-13 10:38:11 +0300
commit74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch)
treee27db4cd0873c81a53d32277446d926d176304e0 /2021
parent3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff)
parentac8125750abed263619da4cc6d653bb5ab76f007 (diff)
downloadaoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.gz
aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.bz2
aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.zip
Merge remote-tracking branch 'origin/main'
Diffstat (limited to '2021')
-rw-r--r--2021/day5/Cargo.toml8
-rw-r--r--2021/day5/input500
-rw-r--r--2021/day5/input.example10
-rw-r--r--2021/day5/input.mini.example2
-rw-r--r--2021/day5/src/main.rs95
-rw-r--r--2021/day6/Cargo.toml9
-rw-r--r--2021/day6/input1
-rw-r--r--2021/day6/input.example1
-rw-r--r--2021/day6/src/main.rs33
9 files changed, 659 insertions, 0 deletions
diff --git a/2021/day5/Cargo.toml b/2021/day5/Cargo.toml
new file mode 100644
index 0000000..f9b62bf
--- /dev/null
+++ b/2021/day5/Cargo.toml
@@ -0,0 +1,8 @@
1[package]
2name = "day5"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
diff --git a/2021/day5/input b/2021/day5/input
new file mode 100644
index 0000000..45eb5f4
--- /dev/null
+++ b/2021/day5/input
@@ -0,0 +1,500 @@
1957,596 -> 957,182
2763,144 -> 69,144
3761,794 -> 911,944
4510,440 -> 147,440
5859,33 -> 859,409
6602,518 -> 748,518
7980,545 -> 676,241
860,528 -> 616,528
937,242 -> 37,892
10216,966 -> 216,120
11372,910 -> 372,307
12688,864 -> 94,864
13691,17 -> 167,17
14989,928 -> 989,816
15207,84 -> 982,84
16890,714 -> 502,326
17286,329 -> 686,329
18959,62 -> 460,62
19840,82 -> 780,82
20960,12 -> 14,958
21418,45 -> 983,610
22318,264 -> 988,934
23458,11 -> 861,11
24805,197 -> 380,622
25261,907 -> 261,734
26409,617 -> 826,617
27285,457 -> 759,931
28104,241 -> 913,241
29781,141 -> 781,671
3048,956 -> 348,956
31921,517 -> 974,517
32565,621 -> 724,621
33381,276 -> 381,59
3482,25 -> 946,25
35978,16 -> 17,977
36850,438 -> 125,438
37313,850 -> 313,195
38789,963 -> 789,29
39110,156 -> 241,156
40925,974 -> 458,974
4193,153 -> 696,756
42748,312 -> 120,312
43566,367 -> 360,367
44687,295 -> 687,284
45219,925 -> 180,925
4659,952 -> 59,140
47641,333 -> 361,53
48158,587 -> 158,96
49857,278 -> 857,195
50653,45 -> 34,664
51639,384 -> 814,384
5267,24 -> 902,859
53850,278 -> 850,448
54342,505 -> 342,287
55504,924 -> 536,956
56408,666 -> 408,310
57747,347 -> 747,176
58766,468 -> 54,468
59283,905 -> 283,421
60476,334 -> 476,59
61490,935 -> 753,672
62385,772 -> 940,772
63907,316 -> 853,370
64144,717 -> 746,115
65741,819 -> 230,819
66524,397 -> 524,237
67411,824 -> 288,824
68160,719 -> 744,135
69252,550 -> 440,738
70902,64 -> 902,422
71392,194 -> 955,757
72937,735 -> 937,446
73292,49 -> 985,49
74899,464 -> 481,46
75304,369 -> 648,369
76247,723 -> 247,697
77665,941 -> 185,461
78548,25 -> 446,127
79663,558 -> 154,558
8052,47 -> 872,867
81983,68 -> 983,795
82492,705 -> 654,867
8382,888 -> 33,888
84550,443 -> 229,764
85868,508 -> 375,15
86120,829 -> 652,829
87787,296 -> 814,296
88705,643 -> 351,643
8952,23 -> 558,23
90390,92 -> 390,393
91713,520 -> 897,520
92280,481 -> 827,481
93599,181 -> 599,235
94483,140 -> 773,140
95571,227 -> 571,779
96830,45 -> 830,745
97617,745 -> 449,745
9889,176 -> 841,928
99358,868 -> 906,320
100897,669 -> 654,669
101717,729 -> 123,135
102305,330 -> 305,598
103220,964 -> 600,964
104607,715 -> 492,715
105837,561 -> 820,561
106542,38 -> 542,118
107390,970 -> 915,445
108343,160 -> 475,160
109163,60 -> 972,869
110786,500 -> 946,500
111401,393 -> 401,910
112336,962 -> 528,962
113520,489 -> 520,525
114558,584 -> 363,584
115786,880 -> 584,678
116704,435 -> 153,986
117356,522 -> 235,643
118225,67 -> 229,71
119389,381 -> 55,47
120918,229 -> 332,229
121558,989 -> 558,370
122307,821 -> 307,447
12313,232 -> 742,961
124659,809 -> 467,809
125905,942 -> 456,942
126234,810 -> 234,497
127421,287 -> 643,65
128183,164 -> 312,164
129652,955 -> 977,630
130157,820 -> 853,124
131278,480 -> 278,384
132504,399 -> 298,605
133955,76 -> 798,76
134565,352 -> 109,352
135912,16 -> 66,862
136972,786 -> 235,49
137146,15 -> 146,186
138944,149 -> 497,596
139485,84 -> 762,84
140975,983 -> 16,24
14111,896 -> 887,20
142826,924 -> 126,224
143246,648 -> 158,736
144773,377 -> 582,377
145296,947 -> 378,865
146592,291 -> 592,861
147331,470 -> 16,470
148239,85 -> 705,85
149409,842 -> 409,966
150145,438 -> 145,340
151239,859 -> 239,409
152651,508 -> 314,845
153720,39 -> 176,583
154762,444 -> 762,29
155983,962 -> 60,39
156745,248 -> 91,902
157990,18 -> 990,503
158441,472 -> 441,211
159524,837 -> 248,561
160160,215 -> 842,897
161469,544 -> 989,544
162311,241 -> 103,33
163887,610 -> 746,610
164377,942 -> 377,803
165920,401 -> 823,401
16610,142 -> 10,486
167850,955 -> 850,578
168887,728 -> 887,678
169555,749 -> 84,749
170839,620 -> 32,620
171432,709 -> 432,183
172312,164 -> 859,711
17320,119 -> 739,838
174930,871 -> 324,265
17541,702 -> 41,520
17648,210 -> 794,956
177479,526 -> 479,123
178364,739 -> 364,379
179920,969 -> 73,122
180382,314 -> 382,604
181969,877 -> 129,37
182274,893 -> 52,671
183696,296 -> 696,221
184469,735 -> 626,892
185682,435 -> 682,937
186626,550 -> 626,604
187898,904 -> 898,755
188836,849 -> 445,458
189405,237 -> 405,724
190253,74 -> 908,729
191837,802 -> 597,802
192375,450 -> 316,450
193621,784 -> 314,784
194856,136 -> 188,804
195983,11 -> 29,965
19682,134 -> 752,134
197677,299 -> 573,299
19843,726 -> 43,304
199886,629 -> 886,603
200439,471 -> 74,836
201973,879 -> 122,28
202550,246 -> 497,246
20349,109 -> 816,876
204294,493 -> 271,493
205495,397 -> 495,342
206846,892 -> 881,927
207267,397 -> 72,592
208670,168 -> 22,816
209482,302 -> 482,681
210309,692 -> 453,692
211597,369 -> 597,589
212518,475 -> 633,475
213391,484 -> 493,382
214177,135 -> 139,135
215458,17 -> 825,17
216514,292 -> 272,50
217410,734 -> 142,734
218465,975 -> 542,898
21932,901 -> 346,587
220390,478 -> 571,659
221835,839 -> 189,839
222769,407 -> 769,788
223974,40 -> 103,911
224710,709 -> 246,709
225468,857 -> 343,982
226777,552 -> 951,552
22723,490 -> 346,490
228270,384 -> 855,384
229386,454 -> 166,454
230166,388 -> 319,388
231698,514 -> 698,180
232554,162 -> 528,162
233651,363 -> 879,363
234327,123 -> 327,255
235439,492 -> 719,212
236903,438 -> 903,356
23799,611 -> 99,552
238512,652 -> 45,185
239565,284 -> 565,180
24081,710 -> 637,154
241321,769 -> 164,769
24232,307 -> 32,204
243310,338 -> 310,914
244239,691 -> 357,691
245573,164 -> 559,164
246887,961 -> 693,767
247362,904 -> 362,944
248467,294 -> 236,294
249182,53 -> 182,264
25019,950 -> 951,18
251191,575 -> 440,824
252192,168 -> 690,666
253538,552 -> 891,905
254243,38 -> 243,403
255417,140 -> 417,818
256894,45 -> 726,213
257245,898 -> 245,527
258567,946 -> 567,983
259572,675 -> 363,675
260318,618 -> 735,618
261616,580 -> 981,580
26237,881 -> 134,881
263624,675 -> 271,322
264323,686 -> 770,686
26593,385 -> 626,385
266715,382 -> 862,382
267170,342 -> 170,84
268747,221 -> 380,221
269981,311 -> 981,577
270372,186 -> 372,826
271961,91 -> 130,922
27248,603 -> 48,110
27334,975 -> 935,74
274707,18 -> 707,192
275261,149 -> 41,369
27635,589 -> 35,247
277544,522 -> 437,522
278797,712 -> 346,712
279622,47 -> 78,591
280642,390 -> 642,414
281793,29 -> 69,29
282978,905 -> 978,296
283212,515 -> 212,920
284835,166 -> 68,933
285168,742 -> 168,720
286390,276 -> 666,276
287507,792 -> 987,792
288895,242 -> 895,402
289121,950 -> 943,128
290303,336 -> 303,205
291293,390 -> 293,736
292433,963 -> 433,867
293824,327 -> 620,531
294553,689 -> 867,689
295226,984 -> 226,284
296153,308 -> 173,308
297855,959 -> 345,959
298830,895 -> 830,981
299557,63 -> 557,205
30051,857 -> 328,857
301224,243 -> 224,517
302830,773 -> 830,520
303607,210 -> 782,385
304876,330 -> 327,330
305985,11 -> 18,978
306352,936 -> 111,936
307900,39 -> 986,39
30810,626 -> 10,795
309369,942 -> 11,942
310136,770 -> 654,252
311209,541 -> 209,557
31269,671 -> 848,671
313811,451 -> 194,451
314149,46 -> 850,747
315704,386 -> 704,953
316117,346 -> 387,346
317238,701 -> 688,251
318789,220 -> 789,815
319967,512 -> 967,213
320641,652 -> 151,162
32191,942 -> 312,942
32242,226 -> 801,985
323166,37 -> 603,37
324854,976 -> 951,976
325392,23 -> 772,23
326299,196 -> 299,288
327514,560 -> 844,890
328618,699 -> 348,429
329604,947 -> 604,735
330189,851 -> 407,633
331907,719 -> 907,449
332167,421 -> 864,421
333491,383 -> 538,383
334939,504 -> 939,433
33578,649 -> 78,975
336817,246 -> 322,741
337395,195 -> 205,195
338872,275 -> 872,500
339196,621 -> 280,621
340634,619 -> 634,422
341395,30 -> 395,232
342895,628 -> 619,904
34358,66 -> 186,194
344626,477 -> 708,559
345747,712 -> 747,28
346935,798 -> 220,798
347459,530 -> 704,530
348648,679 -> 79,110
349778,135 -> 778,66
350657,352 -> 906,352
351499,160 -> 615,160
352830,226 -> 830,489
353364,971 -> 826,971
354879,923 -> 543,923
355914,984 -> 612,984
356594,810 -> 508,810
35755,467 -> 269,253
358360,478 -> 360,902
359176,890 -> 176,431
360867,121 -> 16,972
361728,876 -> 65,213
362606,112 -> 842,112
363563,532 -> 914,532
364783,139 -> 369,139
365564,359 -> 846,359
36679,326 -> 15,390
367972,419 -> 972,35
368686,839 -> 755,839
369427,856 -> 77,856
370102,410 -> 925,410
371468,177 -> 901,610
372865,249 -> 930,314
373784,447 -> 863,368
374684,708 -> 223,708
375555,228 -> 555,900
376380,600 -> 222,600
377900,408 -> 188,408
378773,677 -> 773,16
379560,287 -> 560,790
380130,851 -> 959,22
381338,951 -> 626,663
382678,726 -> 405,726
383243,469 -> 243,276
384783,486 -> 66,486
385469,536 -> 469,770
386689,148 -> 689,340
387396,689 -> 396,673
388848,759 -> 848,770
389965,775 -> 965,605
390889,907 -> 284,302
391161,169 -> 942,950
392619,267 -> 394,492
393323,786 -> 18,481
394953,643 -> 884,643
395371,446 -> 371,252
396890,915 -> 192,217
397674,259 -> 674,247
398877,479 -> 877,117
399620,843 -> 620,734
400597,722 -> 597,939
401935,663 -> 935,469
402108,844 -> 108,596
403211,447 -> 211,482
404123,657 -> 721,59
405774,858 -> 774,86
406978,500 -> 978,305
407152,890 -> 937,105
408728,356 -> 728,261
409354,373 -> 433,452
410586,627 -> 586,949
411764,381 -> 735,410
412986,899 -> 199,112
413251,418 -> 251,596
414897,379 -> 897,388
415149,901 -> 253,901
416650,229 -> 46,229
417446,707 -> 446,44
418953,898 -> 697,898
419772,422 -> 762,432
420608,756 -> 927,756
421138,911 -> 716,333
422414,583 -> 244,583
423219,267 -> 219,751
42469,746 -> 69,886
425444,243 -> 493,194
426604,950 -> 244,950
427450,198 -> 31,617
428918,852 -> 109,43
429967,800 -> 267,100
430388,516 -> 760,516
431656,954 -> 513,954
43280,680 -> 80,44
433360,848 -> 360,800
43410,10 -> 989,989
435889,927 -> 104,142
436882,187 -> 812,187
437416,522 -> 356,522
438797,795 -> 273,271
439175,371 -> 12,371
440726,595 -> 956,595
441852,355 -> 852,357
442959,83 -> 338,83
443822,766 -> 140,84
444968,129 -> 181,916
44517,28 -> 975,986
44615,913 -> 624,304
447589,322 -> 468,443
448894,21 -> 309,606
44983,25 -> 963,905
450143,380 -> 377,146
451587,151 -> 507,151
452257,967 -> 257,882
453770,552 -> 770,573
454128,90 -> 886,848
455456,953 -> 456,669
456930,434 -> 668,434
457501,835 -> 796,540
45836,980 -> 989,27
459494,690 -> 626,690
460105,945 -> 972,78
461409,730 -> 409,62
462901,160 -> 831,90
463881,490 -> 28,490
464736,244 -> 38,942
465295,770 -> 199,770
466866,416 -> 808,416
467875,716 -> 875,189
468763,393 -> 241,915
469583,237 -> 704,237
47029,153 -> 637,153
471767,282 -> 767,107
472456,242 -> 456,165
473455,232 -> 749,232
47454,128 -> 766,840
475901,73 -> 939,73
476928,118 -> 854,118
47715,983 -> 975,23
478600,92 -> 260,92
479247,714 -> 247,490
480511,332 -> 19,824
48155,15 -> 968,928
482246,29 -> 25,29
48322,101 -> 761,840
484876,177 -> 139,914
485165,150 -> 822,807
486258,707 -> 773,707
48768,788 -> 68,875
488858,142 -> 758,142
489719,642 -> 810,642
490928,904 -> 928,338
491287,977 -> 978,286
492883,13 -> 883,31
493681,301 -> 681,957
494762,863 -> 405,506
495194,657 -> 733,118
49678,41 -> 497,41
497840,627 -> 840,168
498335,54 -> 335,822
499136,983 -> 526,593
500120,156 -> 120,630
diff --git a/2021/day5/input.example b/2021/day5/input.example
new file mode 100644
index 0000000..b258f68
--- /dev/null
+++ b/2021/day5/input.example
@@ -0,0 +1,10 @@
10,9 -> 5,9
28,0 -> 0,8
39,4 -> 3,4
42,2 -> 2,1
57,0 -> 7,4
66,4 -> 2,0
70,9 -> 2,9
83,4 -> 1,4
90,0 -> 8,8
105,5 -> 8,2
diff --git a/2021/day5/input.mini.example b/2021/day5/input.mini.example
new file mode 100644
index 0000000..b775b12
--- /dev/null
+++ b/2021/day5/input.mini.example
@@ -0,0 +1,2 @@
11,1 -> 3,3
29,7 -> 7,9
diff --git a/2021/day5/src/main.rs b/2021/day5/src/main.rs
new file mode 100644
index 0000000..498deb0
--- /dev/null
+++ b/2021/day5/src/main.rs
@@ -0,0 +1,95 @@
1use std::cmp::{max, min};
2use std::collections::HashMap;
3use std::env;
4use std::fs::File;
5use std::io::{BufRead, BufReader};
6
7fn main() {
8 let reader = open_file();
9
10 let mut coords: HashMap<(i32, i32), i32> = HashMap::new();
11
12 for line in reader.lines() {
13 if let Ok(line) = line {
14 let vents = line.split(" -> ").collect::<Vec<&str>>();
15 let first = vents[0].split(",").collect::<Vec<&str>>();
16 let second = vents[1].split(",").collect::<Vec<&str>>();
17
18 let first: Vec<i32> = first.into_iter().map(|s| s.parse().unwrap()).collect();
19 let second: Vec<i32> = second.into_iter().map(|s| s.parse().unwrap()).collect();
20
21 let xstep: i32;
22 let ystep: i32;
23 let start_x = min(first[0], second[0]);
24 let end_x = max(first[0], second[0]);
25 let start_y = min(first[1], second[1]);
26 let end_y = max(first[1], second[1]);
27 let distance = max(end_x - start_x, end_y - start_y);
28
29 if first[0] > second[0] {
30 xstep = -1;
31 } else if first[0] < second[0] {
32 xstep = 1;
33 } else {
34 xstep = 0;
35 }
36
37 if first[1] > second[1] {
38 ystep = -1;
39 } else if first[1] < second[1] {
40 ystep = 1;
41 } else {
42 ystep = 0;
43 }
44
45 for step in 0..=distance {
46 let counter = coords
47 .entry((first[0] + step * xstep, first[1] + step * ystep))
48 .or_insert(0);
49 *counter += 1;
50 }
51 }
52 }
53
54 // println!("coords: {:?}", coords);
55 // visualize(&coords);
56
57 let result = coords.iter().filter(|(_, &y)| y >= 2).count();
58 println!("{}", result);
59}
60
61fn visualize(map: &HashMap<(i32, i32), i32>) {
62 let start_x = map.iter().map(|((x, _), _)| x).min().unwrap();
63 let start_y = map.iter().map(|((_, y), _)| y).min().unwrap();
64 let end_x = map.iter().map(|((x, _), _)| x).max().unwrap();
65 let end_y = map.iter().map(|((_, y), _)| y).max().unwrap();
66 println!("start_x: {}", start_x);
67 println!("start_y: {}", start_y);
68 println!("end_x: {}", end_x);
69 println!("end_y: {}", end_y);
70
71 for y in *start_y..=*end_y {
72 for x in *start_x..=*end_x {
73 if let Some(val) = map.get(&(x, y)) {
74 print!("{}", val);
75 } else {
76 print!(".")
77 }
78 }
79 println!("");
80 }
81}
82
83fn open_file() -> BufReader<File> {
84 let args: Vec<String> = env::args().collect();
85
86 if args.len() != 2 {
87 eprintln!("Usage: {} <filename>", args[0]);
88 std::process::exit(1);
89 }
90
91 let filename = &args[1];
92
93 let file = File::open(filename).unwrap_or_else(|_| panic!("No such file: {}", filename));
94 BufReader::new(file)
95}
diff --git a/2021/day6/Cargo.toml b/2021/day6/Cargo.toml
new file mode 100644
index 0000000..9957f8b
--- /dev/null
+++ b/2021/day6/Cargo.toml
@@ -0,0 +1,9 @@
1[package]
2name = "day6"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
9itertools = "0.10.3"
diff --git a/2021/day6/input b/2021/day6/input
new file mode 100644
index 0000000..9bb513a
--- /dev/null
+++ b/2021/day6/input
@@ -0,0 +1 @@
1,1,3,5,3,1,1,4,1,1,5,2,4,3,1,1,3,1,1,5,5,1,3,2,5,4,1,1,5,1,4,2,1,4,2,1,4,4,1,5,1,4,4,1,1,5,1,5,1,5,1,1,1,5,1,2,5,1,1,3,2,2,2,1,4,1,1,2,4,1,3,1,2,1,3,5,2,3,5,1,1,4,3,3,5,1,5,3,1,2,3,4,1,1,5,4,1,3,4,4,1,2,4,4,1,1,3,5,3,1,2,2,5,1,4,1,3,3,3,3,1,1,2,1,5,3,4,5,1,5,2,5,3,2,1,4,2,1,1,1,4,1,2,1,2,2,4,5,5,5,4,1,4,1,4,2,3,2,3,1,1,2,3,1,1,1,5,2,2,5,3,1,4,1,2,1,1,5,3,1,4,5,1,4,2,1,1,5,1,5,4,1,5,5,2,3,1,3,5,1,1,1,1,3,1,1,4,1,5,2,1,1,3,5,1,1,4,2,1,2,5,2,5,1,1,1,2,3,5,5,1,4,3,2,2,3,2,1,1,4,1,3,5,2,3,1,1,5,1,3,5,1,1,5,5,3,1,3,3,1,2,3,1,5,1,3,2,1,3,1,1,2,3,5,3,5,5,4,3,1,5,1,1,2,3,2,2,1,1,2,1,4,1,2,3,3,3,1,3,5
diff --git a/2021/day6/input.example b/2021/day6/input.example
new file mode 100644
index 0000000..55129f1
--- /dev/null
+++ b/2021/day6/input.example
@@ -0,0 +1 @@
3,4,3,1,2
diff --git a/2021/day6/src/main.rs b/2021/day6/src/main.rs
new file mode 100644
index 0000000..378764e
--- /dev/null
+++ b/2021/day6/src/main.rs
@@ -0,0 +1,33 @@
1use std::env;
2
3fn main() {
4 let args: Vec<String> = env::args().collect();
5
6 if args.len() != 2 {
7 eprintln!("Usage: {} <filename>", args[0]);
8 std::process::exit(1);
9 }
10 let mut smart_lanternfish: [u64; 9] = [0; 9];
11
12 let foo: String = std::fs::read_to_string(&args[1]).unwrap().parse().unwrap();
13 let mut lanternfish: Vec<u8> = Vec::new();
14 lanternfish.extend(foo.trim().split(",").map(|x| x.parse::<u8>().unwrap()));
15
16 for initial_fish in &lanternfish {
17 smart_lanternfish[*initial_fish as usize] += 1;
18 }
19
20 // println!("{:?}", smart_lanternfish);
21 for _day in 0..256 {
22 smart_lanternfish.rotate_left(1);
23 smart_lanternfish[6] += smart_lanternfish[8];
24 // println!(
25 // "After {day:>width$} days: {lanternfish:?}",
26 // day = _day,
27 // width = 2,
28 // lanternfish = smart_lanternfish
29 // );
30 }
31
32 println!("{}", smart_lanternfish.iter().sum::<u64>());
33}