From 54cbcaa842ea9032c850158183eeba45909e26e2 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 12 Dec 2021 02:45:22 +0300 Subject: 2021, day5: done --- 2021/day5/Cargo.toml | 8 + 2021/day5/input | 500 +++++++++++++++++++++++++++++++++++++++++++ 2021/day5/input.example | 10 + 2021/day5/input.mini.example | 2 + 2021/day5/src/main.rs | 95 ++++++++ 5 files changed, 615 insertions(+) create mode 100644 2021/day5/Cargo.toml create mode 100644 2021/day5/input create mode 100644 2021/day5/input.example create mode 100644 2021/day5/input.mini.example create mode 100644 2021/day5/src/main.rs (limited to '2021') 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 @@ +[package] +name = "day5" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[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 @@ +957,596 -> 957,182 +763,144 -> 69,144 +761,794 -> 911,944 +510,440 -> 147,440 +859,33 -> 859,409 +602,518 -> 748,518 +980,545 -> 676,241 +60,528 -> 616,528 +37,242 -> 37,892 +216,966 -> 216,120 +372,910 -> 372,307 +688,864 -> 94,864 +691,17 -> 167,17 +989,928 -> 989,816 +207,84 -> 982,84 +890,714 -> 502,326 +286,329 -> 686,329 +959,62 -> 460,62 +840,82 -> 780,82 +960,12 -> 14,958 +418,45 -> 983,610 +318,264 -> 988,934 +458,11 -> 861,11 +805,197 -> 380,622 +261,907 -> 261,734 +409,617 -> 826,617 +285,457 -> 759,931 +104,241 -> 913,241 +781,141 -> 781,671 +48,956 -> 348,956 +921,517 -> 974,517 +565,621 -> 724,621 +381,276 -> 381,59 +82,25 -> 946,25 +978,16 -> 17,977 +850,438 -> 125,438 +313,850 -> 313,195 +789,963 -> 789,29 +110,156 -> 241,156 +925,974 -> 458,974 +93,153 -> 696,756 +748,312 -> 120,312 +566,367 -> 360,367 +687,295 -> 687,284 +219,925 -> 180,925 +59,952 -> 59,140 +641,333 -> 361,53 +158,587 -> 158,96 +857,278 -> 857,195 +653,45 -> 34,664 +639,384 -> 814,384 +67,24 -> 902,859 +850,278 -> 850,448 +342,505 -> 342,287 +504,924 -> 536,956 +408,666 -> 408,310 +747,347 -> 747,176 +766,468 -> 54,468 +283,905 -> 283,421 +476,334 -> 476,59 +490,935 -> 753,672 +385,772 -> 940,772 +907,316 -> 853,370 +144,717 -> 746,115 +741,819 -> 230,819 +524,397 -> 524,237 +411,824 -> 288,824 +160,719 -> 744,135 +252,550 -> 440,738 +902,64 -> 902,422 +392,194 -> 955,757 +937,735 -> 937,446 +292,49 -> 985,49 +899,464 -> 481,46 +304,369 -> 648,369 +247,723 -> 247,697 +665,941 -> 185,461 +548,25 -> 446,127 +663,558 -> 154,558 +52,47 -> 872,867 +983,68 -> 983,795 +492,705 -> 654,867 +82,888 -> 33,888 +550,443 -> 229,764 +868,508 -> 375,15 +120,829 -> 652,829 +787,296 -> 814,296 +705,643 -> 351,643 +52,23 -> 558,23 +390,92 -> 390,393 +713,520 -> 897,520 +280,481 -> 827,481 +599,181 -> 599,235 +483,140 -> 773,140 +571,227 -> 571,779 +830,45 -> 830,745 +617,745 -> 449,745 +89,176 -> 841,928 +358,868 -> 906,320 +897,669 -> 654,669 +717,729 -> 123,135 +305,330 -> 305,598 +220,964 -> 600,964 +607,715 -> 492,715 +837,561 -> 820,561 +542,38 -> 542,118 +390,970 -> 915,445 +343,160 -> 475,160 +163,60 -> 972,869 +786,500 -> 946,500 +401,393 -> 401,910 +336,962 -> 528,962 +520,489 -> 520,525 +558,584 -> 363,584 +786,880 -> 584,678 +704,435 -> 153,986 +356,522 -> 235,643 +225,67 -> 229,71 +389,381 -> 55,47 +918,229 -> 332,229 +558,989 -> 558,370 +307,821 -> 307,447 +13,232 -> 742,961 +659,809 -> 467,809 +905,942 -> 456,942 +234,810 -> 234,497 +421,287 -> 643,65 +183,164 -> 312,164 +652,955 -> 977,630 +157,820 -> 853,124 +278,480 -> 278,384 +504,399 -> 298,605 +955,76 -> 798,76 +565,352 -> 109,352 +912,16 -> 66,862 +972,786 -> 235,49 +146,15 -> 146,186 +944,149 -> 497,596 +485,84 -> 762,84 +975,983 -> 16,24 +11,896 -> 887,20 +826,924 -> 126,224 +246,648 -> 158,736 +773,377 -> 582,377 +296,947 -> 378,865 +592,291 -> 592,861 +331,470 -> 16,470 +239,85 -> 705,85 +409,842 -> 409,966 +145,438 -> 145,340 +239,859 -> 239,409 +651,508 -> 314,845 +720,39 -> 176,583 +762,444 -> 762,29 +983,962 -> 60,39 +745,248 -> 91,902 +990,18 -> 990,503 +441,472 -> 441,211 +524,837 -> 248,561 +160,215 -> 842,897 +469,544 -> 989,544 +311,241 -> 103,33 +887,610 -> 746,610 +377,942 -> 377,803 +920,401 -> 823,401 +10,142 -> 10,486 +850,955 -> 850,578 +887,728 -> 887,678 +555,749 -> 84,749 +839,620 -> 32,620 +432,709 -> 432,183 +312,164 -> 859,711 +20,119 -> 739,838 +930,871 -> 324,265 +41,702 -> 41,520 +48,210 -> 794,956 +479,526 -> 479,123 +364,739 -> 364,379 +920,969 -> 73,122 +382,314 -> 382,604 +969,877 -> 129,37 +274,893 -> 52,671 +696,296 -> 696,221 +469,735 -> 626,892 +682,435 -> 682,937 +626,550 -> 626,604 +898,904 -> 898,755 +836,849 -> 445,458 +405,237 -> 405,724 +253,74 -> 908,729 +837,802 -> 597,802 +375,450 -> 316,450 +621,784 -> 314,784 +856,136 -> 188,804 +983,11 -> 29,965 +82,134 -> 752,134 +677,299 -> 573,299 +43,726 -> 43,304 +886,629 -> 886,603 +439,471 -> 74,836 +973,879 -> 122,28 +550,246 -> 497,246 +49,109 -> 816,876 +294,493 -> 271,493 +495,397 -> 495,342 +846,892 -> 881,927 +267,397 -> 72,592 +670,168 -> 22,816 +482,302 -> 482,681 +309,692 -> 453,692 +597,369 -> 597,589 +518,475 -> 633,475 +391,484 -> 493,382 +177,135 -> 139,135 +458,17 -> 825,17 +514,292 -> 272,50 +410,734 -> 142,734 +465,975 -> 542,898 +32,901 -> 346,587 +390,478 -> 571,659 +835,839 -> 189,839 +769,407 -> 769,788 +974,40 -> 103,911 +710,709 -> 246,709 +468,857 -> 343,982 +777,552 -> 951,552 +23,490 -> 346,490 +270,384 -> 855,384 +386,454 -> 166,454 +166,388 -> 319,388 +698,514 -> 698,180 +554,162 -> 528,162 +651,363 -> 879,363 +327,123 -> 327,255 +439,492 -> 719,212 +903,438 -> 903,356 +99,611 -> 99,552 +512,652 -> 45,185 +565,284 -> 565,180 +81,710 -> 637,154 +321,769 -> 164,769 +32,307 -> 32,204 +310,338 -> 310,914 +239,691 -> 357,691 +573,164 -> 559,164 +887,961 -> 693,767 +362,904 -> 362,944 +467,294 -> 236,294 +182,53 -> 182,264 +19,950 -> 951,18 +191,575 -> 440,824 +192,168 -> 690,666 +538,552 -> 891,905 +243,38 -> 243,403 +417,140 -> 417,818 +894,45 -> 726,213 +245,898 -> 245,527 +567,946 -> 567,983 +572,675 -> 363,675 +318,618 -> 735,618 +616,580 -> 981,580 +37,881 -> 134,881 +624,675 -> 271,322 +323,686 -> 770,686 +93,385 -> 626,385 +715,382 -> 862,382 +170,342 -> 170,84 +747,221 -> 380,221 +981,311 -> 981,577 +372,186 -> 372,826 +961,91 -> 130,922 +48,603 -> 48,110 +34,975 -> 935,74 +707,18 -> 707,192 +261,149 -> 41,369 +35,589 -> 35,247 +544,522 -> 437,522 +797,712 -> 346,712 +622,47 -> 78,591 +642,390 -> 642,414 +793,29 -> 69,29 +978,905 -> 978,296 +212,515 -> 212,920 +835,166 -> 68,933 +168,742 -> 168,720 +390,276 -> 666,276 +507,792 -> 987,792 +895,242 -> 895,402 +121,950 -> 943,128 +303,336 -> 303,205 +293,390 -> 293,736 +433,963 -> 433,867 +824,327 -> 620,531 +553,689 -> 867,689 +226,984 -> 226,284 +153,308 -> 173,308 +855,959 -> 345,959 +830,895 -> 830,981 +557,63 -> 557,205 +51,857 -> 328,857 +224,243 -> 224,517 +830,773 -> 830,520 +607,210 -> 782,385 +876,330 -> 327,330 +985,11 -> 18,978 +352,936 -> 111,936 +900,39 -> 986,39 +10,626 -> 10,795 +369,942 -> 11,942 +136,770 -> 654,252 +209,541 -> 209,557 +69,671 -> 848,671 +811,451 -> 194,451 +149,46 -> 850,747 +704,386 -> 704,953 +117,346 -> 387,346 +238,701 -> 688,251 +789,220 -> 789,815 +967,512 -> 967,213 +641,652 -> 151,162 +91,942 -> 312,942 +42,226 -> 801,985 +166,37 -> 603,37 +854,976 -> 951,976 +392,23 -> 772,23 +299,196 -> 299,288 +514,560 -> 844,890 +618,699 -> 348,429 +604,947 -> 604,735 +189,851 -> 407,633 +907,719 -> 907,449 +167,421 -> 864,421 +491,383 -> 538,383 +939,504 -> 939,433 +78,649 -> 78,975 +817,246 -> 322,741 +395,195 -> 205,195 +872,275 -> 872,500 +196,621 -> 280,621 +634,619 -> 634,422 +395,30 -> 395,232 +895,628 -> 619,904 +58,66 -> 186,194 +626,477 -> 708,559 +747,712 -> 747,28 +935,798 -> 220,798 +459,530 -> 704,530 +648,679 -> 79,110 +778,135 -> 778,66 +657,352 -> 906,352 +499,160 -> 615,160 +830,226 -> 830,489 +364,971 -> 826,971 +879,923 -> 543,923 +914,984 -> 612,984 +594,810 -> 508,810 +55,467 -> 269,253 +360,478 -> 360,902 +176,890 -> 176,431 +867,121 -> 16,972 +728,876 -> 65,213 +606,112 -> 842,112 +563,532 -> 914,532 +783,139 -> 369,139 +564,359 -> 846,359 +79,326 -> 15,390 +972,419 -> 972,35 +686,839 -> 755,839 +427,856 -> 77,856 +102,410 -> 925,410 +468,177 -> 901,610 +865,249 -> 930,314 +784,447 -> 863,368 +684,708 -> 223,708 +555,228 -> 555,900 +380,600 -> 222,600 +900,408 -> 188,408 +773,677 -> 773,16 +560,287 -> 560,790 +130,851 -> 959,22 +338,951 -> 626,663 +678,726 -> 405,726 +243,469 -> 243,276 +783,486 -> 66,486 +469,536 -> 469,770 +689,148 -> 689,340 +396,689 -> 396,673 +848,759 -> 848,770 +965,775 -> 965,605 +889,907 -> 284,302 +161,169 -> 942,950 +619,267 -> 394,492 +323,786 -> 18,481 +953,643 -> 884,643 +371,446 -> 371,252 +890,915 -> 192,217 +674,259 -> 674,247 +877,479 -> 877,117 +620,843 -> 620,734 +597,722 -> 597,939 +935,663 -> 935,469 +108,844 -> 108,596 +211,447 -> 211,482 +123,657 -> 721,59 +774,858 -> 774,86 +978,500 -> 978,305 +152,890 -> 937,105 +728,356 -> 728,261 +354,373 -> 433,452 +586,627 -> 586,949 +764,381 -> 735,410 +986,899 -> 199,112 +251,418 -> 251,596 +897,379 -> 897,388 +149,901 -> 253,901 +650,229 -> 46,229 +446,707 -> 446,44 +953,898 -> 697,898 +772,422 -> 762,432 +608,756 -> 927,756 +138,911 -> 716,333 +414,583 -> 244,583 +219,267 -> 219,751 +69,746 -> 69,886 +444,243 -> 493,194 +604,950 -> 244,950 +450,198 -> 31,617 +918,852 -> 109,43 +967,800 -> 267,100 +388,516 -> 760,516 +656,954 -> 513,954 +80,680 -> 80,44 +360,848 -> 360,800 +10,10 -> 989,989 +889,927 -> 104,142 +882,187 -> 812,187 +416,522 -> 356,522 +797,795 -> 273,271 +175,371 -> 12,371 +726,595 -> 956,595 +852,355 -> 852,357 +959,83 -> 338,83 +822,766 -> 140,84 +968,129 -> 181,916 +17,28 -> 975,986 +15,913 -> 624,304 +589,322 -> 468,443 +894,21 -> 309,606 +83,25 -> 963,905 +143,380 -> 377,146 +587,151 -> 507,151 +257,967 -> 257,882 +770,552 -> 770,573 +128,90 -> 886,848 +456,953 -> 456,669 +930,434 -> 668,434 +501,835 -> 796,540 +36,980 -> 989,27 +494,690 -> 626,690 +105,945 -> 972,78 +409,730 -> 409,62 +901,160 -> 831,90 +881,490 -> 28,490 +736,244 -> 38,942 +295,770 -> 199,770 +866,416 -> 808,416 +875,716 -> 875,189 +763,393 -> 241,915 +583,237 -> 704,237 +29,153 -> 637,153 +767,282 -> 767,107 +456,242 -> 456,165 +455,232 -> 749,232 +54,128 -> 766,840 +901,73 -> 939,73 +928,118 -> 854,118 +15,983 -> 975,23 +600,92 -> 260,92 +247,714 -> 247,490 +511,332 -> 19,824 +55,15 -> 968,928 +246,29 -> 25,29 +22,101 -> 761,840 +876,177 -> 139,914 +165,150 -> 822,807 +258,707 -> 773,707 +68,788 -> 68,875 +858,142 -> 758,142 +719,642 -> 810,642 +928,904 -> 928,338 +287,977 -> 978,286 +883,13 -> 883,31 +681,301 -> 681,957 +762,863 -> 405,506 +194,657 -> 733,118 +78,41 -> 497,41 +840,627 -> 840,168 +335,54 -> 335,822 +136,983 -> 526,593 +120,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 @@ +0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,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 @@ +1,1 -> 3,3 +9,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 @@ +use std::cmp::{max, min}; +use std::collections::HashMap; +use std::env; +use std::fs::File; +use std::io::{BufRead, BufReader}; + +fn main() { + let reader = open_file(); + + let mut coords: HashMap<(i32, i32), i32> = HashMap::new(); + + for line in reader.lines() { + if let Ok(line) = line { + let vents = line.split(" -> ").collect::>(); + let first = vents[0].split(",").collect::>(); + let second = vents[1].split(",").collect::>(); + + let first: Vec = first.into_iter().map(|s| s.parse().unwrap()).collect(); + let second: Vec = second.into_iter().map(|s| s.parse().unwrap()).collect(); + + let xstep: i32; + let ystep: i32; + let start_x = min(first[0], second[0]); + let end_x = max(first[0], second[0]); + let start_y = min(first[1], second[1]); + let end_y = max(first[1], second[1]); + let distance = max(end_x - start_x, end_y - start_y); + + if first[0] > second[0] { + xstep = -1; + } else if first[0] < second[0] { + xstep = 1; + } else { + xstep = 0; + } + + if first[1] > second[1] { + ystep = -1; + } else if first[1] < second[1] { + ystep = 1; + } else { + ystep = 0; + } + + for step in 0..=distance { + let counter = coords + .entry((first[0] + step * xstep, first[1] + step * ystep)) + .or_insert(0); + *counter += 1; + } + } + } + + // println!("coords: {:?}", coords); + // visualize(&coords); + + let result = coords.iter().filter(|(_, &y)| y >= 2).count(); + println!("{}", result); +} + +fn visualize(map: &HashMap<(i32, i32), i32>) { + let start_x = map.iter().map(|((x, _), _)| x).min().unwrap(); + let start_y = map.iter().map(|((_, y), _)| y).min().unwrap(); + let end_x = map.iter().map(|((x, _), _)| x).max().unwrap(); + let end_y = map.iter().map(|((_, y), _)| y).max().unwrap(); + println!("start_x: {}", start_x); + println!("start_y: {}", start_y); + println!("end_x: {}", end_x); + println!("end_y: {}", end_y); + + for y in *start_y..=*end_y { + for x in *start_x..=*end_x { + if let Some(val) = map.get(&(x, y)) { + print!("{}", val); + } else { + print!(".") + } + } + println!(""); + } +} + +fn open_file() -> BufReader { + let args: Vec = env::args().collect(); + + if args.len() != 2 { + eprintln!("Usage: {} ", args[0]); + std::process::exit(1); + } + + let filename = &args[1]; + + let file = File::open(filename).unwrap_or_else(|_| panic!("No such file: {}", filename)); + BufReader::new(file) +} -- cgit v1.2.3-70-g09d2 From ac8125750abed263619da4cc6d653bb5ab76f007 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Mon, 13 Dec 2021 00:36:34 +0300 Subject: 2021, day6: done --- 2021/day6/Cargo.toml | 9 +++++++++ 2021/day6/input | 1 + 2021/day6/input.example | 1 + 2021/day6/src/main.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 2021/day6/Cargo.toml create mode 100644 2021/day6/input create mode 100644 2021/day6/input.example create mode 100644 2021/day6/src/main.rs (limited to '2021') 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 @@ +[package] +name = "day6" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "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 @@ +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + + if args.len() != 2 { + eprintln!("Usage: {} ", args[0]); + std::process::exit(1); + } + let mut smart_lanternfish: [u64; 9] = [0; 9]; + + let foo: String = std::fs::read_to_string(&args[1]).unwrap().parse().unwrap(); + let mut lanternfish: Vec = Vec::new(); + lanternfish.extend(foo.trim().split(",").map(|x| x.parse::().unwrap())); + + for initial_fish in &lanternfish { + smart_lanternfish[*initial_fish as usize] += 1; + } + + // println!("{:?}", smart_lanternfish); + for _day in 0..256 { + smart_lanternfish.rotate_left(1); + smart_lanternfish[6] += smart_lanternfish[8]; + // println!( + // "After {day:>width$} days: {lanternfish:?}", + // day = _day, + // width = 2, + // lanternfish = smart_lanternfish + // ); + } + + println!("{}", smart_lanternfish.iter().sum::()); +} -- cgit v1.2.3-70-g09d2