From a74896086ffa6486c71e527da2e29de7d7a8dccd Mon Sep 17 00:00:00 2001 From: Rob Tsuk Date: Wed, 14 Dec 2022 09:38:37 -0800 Subject: [PATCH] More work on day 14 --- Cargo.toml | 7 ++ data/day14.txt | 148 ++++++++++++++++++++++++++++++++ src/bin/day14.rs | 218 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 366 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 079daa3..21b9e2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,13 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +env_logger = "0.10.0" euclid = { version = "0.22.7", features = ["serde"] } +log = "0.4.17" nom = "7.1.1" pathfinding = "4.0.0" +pixels = "0.10.0" +structopt = "0.3.26" +tiny-skia = "0.8.2" +winit = "0.27.5" +winit_input_helper = "0.13.0" diff --git a/data/day14.txt b/data/day14.txt index e69de29..8e9547c 100644 --- a/data/day14.txt +++ b/data/day14.txt @@ -0,0 +1,148 @@ +511,54 -> 511,56 -> 504,56 -> 504,61 -> 520,61 -> 520,56 -> 515,56 -> 515,54 +516,113 -> 521,113 +511,101 -> 511,103 -> 504,103 -> 504,106 -> 517,106 -> 517,103 -> 513,103 -> 513,101 +529,82 -> 533,82 +541,157 -> 545,157 +489,20 -> 489,24 -> 483,24 -> 483,32 -> 495,32 -> 495,24 -> 492,24 -> 492,20 +512,111 -> 517,111 +538,154 -> 542,154 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +523,76 -> 527,76 +511,101 -> 511,103 -> 504,103 -> 504,106 -> 517,106 -> 517,103 -> 513,103 -> 513,101 +511,82 -> 515,82 +514,79 -> 518,79 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +489,20 -> 489,24 -> 483,24 -> 483,32 -> 495,32 -> 495,24 -> 492,24 -> 492,20 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +514,85 -> 518,85 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +532,134 -> 532,136 -> 530,136 -> 530,142 -> 542,142 -> 542,136 -> 536,136 -> 536,134 +532,85 -> 536,85 +511,101 -> 511,103 -> 504,103 -> 504,106 -> 517,106 -> 517,103 -> 513,103 -> 513,101 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +511,101 -> 511,103 -> 504,103 -> 504,106 -> 517,106 -> 517,103 -> 513,103 -> 513,101 +535,151 -> 539,151 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +526,85 -> 530,85 +497,50 -> 497,51 -> 512,51 +532,134 -> 532,136 -> 530,136 -> 530,142 -> 542,142 -> 542,136 -> 536,136 -> 536,134 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +509,113 -> 514,113 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +538,148 -> 542,148 +492,17 -> 496,17 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +511,54 -> 511,56 -> 504,56 -> 504,61 -> 520,61 -> 520,56 -> 515,56 -> 515,54 +541,145 -> 545,145 +497,50 -> 497,51 -> 512,51 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +550,154 -> 554,154 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +520,79 -> 524,79 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +517,63 -> 517,64 -> 524,64 -> 524,63 +489,20 -> 489,24 -> 483,24 -> 483,32 -> 495,32 -> 495,24 -> 492,24 -> 492,20 +532,154 -> 536,154 +489,20 -> 489,24 -> 483,24 -> 483,32 -> 495,32 -> 495,24 -> 492,24 -> 492,20 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +521,117 -> 521,118 -> 531,118 -> 531,117 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +532,134 -> 532,136 -> 530,136 -> 530,142 -> 542,142 -> 542,136 -> 536,136 -> 536,134 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +547,157 -> 551,157 +532,134 -> 532,136 -> 530,136 -> 530,142 -> 542,142 -> 542,136 -> 536,136 -> 536,134 +521,117 -> 521,118 -> 531,118 -> 531,117 +517,63 -> 517,64 -> 524,64 -> 524,63 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +523,82 -> 527,82 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +495,15 -> 499,15 +553,157 -> 557,157 +511,101 -> 511,103 -> 504,103 -> 504,106 -> 517,106 -> 517,103 -> 513,103 -> 513,101 +498,17 -> 502,17 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +532,134 -> 532,136 -> 530,136 -> 530,142 -> 542,142 -> 542,136 -> 536,136 -> 536,134 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +547,151 -> 551,151 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +511,101 -> 511,103 -> 504,103 -> 504,106 -> 517,106 -> 517,103 -> 513,103 -> 513,101 +504,17 -> 508,17 +544,148 -> 548,148 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +511,54 -> 511,56 -> 504,56 -> 504,61 -> 520,61 -> 520,56 -> 515,56 -> 515,54 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +523,113 -> 528,113 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +535,157 -> 539,157 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +498,13 -> 502,13 +532,134 -> 532,136 -> 530,136 -> 530,142 -> 542,142 -> 542,136 -> 536,136 -> 536,134 +526,79 -> 530,79 +511,54 -> 511,56 -> 504,56 -> 504,61 -> 520,61 -> 520,56 -> 515,56 -> 515,54 +511,54 -> 511,56 -> 504,56 -> 504,61 -> 520,61 -> 520,56 -> 515,56 -> 515,54 +521,117 -> 521,118 -> 531,118 -> 531,117 +541,151 -> 545,151 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +532,134 -> 532,136 -> 530,136 -> 530,142 -> 542,142 -> 542,136 -> 536,136 -> 536,134 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +489,20 -> 489,24 -> 483,24 -> 483,32 -> 495,32 -> 495,24 -> 492,24 -> 492,20 +519,111 -> 524,111 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +529,157 -> 533,157 +511,101 -> 511,103 -> 504,103 -> 504,106 -> 517,106 -> 517,103 -> 513,103 -> 513,101 +501,15 -> 505,15 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +489,20 -> 489,24 -> 483,24 -> 483,32 -> 495,32 -> 495,24 -> 492,24 -> 492,20 +508,85 -> 512,85 +520,85 -> 524,85 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +517,76 -> 521,76 +517,82 -> 521,82 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +523,69 -> 523,70 -> 530,70 +520,73 -> 524,73 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +544,154 -> 548,154 +511,54 -> 511,56 -> 504,56 -> 504,61 -> 520,61 -> 520,56 -> 515,56 -> 515,54 +511,54 -> 511,56 -> 504,56 -> 504,61 -> 520,61 -> 520,56 -> 515,56 -> 515,54 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +515,109 -> 520,109 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +517,63 -> 517,64 -> 524,64 -> 524,63 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 +523,69 -> 523,70 -> 530,70 +528,131 -> 528,121 -> 528,131 -> 530,131 -> 530,127 -> 530,131 -> 532,131 -> 532,124 -> 532,131 -> 534,131 -> 534,126 -> 534,131 +501,98 -> 501,93 -> 501,98 -> 503,98 -> 503,97 -> 503,98 -> 505,98 -> 505,90 -> 505,98 -> 507,98 -> 507,92 -> 507,98 -> 509,98 -> 509,94 -> 509,98 -> 511,98 -> 511,93 -> 511,98 +489,45 -> 489,38 -> 489,45 -> 491,45 -> 491,38 -> 491,45 -> 493,45 -> 493,40 -> 493,45 -> 495,45 -> 495,39 -> 495,45 -> 497,45 -> 497,43 -> 497,45 -> 499,45 -> 499,43 -> 499,45 -> 501,45 -> 501,36 -> 501,45 +489,20 -> 489,24 -> 483,24 -> 483,32 -> 495,32 -> 495,24 -> 492,24 -> 492,20 +521,170 -> 521,163 -> 521,170 -> 523,170 -> 523,160 -> 523,170 -> 525,170 -> 525,165 -> 525,170 -> 527,170 -> 527,168 -> 527,170 -> 529,170 -> 529,169 -> 529,170 -> 531,170 -> 531,169 -> 531,170 -> 533,170 -> 533,160 -> 533,170 \ No newline at end of file diff --git a/src/bin/day14.rs b/src/bin/day14.rs index ea635f9..bbcb6d9 100644 --- a/src/bin/day14.rs +++ b/src/bin/day14.rs @@ -1,20 +1,224 @@ +#![allow(unused)] + +use euclid::{point2, rect}; +use log::error; +use pixels::{Error, Pixels, SurfaceTexture}; +use std::collections::HashSet; +use structopt::StructOpt; +use tiny_skia::{Color, FillRule, Paint, PathBuilder, Pixmap, Stroke, Transform}; +use winit::dpi::LogicalSize; +use winit::event::{Event, VirtualKeyCode}; +use winit::event_loop::{ControlFlow, EventLoop}; +use winit::window::WindowBuilder; +use winit_input_helper::WinitInputHelper; + const DATA: &str = include_str!("../../data/day14.txt"); +const SAMPLE: &str = r#"498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9"#; + +type Point = euclid::default::Point2D; +type Vector = euclid::default::Vector2D; +type Rect = euclid::default::Rect; +type Box = euclid::default::Box2D; +type RockList = Vec>; + +struct LineIter { + current: Point, + end: Point, + delta: Vector, +} + +impl LineIter { + fn new(start: Point, end: Point) -> Self { + let b = Box::from_points(&[start, end]); + let start = b.min; + let end = b.max; + let mut delta = (end - start); + if delta.x > 0 { + delta.x /= delta.x; + } + if delta.y > 0 { + delta.y /= delta.y; + } + Self { + current: start, + delta, + end: end, + } + } +} + +impl Iterator for LineIter { + type Item = Point; + + fn next(&mut self) -> Option { + if self.current.x > self.end.x || self.current.y > self.end.y { + return None; + } + let next = self.current; + self.current += self.delta; + Some(next) + } +} + +#[derive(Debug)] +struct RockFall { + bounds: Rect, + blocks: HashSet, +} + +impl RockFall { + fn new(list: RockList) -> Self { + let mut bounds = Rect::from_points(list.iter().flatten()); + let mut blocks = HashSet::new(); + for rock in list { + for i in 0..rock.len() - 1 { + let iter = LineIter::new(rock[i], rock[i + 1]); + blocks.extend(iter); + } + } + Self { bounds, blocks } + } + + fn render(&mut self, pixmap: &mut Pixmap) { + let delta = 1.0; + let mut paint1 = Paint::default(); + paint1.set_color_rgba8(50, 107, 160, 255); + paint1.anti_alias = true; + let mut stroke = Stroke::default(); + let identity = Transform::from_scale(5.0, 5.0).post_translate(25.0, 25.0); -fn parse(s: &str) { + for block in self.blocks.iter() { + let p = *block - self.bounds.origin; + let r = tiny_skia::Rect::from_xywh(p.x as f32, p.y as f32, 1.0, 1.0).unwrap(); + let path1 = PathBuilder::from_rect(r); + pixmap.fill_path(&path1, &paint1, FillRule::Winding, identity, None); + } + } } -fn main() { - let _ = parse(DATA); +fn parse_point(s: &str) -> Point { + let mut parts = s + .split(',') + .map(str::parse::) + .map(Result::ok) + .map(Option::unwrap_or_default); + + point2( + parts.next().unwrap_or_default(), + parts.next().unwrap_or_default(), + ) +} + +fn parse(s: &str) -> RockList { + s.lines() + .map(|s| s.split(" -> ").map(parse_point).collect::>()) + .collect() +} + +const WIDTH: u32 = 1000; +const HEIGHT: u32 = 1000; + +#[derive(Debug, StructOpt)] +#[structopt(name = "day14", about = "Falling sand.")] +struct Opt { + /// Use puzzle input instead of the sample + #[structopt(short, long)] + puzzle_input: bool, +} + +fn main() -> Result<(), Error> { + env_logger::init(); + + let opt = Opt::from_args(); + + let rocklist = parse(if !opt.puzzle_input { SAMPLE } else { DATA }); + + let mut rockfall = RockFall::new(rocklist); + + let event_loop = EventLoop::new(); + let mut input = WinitInputHelper::new(); + let window = { + let size = LogicalSize::new(WIDTH as f64, HEIGHT as f64); + WindowBuilder::new() + .with_title("Day 14") + .with_inner_size(size) + .with_min_inner_size(size) + .build(&event_loop) + .unwrap() + }; + + let mut pixels = { + let window_size = window.inner_size(); + let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window); + Pixels::new(WIDTH, HEIGHT, surface_texture)? + }; + + let mut drawing = Pixmap::new(1000, 1000).unwrap(); + + event_loop.run(move |event, _, control_flow| { + rockfall.render(&mut drawing); + + if let Event::RedrawRequested(_) = event { + pixels.get_frame_mut().copy_from_slice(drawing.data()); + if pixels + .render() + .map_err(|e| error!("pixels.render() failed: {}", e)) + .is_err() + { + *control_flow = ControlFlow::Exit; + return; + } + } + + // Handle input events + if input.update(&event) { + // Close events + if input.key_pressed(VirtualKeyCode::Escape) || input.quit() { + *control_flow = ControlFlow::Exit; + return; + } + + // Resize the window + if let Some(size) = input.window_resized() { + pixels.resize_surface(size.width, size.height); + } + + window.request_redraw(); + } + }); } #[cfg(test)] mod test { use super::*; - const SAMPLE: &str = r#"498,4 -> 498,6 -> 496,6 -503,4 -> 502,4 -> 502,9 -> 494,9"#; - #[test] fn test_parse() { - } + let l = parse(SAMPLE); + assert_eq!( + l, + vec![ + vec![point2(498, 4), point2(498, 6), point2(496, 6)], + vec![ + point2(503, 4), + point2(502, 4), + point2(502, 9), + point2(494, 9) + ] + ] + ); + + let rockfall = RockFall::new(l); + assert_eq!(rockfall.bounds, rect(494, 4, 9, 5)); + } + + #[test] + fn test_line_iter() { + let points: Vec<_> = LineIter::new(point2(498, 4), point2(498, 6)).collect(); + dbg!(&points); + assert_eq!(points, [point2(498, 4,), point2(498, 5,), point2(498, 6,)]); + let points: Vec<_> = LineIter::new(point2(498, 6), point2(496, 6)).collect(); + assert_eq!(points, [point2(496, 6,), point2(497, 6,), point2(498, 6,)]); + } }