diff options
70 files changed, 11292 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e7caa9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target/ diff --git a/d01/Cargo.toml b/d01/Cargo.toml new file mode 100644 index 0000000..0c0a29c --- /dev/null +++ b/d01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "d01" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/d01/input.txt b/d01/input.txt new file mode 100644 index 0000000..2c9ac6a --- /dev/null +++ b/d01/input.txt @@ -0,0 +1 @@ +L2, L5, L5, R5, L2, L4, R1, R1, L4, R2, R1, L1, L4, R1, L4, L4, R5, R3, R1, L1, R1, L5, L1, R5, L4, R2, L5, L3, L3, R3, L3, R4, R4, L2, L5, R1, R2, L2, L1, R3, R4, L193, R3, L5, R45, L1, R4, R79, L5, L5, R5, R1, L4, R3, R3, L4, R185, L5, L3, L1, R5, L2, R1, R3, R2, L3, L4, L2, R2, L3, L2, L2, L3, L5, R3, R4, L5, R1, R2, L2, R4, R3, L4, L3, L1, R3, R2, R1, R1, L3, R4, L5, R2, R1, R3, L3, L2, L2, R2, R1, R2, R3, L3, L3, R4, L4, R4, R4, R4, L3, L1, L2, R5, R2, R2, R2, L4, L3, L4, R4, L5, L4, R2, L4, L4, R4, R1, R5, L2, L4, L5, L3, L2, L4, L4, R3, L3, L4, R1, L2, R3, L2, R1, R2, R5, L4, L2, L1, L3, R2, R3, L2, L1, L5, L2, L1, R4 diff --git a/d01/src/main.rs b/d01/src/main.rs new file mode 100644 index 0000000..3117a5a --- /dev/null +++ b/d01/src/main.rs @@ -0,0 +1,123 @@ +use std::fs; + +enum Heading { + North, + East, + South, + West, +} + +impl Heading { + fn turn_left(&mut self) { + *self = match *self { + Heading::North => Heading::West, + Heading::East => Heading::North, + Heading::South => Heading::East, + Heading::West => Heading::South, + }; + } + + fn turn_right(&mut self) { + *self = match *self { + Heading::North => Heading::East, + Heading::East => Heading::South, + Heading::South => Heading::West, + Heading::West => Heading::North, + }; + } +} + +fn get_route(input: &str) -> Vec<(isize, isize)> { + let steps: Vec<&str> = input.trim().split(", ").collect(); + + let mut pos = (0, 0); + let mut route = vec![pos]; + + let mut facing = Heading::North; + for s in steps { + // Read which way to turn + if s.starts_with("L") { + facing.turn_left(); + } else if s.starts_with("R") { + facing.turn_right(); + } + + // Read how far to go in the new direction + let dist: usize = s[1..].parse().unwrap(); + + // Add each 1-block step to `route' so that part 2 can be solved + for _i in 0..dist { + pos = match facing { + Heading::North => (pos.0 + 1, pos.1), + Heading::East => (pos.0, pos.1 + 1), + Heading::South => (pos.0 - 1, pos.1), + Heading::West => (pos.0, pos.1 - 1), + }; + route.push(pos); + } + } + + route +} + +fn solve_part1(input: &str) -> isize { + let route = get_route(input); + let ended = route.last().unwrap(); + + // Manhattan distance from origin to `ended' position + ended.0.abs() + ended.1.abs() +} + +fn solve_part2(input: &str) -> Option<isize> { + let route = get_route(input); + + for i in 1..route.len() { + let pos = route[i - 1]; + if route[i..].contains(&pos) { + // Manhattan distance to first twice-visited position + return Some(pos.0.abs() + pos.1.abs()); + } + } + + None +} + +fn main() { + // Read instructions from input text file + let input = fs::read_to_string("input.txt").unwrap(); + + // Part 1 gives 181 for me + println!("Part 1 solution: {}", solve_part1(&input)); + + // Part 2 gives 140 for me + println!("Part 2 solution: {}", solve_part2(&input).unwrap()); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_example1() { + let input = "R2, L3"; + assert_eq!(solve_part1(input), 5); + } + + #[test] + fn part1_example2() { + let input = "R2, R2, R2"; + assert_eq!(solve_part1(input), 2); + } + + #[test] + fn part1_example3() { + let input = "R5, L5, R5, R3"; + assert_eq!(solve_part1(input), 12); + } + + #[test] + fn part2_example1() { + let input = "R8, R4, R4, R8"; + assert_eq!(solve_part2(input).unwrap(), 4); + } +} diff --git a/d02/Cargo.toml b/d02/Cargo.toml new file mode 100644 index 0000000..47add02 --- /dev/null +++ b/d02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "d02" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/d02/input.txt b/d02/input.txt new file mode 100644 index 0000000..ba5a73d --- /dev/null +++ b/d02/input.txt @@ -0,0 +1,5 @@ +RUDULRLLUULRURDDRRUDURULLLDRLRLUDDLUDUDDUDRRDUDULDUUULLRULLRLDDLDLDDRLRRRRUDLLDDUULDRLLUDDRRUURLULRRRDLLURRUUDURUDDURLUDDDLUDDUUDUURUDLRDRDRLRDRLDRUDRUUDLRDDRRURDDLRDDRRURDUDDLULLUDRURURRRLRRUDUULULULRRLDLUDUURRLLRUDLLDRDDLRRRULRUDLULDDLLLULDLRUDLLLLRDDLRDRLDRLLRDRRDLRDULULRLLLDRUDRRRUULRUULDRURLUDRURRDLLDLRDLDDDDRRLUDLRRLUUUURDRDDLRRURURRDUULLRLURLURUDDDRDURDUUDRLRLRRLDDLDLDLDDDUDDULURLDDLLRLRRDULUDDLULRLUDDLDLRULUUUDRLDRUDURLUDDRLLRUULDLRRRRDLLLLURULLRDRRUDLUULRRDLLRLRLUDLDDULLDLLRDLDLL +LLUUUUUUDUDRLRDRDLDURRRLLRRLRURLLUURRLLUDUDLULUURUUURDLUDLDDLULLRDLRUULDLRDUDURLLDDUDUDULLUDDUULLLUULRRRLULRURRDLRUDUDDURRRDRUURDURLLULLRULLDRUULLURLDRDUUDDDDDDRRLDRLRRRLULDDUURRLLLLDRURLURDRDRDURUDUURRDUDUDRLLUUDDRLUDDDRDLDLRLDRURRDLLRULDRLLURURRLUULLRLRRURDDRDRUUURUURUUUDLLRRLUDRLDLRLURLDLUDDUDDDLDUDRRLDLRURULRLLRDUULURRRULDLLLRLDDDUURRRRDULLRURRLULULDLRRUDUDDLRUURDLDUDDUDRRDLRRRDUDUUUDLLDDDDLURLURRRUUULLLULRRLLLLLLULDUUDLRUDRRDLRDUUDUDLLRLDLLRUURDUUURUUUDDLLUUDLULDURLULULUUUDRUDULLURRULRULLRDLDDU +RLUUURULLDLRLDUDRDURRDUURLLUDDDUULRRRLRLURDDRUULUDULDUUDDDDUDDDDRUDDLDUUDRUDLRRRLLRDDLLLRLLRUULRUULDDRURRLURRLRLULDDRRRDDURDDRDRDULRUDRUUDULRLLULDLRLLDRULRDDRRDDUDLRLLUDRDRRRLUDULRDLRDDURRUUDDRRUDURRUUUDDRRDUDURLUUDUDUURDDDLURLULLUULULURUDUUDRUDULLUUULURDLDUULLDDLLDULRLRLRDUUURUUDLRLDURUDRLDULLUDLDLLRDUURRDUDURLUUUDLLRRULRLULRLDLLURDURRULRLLRRDUDLLRDRRRRDLUUDRUUUDDLRLUDDDDDDRURRRUUURRDLLRURLDDLLDLRRLLLDRRULRRUDLDRDDRRLULURLLUURURURRRRUUUUURUDURLRLLLULULDLLDLRDRRULUDUDRDRRDRDRRDUDLLLRUDRUDDDULRULRRRDRLRUUUURUDURDUUULLULRUDDULDUUDLDURRD +ULRULDDLDLULLLRRRLRUDDDDDLLDDUDLRRDULUUDRDLRRURDRRLUULRURUDRRULDLLLUDRUUDULULUDDRUDDDRDURRRDRDUUURLRDULUDRDRLDRUDDLLLDRRULUDLUDLDLLRRUDUULULDLDLLUURDLDDLLUUDURLURLLLDRDLDRRLRULUURRDRULRUUURULRRUDDDDLLDLDDLLRRLRRRRDUUDUDLDRDRRURDLRURULDLRDLLLLRUDRLLRDLRLRDURDRUDURRRLRDRDLLRLUDDDDRLRLLDUURRURLUURUULUDLUURDRRUDDLUDUDDDURRDRUDRLRULDULUUUUUUDDUDRUDUUURUDRRDLUDLUUDUULUDURDLDDDLLURRURUUDUDDRRDRLLULULDRLRURRDDDRDUUURDDDRULUDRDDLDURRLDDDLRRRLDDRDURULDLUDLLLURLURRLRRULDLLDDUDRRULDRRRRLURRUULRRRUDLURDLLDLLDULUUDRRLDLLLDRLRUDLUULDLDRUDUDURDRUDRDDDLRLULLUR +LRLUUURRLRRRRRUURRLLULRLULLDLUDLUDRDDRLDLRLULLURDURLURDLLRLDUUDDURRRRLDLLRULLRLDLLUUDRLDDLLDRULDRLLRURDLRURRUDLULLRURDLURRURUDULLDRLLUUULUDRURRUUDUDULUUULRLDDULDRDLUDDUDDDLRURULLDLLLRLLUURDLRUDLLLLDLLRLRUUUDDRUUUUDLDLRDDURLDURUULLLUUDLLLLDULRRRLLDLDRRDRLUDRUDURLLUDLRLLUDUDRDDDRDLRDLRULUULDRLUDLRLDUURLRRLUDDDUUDDDUDRLDLDUDLURUULLDDDURUUULRLUDLDURUUDRDRURUDDUURDUUUDLLDLDLDURUURLLLLRURUURURULRULLRUDLRRUUUUUDRRLLRDDUURDRDRDDDUDRLURDRRRUDLLLDURDLUUDLLUDDULUUDLDUUULLDRDLRURUURRDURRDLURRRRLLUUULRDULDDLDUURRDLDLLULRRLLUDLDUDLUUL diff --git a/d02/src/main.rs b/d02/src/main.rs new file mode 100644 index 0000000..33c7ec0 --- /dev/null +++ b/d02/src/main.rs @@ -0,0 +1,86 @@ +use std::fs; + +fn solve_part1(lines: &Vec<&str>) -> String { + let mut result = String::new(); + + let mut num = 5; + for line in lines { + for c in line.chars() { + num = match c { + 'U' => if num - 3 > 0 { num - 3 } else { num }, + 'D' => if num + 3 < 10 { num + 3 } else { num }, + 'L' => if num % 3 != 1 { num - 1 } else { num }, + 'R' => if num % 3 != 0 { num + 1 } else { num }, + _ => num, // shouldn't happen + } + } + result.push_str(&num.to_string()); + } + + result +} + +fn solve_part2(lines: &Vec<&str>) -> String { + let mut result = String::new(); + + // Part 2's keypad is a bit more exotic + let keypad = [ + [0, 0, 1, 0, 0], + [0, 2, 3, 4, 0], + [5, 6, 7, 8, 9], + [0, 10, 11, 12, 0], + [0, 0, 13, 0, 0], + ]; + let mut pos = (2, 0); // (row, col) index + + for line in lines { + for c in line.chars() { + let mut new = pos; + match c { + 'U' => new.0 = if pos.0 > 0 { pos.0 - 1 } else { pos.0 }, + 'D' => new.0 = if pos.0 < 4 { pos.0 + 1 } else { pos.0 }, + 'L' => new.1 = if pos.1 > 0 { pos.1 - 1 } else { pos.1 }, + 'R' => new.1 = if pos.1 < 4 { pos.1 + 1 } else { pos.1 }, + _ => {}, // shouldn't happen + }; + // Ignore this `c' if it would take us off the diamond's edge + if keypad[new.0][new.1] != 0 { + pos = new; + } + } + + // Print as hexadecimal to follow puzzle description + result.push_str(&format!("{:X}", keypad[pos.0][pos.1])); + } + + result +} + +fn main() { + // Read directions from input text file + let input = fs::read_to_string("input.txt").unwrap(); + let lines = input.lines().collect(); + + // Part 1 gives 74921 for me + println!("Part 1 solution: {}", solve_part1(&lines)); + + // Part 2 gives A6B35 for me + println!("Part 2 solution: {}", solve_part2(&lines)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_example1() { + let lines = vec!["ULL", "RRDDD", "LURDL", "UUUUD"]; + assert_eq!(solve_part1(&lines), "1985"); + } + + #[test] + fn part2_example1() { + let lines = vec!["ULL", "RRDDD", "LURDL", "UUUUD"]; + assert_eq!(solve_part2(&lines), "5DB3"); + } +} diff --git a/d03/Cargo.toml b/d03/Cargo.toml new file mode 100644 index 0000000..5bff35d --- /dev/null +++ b/d03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "d03" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/d03/input.txt b/d03/input.txt new file mode 100644 index 0000000..b8e5272 --- /dev/null +++ b/d03/input.txt @@ -0,0 +1,1911 @@ + 541 588 421 + 827 272 126 + 660 514 367 + 39 703 839 + 229 871 3 + 237 956 841 + 898 566 112 + 101 79 112 + 813 541 146 + 603 135 565 + 335 363 180 + 382 493 669 + 545 81 843 + 657 960 849 + 823 909 9 + 370 194 908 + 101 750 317 + 295 671 968 + 656 431 64 + 634 291 245 + 819 663 266 + 504 662 829 + 760 590 918 + 733 859 258 + 197 651 137 + 357 799 638 + 544 250 597 + 604 654 431 + 90 870 184 + 609 535 604 + 313 777 650 + 421 320 741 + 278 967 430 + 299 722 23 + 362 949 973 + 353 258 989 + 992 607 461 + 850 264 490 + 251 351 569 + 810 320 116 + 959 335 611 + 450 242 624 + 849 26 376 + 419 42 222 + 995 14 291 + 711 174 615 + 17 982 961 + 722 895 941 + 920 161 480 + 905 246 531 + 794 190 144 + 317 318 633 + 145 233 150 + 445 534 519 + 935 592 863 + 784 260 844 + 255 704 570 + 521 57 61 + 490 149 110 + 163 144 142 + 771 289 60 + 914 233 541 + 271 93 542 + 411 640 812 + 370 968 65 + 61 960 781 + 333 79 421 + 269 190 428 + 477 225 96 + 610 641 265 + 650 135 461 + 145 759 380 + 758 451 682 + 524 125 861 + 760 555 251 + 360 219 567 + 456 285 726 + 510 98 927 + 886 336 228 + 356 916 323 + 685 974 165 + 253 532 207 + 868 990 548 + 811 803 525 + 205 624 894 + 707 512 328 + 835 288 657 + 496 776 413 + 484 185 10 + 33 832 403 + 896 678 789 + 745 595 773 + 287 201 669 + 150 189 513 + 884 42 131 + 934 214 407 + 239 819 149 + 145 582 334 + 375 307 467 + 680 59 850 + 873 441 973 + 243 393 256 + 509 237 265 + 302 550 334 + 728 524 291 + 130 986 333 + 60 926 390 + 133 653 692 + 428 755 491 + 24 476 659 + 446 290 823 + 906 703 410 + 950 183 391 + 199 735 250 + 734 908 254 + 635 373 34 + 489 567 275 + 587 746 463 + 512 508 367 + 104 971 568 + 647 857 191 + 605 20 775 + 118 853 634 + 432 559 322 + 887 433 134 + 660 332 336 + 669 711 952 + 762 713 610 + 108 972 860 + 732 743 562 + 877 277 758 + 225 590 380 + 708 35 424 + 989 425 439 + 279 413 818 + 107 422 634 + 775 833 667 + 755 755 279 + 694 293 6 + 156 75 649 + 820 303 651 + 448 934 587 + 25 830 788 + 473 192 932 + 734 126 535 + 940 215 513 + 935 126 656 + 428 905 789 + 760 742 772 + 510 523 633 + 994 808 110 + 93 740 100 + 943 80 198 + 329 888 532 + 50 763 337 + 320 673 741 + 722 52 639 + 18 130 566 + 733 130 921 + 280 586 642 + 450 222 350 + 178 427 325 + 317 685 261 + 744 261 105 + 621 809 326 + 740 532 605 + 855 690 526 + 441 301 185 + 783 309 196 + 525 272 334 + 502 58 432 + 980 150 121 + 891 9 800 + 286 143 705 + 985 846 850 + 362 696 70 + 807 194 900 + 912 193 448 + 405 148 461 + 677 261 30 + 121 406 308 + 34 206 451 + 109 473 312 + 46 726 695 + 793 847 25 + 834 160 708 + 372 849 375 + 744 799 423 + 631 478 715 + 346 82 545 + 541 184 95 + 492 228 588 + 542 635 43 + 75 347 439 + 612 321 431 + 719 526 847 + 158 270 338 + 813 482 560 + 817 253 402 + 899 630 887 + 198 456 730 + 235 286 344 + 125 646 398 + 204 375 401 + 80 289 308 + 295 664 862 + 284 382 565 + 816 130 651 + 947 596 383 + 131 611 723 + 617 471 549 + 663 282 697 + 475 219 901 + 535 829 434 + 542 502 544 + 23 649 110 + 733 691 820 + 684 21 821 + 740 689 1 + 224 249 280 + 485 776 350 + 682 971 182 + 465 12 610 + 294 296 13 + 606 297 594 + 337 104 3 + 154 257 343 + 352 171 349 + 322 466 973 + 189 173 854 + 484 338 714 + 194 194 551 + 188 741 162 + 14 696 663 + 219 868 33 + 224 4 446 + 218 865 411 + 392 536 574 + 616 692 482 + 746 592 94 + 230 72 663 + 291 487 99 + 326 482 669 + 131 265 155 + 61 422 609 + 80 552 641 + 509 334 851 + 475 211 911 + 193 274 737 + 128 15 973 + 381 31 255 + 413 21 812 + 778 461 3 + 514 307 697 + 329 353 695 + 48 90 212 + 856 354 294 + 863 265 360 + 496 918 264 + 377 889 164 + 603 164 418 + 159 701 199 + 283 503 186 + 127 658 54 + 938 226 855 + 189 533 465 + 924 365 906 + 322 422 291 + 943 713 238 + 714 508 138 + 612 328 49 + 962 877 685 + 849 963 732 + 154 278 428 + 49 624 476 + 111 518 753 + 284 330 216 + 716 689 290 + 667 369 127 + 719 728 175 + 543 493 139 + 759 408 286 + 915 970 827 + 482 289 787 + 713 860 621 + 836 286 729 + 921 447 223 + 665 655 706 + 692 579 316 + 772 679 289 + 972 394 84 + 221 675 877 + 272 336 864 + 439 684 69 + 780 721 503 + 225 587 125 + 802 945 408 + 280 237 682 + 406 281 745 + 176 60 71 + 450 720 335 + 656 293 383 + 324 694 454 + 547 57 51 + 415 965 657 + 311 936 687 + 989 878 585 + 156 72 165 + 938 823 574 + 626 968 146 + 823 896 588 + 294 236 597 + 177 294 177 + 287 351 480 + 133 252 649 + 706 319 175 + 34 418 960 + 722 583 906 + 711 379 674 + 529 574 882 + 546 868 781 + 577 875 687 + 881 844 503 + 926 247 190 + 621 535 356 + 294 105 257 + 601 640 579 + 573 142 519 + 978 900 427 + 693 823 350 + 275 362 683 + 137 291 741 + 258 130 517 + 838 686 12 + 59 800 869 + 790 188 881 + 997 324 278 + 582 338 887 + 900 23 712 + 288 619 402 + 480 168 596 + 292 505 505 + 993 30 982 + 636 335 892 + 813 363 175 + 230 248 620 + 450 668 896 + 460 736 874 + 331 800 837 + 844 805 734 + 620 54 708 + 726 820 592 + 126 20 394 + 728 820 219 + 229 27 32 + 164 569 939 + 290 559 949 + 325 360 794 + 342 356 529 + 203 627 691 + 60 932 544 + 767 865 563 + 770 316 525 + 99 756 674 + 165 783 180 + 72 27 846 + 973 419 905 + 104 364 294 + 896 271 613 + 294 222 416 + 414 609 858 + 481 429 478 + 308 544 615 + 624 139 560 + 574 626 840 + 998 338 328 + 403 739 211 + 625 472 120 + 808 190 64 + 5 202 558 + 800 42 607 + 874 801 675 + 27 275 735 + 898 927 280 + 366 794 526 + 233 529 13 + 314 744 525 + 842 530 617 + 704 461 406 + 281 946 841 + 389 113 678 + 545 652 410 + 851 754 339 + 14 56 706 + 623 131 738 + 636 127 931 + 857 404 199 + 682 25 387 + 574 406 435 + 508 528 307 + 574 531 575 + 223 433 827 + 80 811 486 + 710 642 904 + 749 903 795 + 920 205 60 + 904 286 108 + 96 428 120 |