From 53f9a710e0589d8bdf0abe80f56e986eb042e31f Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Wed, 14 Dec 2022 11:46:33 +0100 Subject: [PATCH] Rust Day 14 - I know I skipped 13 --- rust/Cargo.lock | 2 +- rust/Cargo.toml | 6 +- rust/src/day14/input.txt | 155 ++++++++++++++++++++++++++++ rust/src/day14/main.rs | 172 ++++++++++++++++++++++++++++++++ rust/src/day14/part_1.rs | 25 +++++ rust/src/day14/part_2.rs | 26 +++++ rust/src/day14/sample_input.txt | 2 + 7 files changed, 386 insertions(+), 2 deletions(-) create mode 100644 rust/src/day14/input.txt create mode 100644 rust/src/day14/main.rs create mode 100644 rust/src/day14/part_1.rs create mode 100644 rust/src/day14/part_2.rs create mode 100644 rust/src/day14/sample_input.txt diff --git a/rust/Cargo.lock b/rust/Cargo.lock index f661578..53de0e2 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "advent-of-code" -version = "22.12.2" +version = "22.14.2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index f6aeb83..204d4d3 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-of-code" -version = "22.12.2" +version = "22.14.2" edition = "2021" resolver = "2" @@ -71,6 +71,10 @@ path = "src/day11/main.rs" name = "day12" path = "src/day12/main.rs" +[[bin]] +name = "day14" +path = "src/day14/main.rs" + [dependencies] # Required for one of the day06 solutions (commented out) # phf = { version = "0.11.1", features = ["macros"] } \ No newline at end of file diff --git a/rust/src/day14/input.txt b/rust/src/day14/input.txt new file mode 100644 index 0000000..8766a47 --- /dev/null +++ b/rust/src/day14/input.txt @@ -0,0 +1,155 @@ +499,65 -> 499,68 -> 491,68 -> 491,74 -> 508,74 -> 508,68 -> 504,68 -> 504,65 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +485,24 -> 485,25 -> 496,25 -> 496,24 +485,24 -> 485,25 -> 496,25 -> 496,24 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +519,149 -> 524,149 +536,105 -> 536,109 -> 533,109 -> 533,115 -> 545,115 -> 545,109 -> 540,109 -> 540,105 +494,16 -> 499,16 +536,105 -> 536,109 -> 533,109 -> 533,115 -> 545,115 -> 545,109 -> 540,109 -> 540,105 +499,65 -> 499,68 -> 491,68 -> 491,74 -> 508,74 -> 508,68 -> 504,68 -> 504,65 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +504,83 -> 508,83 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +516,83 -> 520,83 +514,88 -> 514,89 -> 530,89 +520,155 -> 525,155 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +522,140 -> 527,140 +536,105 -> 536,109 -> 533,109 -> 533,115 -> 545,115 -> 545,109 -> 540,109 -> 540,105 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +502,22 -> 507,22 +494,56 -> 494,59 -> 489,59 -> 489,62 -> 501,62 -> 501,59 -> 500,59 -> 500,56 +525,137 -> 530,137 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +523,152 -> 528,152 +529,128 -> 529,123 -> 529,128 -> 531,128 -> 531,123 -> 531,128 -> 533,128 -> 533,123 -> 533,128 +507,77 -> 511,77 +494,56 -> 494,59 -> 489,59 -> 489,62 -> 501,62 -> 501,59 -> 500,59 -> 500,56 +536,140 -> 541,140 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +543,140 -> 548,140 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +492,28 -> 492,29 -> 509,29 +485,45 -> 485,47 -> 480,47 -> 480,53 -> 496,53 -> 496,47 -> 489,47 -> 489,45 +518,161 -> 527,161 -> 527,160 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +499,65 -> 499,68 -> 491,68 -> 491,74 -> 508,74 -> 508,68 -> 504,68 -> 504,65 +529,128 -> 529,123 -> 529,128 -> 531,128 -> 531,123 -> 531,128 -> 533,128 -> 533,123 -> 533,128 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +536,105 -> 536,109 -> 533,109 -> 533,115 -> 545,115 -> 545,109 -> 540,109 -> 540,105 +518,161 -> 527,161 -> 527,160 +531,131 -> 536,131 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +501,85 -> 505,85 +497,13 -> 502,13 +529,128 -> 529,123 -> 529,128 -> 531,128 -> 531,123 -> 531,128 -> 533,128 -> 533,123 -> 533,128 +507,85 -> 511,85 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +513,155 -> 518,155 +485,45 -> 485,47 -> 480,47 -> 480,53 -> 496,53 -> 496,47 -> 489,47 -> 489,45 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +532,137 -> 537,137 +485,45 -> 485,47 -> 480,47 -> 480,53 -> 496,53 -> 496,47 -> 489,47 -> 489,45 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +485,24 -> 485,25 -> 496,25 -> 496,24 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +495,22 -> 500,22 +529,128 -> 529,123 -> 529,128 -> 531,128 -> 531,123 -> 531,128 -> 533,128 -> 533,123 -> 533,128 +516,152 -> 521,152 +529,128 -> 529,123 -> 529,128 -> 531,128 -> 531,123 -> 531,128 -> 533,128 -> 533,123 -> 533,128 +499,65 -> 499,68 -> 491,68 -> 491,74 -> 508,74 -> 508,68 -> 504,68 -> 504,65 +529,128 -> 529,123 -> 529,128 -> 531,128 -> 531,123 -> 531,128 -> 533,128 -> 533,123 -> 533,128 +529,128 -> 529,123 -> 529,128 -> 531,128 -> 531,123 -> 531,128 -> 533,128 -> 533,123 -> 533,128 +513,81 -> 517,81 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +536,105 -> 536,109 -> 533,109 -> 533,115 -> 545,115 -> 545,109 -> 540,109 -> 540,105 +485,45 -> 485,47 -> 480,47 -> 480,53 -> 496,53 -> 496,47 -> 489,47 -> 489,45 +529,128 -> 529,123 -> 529,128 -> 531,128 -> 531,123 -> 531,128 -> 533,128 -> 533,123 -> 533,128 +530,152 -> 535,152 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +505,19 -> 510,19 +494,56 -> 494,59 -> 489,59 -> 489,62 -> 501,62 -> 501,59 -> 500,59 -> 500,56 +491,19 -> 496,19 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +499,65 -> 499,68 -> 491,68 -> 491,74 -> 508,74 -> 508,68 -> 504,68 -> 504,65 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +522,146 -> 527,146 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +499,65 -> 499,68 -> 491,68 -> 491,74 -> 508,74 -> 508,68 -> 504,68 -> 504,65 +533,149 -> 538,149 +541,155 -> 546,155 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +488,22 -> 493,22 +539,137 -> 544,137 +494,56 -> 494,59 -> 489,59 -> 489,62 -> 501,62 -> 501,59 -> 500,59 -> 500,56 +510,83 -> 514,83 +528,134 -> 533,134 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +537,152 -> 542,152 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +495,85 -> 499,85 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +501,16 -> 506,16 +494,56 -> 494,59 -> 489,59 -> 489,62 -> 501,62 -> 501,59 -> 500,59 -> 500,56 +514,88 -> 514,89 -> 530,89 +485,45 -> 485,47 -> 480,47 -> 480,53 -> 496,53 -> 496,47 -> 489,47 -> 489,45 +504,79 -> 508,79 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +525,143 -> 530,143 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +529,146 -> 534,146 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +494,56 -> 494,59 -> 489,59 -> 489,62 -> 501,62 -> 501,59 -> 500,59 -> 500,56 +526,149 -> 531,149 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +527,155 -> 532,155 +507,81 -> 511,81 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +492,28 -> 492,29 -> 509,29 +535,134 -> 540,134 +499,65 -> 499,68 -> 491,68 -> 491,74 -> 508,74 -> 508,68 -> 504,68 -> 504,65 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +536,105 -> 536,109 -> 533,109 -> 533,115 -> 545,115 -> 545,109 -> 540,109 -> 540,105 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +536,105 -> 536,109 -> 533,109 -> 533,115 -> 545,115 -> 545,109 -> 540,109 -> 540,105 +485,45 -> 485,47 -> 480,47 -> 480,53 -> 496,53 -> 496,47 -> 489,47 -> 489,45 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +485,45 -> 485,47 -> 480,47 -> 480,53 -> 496,53 -> 496,47 -> 489,47 -> 489,45 +510,79 -> 514,79 +529,140 -> 534,140 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +498,83 -> 502,83 +494,56 -> 494,59 -> 489,59 -> 489,62 -> 501,62 -> 501,59 -> 500,59 -> 500,56 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +519,85 -> 523,85 +498,19 -> 503,19 +501,81 -> 505,81 +513,85 -> 517,85 +509,22 -> 514,22 +523,102 -> 523,92 -> 523,102 -> 525,102 -> 525,99 -> 525,102 -> 527,102 -> 527,95 -> 527,102 -> 529,102 -> 529,101 -> 529,102 -> 531,102 -> 531,101 -> 531,102 -> 533,102 -> 533,99 -> 533,102 -> 535,102 -> 535,94 -> 535,102 -> 537,102 -> 537,92 -> 537,102 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +509,174 -> 509,170 -> 509,174 -> 511,174 -> 511,169 -> 511,174 -> 513,174 -> 513,169 -> 513,174 -> 515,174 -> 515,167 -> 515,174 -> 517,174 -> 517,166 -> 517,174 -> 519,174 -> 519,173 -> 519,174 -> 521,174 -> 521,164 -> 521,174 -> 523,174 -> 523,172 -> 523,174 +479,42 -> 479,33 -> 479,42 -> 481,42 -> 481,32 -> 481,42 -> 483,42 -> 483,34 -> 483,42 -> 485,42 -> 485,36 -> 485,42 -> 487,42 -> 487,33 -> 487,42 +534,155 -> 539,155 \ No newline at end of file diff --git a/rust/src/day14/main.rs b/rust/src/day14/main.rs new file mode 100644 index 0000000..0cf1ac8 --- /dev/null +++ b/rust/src/day14/main.rs @@ -0,0 +1,172 @@ +#![feature(let_chains)] +#![feature(map_try_insert)] +#![feature(generators, generator_trait)] +const INPUT: &str = include_str!("input.txt"); + +use std::collections::HashMap; + +mod part_1; +use part_1::part_1; +mod part_2; +use part_2::part_2; + +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +enum Material { + Stone, + Sand, +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub(crate) struct Cave { + pub(crate) field: HashMap<(usize, usize), Material>, +} + +impl Cave { + fn lowest_stone(&self) -> usize { + self.field + .iter() + .filter_map(|(position, material)| { + if matches!(material, Material::Stone) { + Some(position.1) + } else { + None + } + }) + .max() + .unwrap_or_default() + } + + /// Inserts a field of sand into this [`Cave`], then returns whether it landed. + pub(crate) fn insert_sand_void(&mut self) -> Option<(usize, usize)> { + let lowest_position = self.lowest_stone(); + + let mut current_coordinate = (X, 0); + + loop { + if current_coordinate.1 >= lowest_position { + return None; + } + + let coordinate_below_straight = (current_coordinate.0, current_coordinate.1 + 1); + let field_below_straight = self.field.get(&coordinate_below_straight); + if field_below_straight.is_none() { + current_coordinate = coordinate_below_straight; + continue; + } + + let coordinate_below_left = (current_coordinate.0 - 1, current_coordinate.1 + 1); + let field_below_left = self.field.get(&coordinate_below_left); + if field_below_left.is_none() { + current_coordinate = coordinate_below_left; + continue; + } + + let coordinate_below_right = (current_coordinate.0 + 1, current_coordinate.1 + 1); + let field_below_right = self.field.get(&coordinate_below_right); + if field_below_right.is_none() { + current_coordinate = coordinate_below_right; + continue; + } + + self.field.insert(current_coordinate, Material::Sand); + return Some(current_coordinate); + } + } + + /// Inserts a field of sand into this [`Cave`], then returns where it landed. + pub(crate) fn insert_sand_floor(&mut self) -> (usize, usize) { + let lowest_position = self.lowest_stone() + 2; + + let mut current_coordinate = (X, 0); + + loop { + let y_below = current_coordinate.1 + 1; + if y_below < lowest_position { + let coordinate_below_straight = (current_coordinate.0, y_below); + let field_below_straight = self.field.get(&coordinate_below_straight); + if field_below_straight.is_none() { + current_coordinate = coordinate_below_straight; + continue; + } + + let coordinate_below_left = (current_coordinate.0 - 1, y_below); + let field_below_left = self.field.get(&coordinate_below_left); + if field_below_left.is_none() { + current_coordinate = coordinate_below_left; + continue; + } + + let coordinate_below_right = (current_coordinate.0 + 1, y_below); + let field_below_right = self.field.get(&coordinate_below_right); + if field_below_right.is_none() { + current_coordinate = coordinate_below_right; + continue; + } + + self.field + .try_insert(current_coordinate, Material::Sand) + .unwrap_or_else(|err| { + panic!("Tried to insert Sand at occupied position {current_coordinate:?} - {err}") + }); + return current_coordinate; + } else { + self.field + .try_insert(current_coordinate, Material::Sand) + .unwrap_or_else(|err| { + panic!("Tried to insert Sand at occupied position {current_coordinate:?} - {err}") + }); + return current_coordinate; + } + } + } +} + +pub(crate) type Input = Cave; + +pub(crate) fn interpolate( + from: (usize, usize), + to: (usize, usize), +) -> impl Iterator { + let min_x = from.0.min(to.0); + let max_x = from.0.max(to.0); + let min_y = from.1.min(to.1); + let max_y = from.1.max(to.1); + (min_x..=max_x).flat_map(move |x| (min_y..=max_y).map(move |y| (x, y))) +} + +fn parse_input(input: &'static str) -> Input { + let result = input.lines().map(|line| { + line.split(" -> ").map(|position_string| { + let coordinates = position_string.split(',').collect::>(); + assert_eq!(coordinates.len(), 2); + + ( + coordinates[0].parse::().unwrap(), + coordinates[1].parse::().unwrap(), + ) + }) + }); + + let mut stone_tiles = HashMap::new(); + + for lines in result { + let mut coordinates = lines; + let mut current_point = coordinates.by_ref().next().unwrap(); + + for destination in coordinates { + for coordinate in interpolate(current_point, destination) { + stone_tiles.insert(coordinate, Material::Stone); + } + + current_point = destination; + } + } + + Cave { field: stone_tiles } +} + +pub fn main() { + let input = parse_input(INPUT); + part_1(input.clone()); + part_2(input); +} diff --git a/rust/src/day14/part_1.rs b/rust/src/day14/part_1.rs new file mode 100644 index 0000000..2844dfb --- /dev/null +++ b/rust/src/day14/part_1.rs @@ -0,0 +1,25 @@ +pub(crate) fn part_1(mut input: crate::Input) -> u64 { + let mut sand_count = 0; + while let Some(_inserted_at) = input.insert_sand_void::<500>() { + sand_count += 1; + } + + println!("Part 1: {sand_count}"); + + sand_count +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_solution() { + assert_eq!(super::part_1(crate::parse_input(crate::INPUT)), 0); + } + + #[test] + fn test_with_sample_solution() { + assert_eq!(super::part_1(crate::parse_input(SAMPLE_INPUT)), 24); + } +} diff --git a/rust/src/day14/part_2.rs b/rust/src/day14/part_2.rs new file mode 100644 index 0000000..0c2e539 --- /dev/null +++ b/rust/src/day14/part_2.rs @@ -0,0 +1,26 @@ +pub(crate) fn part_2(mut input: crate::Input) -> u64 { + // Increase by 1 because I break as soon as (500, 0) is placed, so it doesn't get counted + let mut sand_count = 1; + while let inserted_at = input.insert_sand_floor::<500>() && inserted_at.1 != 0 { + sand_count += 1; + } + + println!("Part 2: {sand_count}"); + + sand_count +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_solution() { + assert_eq!(super::part_2(crate::parse_input(crate::INPUT)), 29044); + } + + #[test] + fn test_with_sample_solution() { + assert_eq!(super::part_2(crate::parse_input(SAMPLE_INPUT)), 93); + } +} diff --git a/rust/src/day14/sample_input.txt b/rust/src/day14/sample_input.txt new file mode 100644 index 0000000..1926028 --- /dev/null +++ b/rust/src/day14/sample_input.txt @@ -0,0 +1,2 @@ +498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9 \ No newline at end of file