From c82f5473ba1122589a5208b8fa6d76e0c18ef65b Mon Sep 17 00:00:00 2001 From: efim Date: Thu, 15 Dec 2022 17:00:01 +0000 Subject: [PATCH] day 14, falling sand part 1 --- day14-input.txt | 147 +++++++++++++++++++ day14-scratch.lisp | 352 +++++++++++++++++++++++++++++++++++++++++++++ day14-test.txt | 2 + day14.lisp | 142 ++++++++++++++++++ 4 files changed, 643 insertions(+) create mode 100644 day14-input.txt create mode 100644 day14-scratch.lisp create mode 100644 day14-test.txt create mode 100644 day14.lisp diff --git a/day14-input.txt b/day14-input.txt new file mode 100644 index 0000000..85a855c --- /dev/null +++ b/day14-input.txt @@ -0,0 +1,147 @@ +502,19 -> 507,19 +523,100 -> 523,104 -> 519,104 -> 519,111 -> 528,111 -> 528,104 -> 526,104 -> 526,100 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +507,117 -> 521,117 -> 521,116 +517,34 -> 522,34 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +510,75 -> 510,78 -> 505,78 -> 505,84 -> 518,84 -> 518,78 -> 515,78 -> 515,75 +503,34 -> 508,34 +501,15 -> 506,15 +523,136 -> 523,138 -> 518,138 -> 518,145 -> 535,145 -> 535,138 -> 528,138 -> 528,136 +513,113 -> 513,114 -> 527,114 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +523,136 -> 523,138 -> 518,138 -> 518,145 -> 535,145 -> 535,138 -> 528,138 -> 528,136 +530,150 -> 535,150 +526,133 -> 526,131 -> 526,133 -> 528,133 -> 528,129 -> 528,133 -> 530,133 -> 530,129 -> 530,133 +523,100 -> 523,104 -> 519,104 -> 519,111 -> 528,111 -> 528,104 -> 526,104 -> 526,100 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +510,34 -> 515,34 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +510,75 -> 510,78 -> 505,78 -> 505,84 -> 518,84 -> 518,78 -> 515,78 -> 515,75 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +524,34 -> 529,34 +498,46 -> 502,46 +510,46 -> 514,46 +509,19 -> 514,19 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +526,133 -> 526,131 -> 526,133 -> 528,133 -> 528,129 -> 528,133 -> 530,133 -> 530,129 -> 530,133 +497,13 -> 502,13 +504,46 -> 508,46 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +545,155 -> 545,157 -> 541,157 -> 541,160 -> 556,160 -> 556,157 -> 550,157 -> 550,155 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +510,75 -> 510,78 -> 505,78 -> 505,84 -> 518,84 -> 518,78 -> 515,78 -> 515,75 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +507,43 -> 511,43 +523,100 -> 523,104 -> 519,104 -> 519,111 -> 528,111 -> 528,104 -> 526,104 -> 526,100 +506,31 -> 511,31 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +510,75 -> 510,78 -> 505,78 -> 505,84 -> 518,84 -> 518,78 -> 515,78 -> 515,75 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +507,117 -> 521,117 -> 521,116 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +534,152 -> 539,152 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +495,19 -> 500,19 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +523,136 -> 523,138 -> 518,138 -> 518,145 -> 535,145 -> 535,138 -> 528,138 -> 528,136 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +527,152 -> 532,152 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +533,148 -> 538,148 +510,75 -> 510,78 -> 505,78 -> 505,84 -> 518,84 -> 518,78 -> 515,78 -> 515,75 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +516,46 -> 520,46 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +545,155 -> 545,157 -> 541,157 -> 541,160 -> 556,160 -> 556,157 -> 550,157 -> 550,155 +501,119 -> 501,120 -> 512,120 -> 512,119 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +512,25 -> 517,25 +526,133 -> 526,131 -> 526,133 -> 528,133 -> 528,129 -> 528,133 -> 530,133 -> 530,129 -> 530,133 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +513,31 -> 518,31 +526,133 -> 526,131 -> 526,133 -> 528,133 -> 528,129 -> 528,133 -> 530,133 -> 530,129 -> 530,133 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +520,31 -> 525,31 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +482,21 -> 482,22 -> 490,22 +510,75 -> 510,78 -> 505,78 -> 505,84 -> 518,84 -> 518,78 -> 515,78 -> 515,75 +498,17 -> 503,17 +523,136 -> 523,138 -> 518,138 -> 518,145 -> 535,145 -> 535,138 -> 528,138 -> 528,136 +545,155 -> 545,157 -> 541,157 -> 541,160 -> 556,160 -> 556,157 -> 550,157 -> 550,155 +513,43 -> 517,43 +523,100 -> 523,104 -> 519,104 -> 519,111 -> 528,111 -> 528,104 -> 526,104 -> 526,100 +505,17 -> 510,17 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +509,28 -> 514,28 +545,155 -> 545,157 -> 541,157 -> 541,160 -> 556,160 -> 556,157 -> 550,157 -> 550,155 +537,150 -> 542,150 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +501,43 -> 505,43 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +501,119 -> 501,120 -> 512,120 -> 512,119 +507,37 -> 511,37 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +523,100 -> 523,104 -> 519,104 -> 519,111 -> 528,111 -> 528,104 -> 526,104 -> 526,100 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +501,119 -> 501,120 -> 512,120 -> 512,119 +510,40 -> 514,40 +488,19 -> 493,19 +482,21 -> 482,22 -> 490,22 +494,15 -> 499,15 +523,136 -> 523,138 -> 518,138 -> 518,145 -> 535,145 -> 535,138 -> 528,138 -> 528,136 +526,133 -> 526,131 -> 526,133 -> 528,133 -> 528,129 -> 528,133 -> 530,133 -> 530,129 -> 530,133 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +526,133 -> 526,131 -> 526,133 -> 528,133 -> 528,129 -> 528,133 -> 530,133 -> 530,129 -> 530,133 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +545,155 -> 545,157 -> 541,157 -> 541,160 -> 556,160 -> 556,157 -> 550,157 -> 550,155 +513,97 -> 513,89 -> 513,97 -> 515,97 -> 515,94 -> 515,97 -> 517,97 -> 517,94 -> 517,97 -> 519,97 -> 519,93 -> 519,97 -> 521,97 -> 521,88 -> 521,97 -> 523,97 -> 523,94 -> 523,97 +523,136 -> 523,138 -> 518,138 -> 518,145 -> 535,145 -> 535,138 -> 528,138 -> 528,136 +504,40 -> 508,40 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +526,133 -> 526,131 -> 526,133 -> 528,133 -> 528,129 -> 528,133 -> 530,133 -> 530,129 -> 530,133 +523,100 -> 523,104 -> 519,104 -> 519,111 -> 528,111 -> 528,104 -> 526,104 -> 526,100 +545,155 -> 545,157 -> 541,157 -> 541,160 -> 556,160 -> 556,157 -> 550,157 -> 550,155 +516,28 -> 521,28 +545,155 -> 545,157 -> 541,157 -> 541,160 -> 556,160 -> 556,157 -> 550,157 -> 550,155 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +491,17 -> 496,17 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +526,133 -> 526,131 -> 526,133 -> 528,133 -> 528,129 -> 528,133 -> 530,133 -> 530,129 -> 530,133 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +497,72 -> 497,63 -> 497,72 -> 499,72 -> 499,65 -> 499,72 -> 501,72 -> 501,71 -> 501,72 -> 503,72 -> 503,67 -> 503,72 -> 505,72 -> 505,63 -> 505,72 -> 507,72 -> 507,71 -> 507,72 -> 509,72 -> 509,70 -> 509,72 -> 511,72 -> 511,69 -> 511,72 +523,100 -> 523,104 -> 519,104 -> 519,111 -> 528,111 -> 528,104 -> 526,104 -> 526,100 +513,113 -> 513,114 -> 527,114 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +510,75 -> 510,78 -> 505,78 -> 505,84 -> 518,84 -> 518,78 -> 515,78 -> 515,75 +488,59 -> 488,51 -> 488,59 -> 490,59 -> 490,52 -> 490,59 -> 492,59 -> 492,55 -> 492,59 -> 494,59 -> 494,52 -> 494,59 -> 496,59 -> 496,50 -> 496,59 -> 498,59 -> 498,58 -> 498,59 -> 500,59 -> 500,56 -> 500,59 -> 502,59 -> 502,56 -> 502,59 -> 504,59 -> 504,57 -> 504,59 +541,152 -> 546,152 +523,136 -> 523,138 -> 518,138 -> 518,145 -> 535,145 -> 535,138 -> 528,138 -> 528,136 diff --git a/day14-scratch.lisp b/day14-scratch.lisp new file mode 100644 index 0000000..7a17317 --- /dev/null +++ b/day14-scratch.lisp @@ -0,0 +1,352 @@ +;; https://adventofcode.com/2022/day/14 + +;; so, 2d space vertical and left-right. point from which sand falls down and lines (likely horizontal || vertical) that collide with falling sand +;; gathering input. i've only thought of doing initialization in two sweeps. +;; +;; one to get "leftmost" and "rightmost", top & bottom coords. +;; so that i'm putting it all into one class that has accessors that translate it to +;; ordinary 2d array +;; +;; sand is pouring from (500, 0) - (column, row) - from +;; columns : left -> right +;; rows : top to bottom +;; top is already 0 +;; +;; chars: #\. - empty space +;; #\# - stone +;; #\o - falling sand ; will not be actually stored I suppose +;; #\x - resting sand + +(defclass arena () + ((grid :initarg :grid :initform (error "supply value for :grid")) + (leftmost :initarg :left :accessor leftmost) + (bottommost :initarg :bottom :accessor bottommost) + (rightmost :initarg :right :accessor rightmost))) + +(defun make-arena (left right bottom) + (let ((cols-num (1+ (- right left))) + (rows-num (1+ bottom))) + (make-instance 'arena :left left :right right :bottom bottom + :grid (make-array (list rows-num cols-num) + :initial-element #\.)))) + +(make-array '(2 5) :initial-element "hello") +;; now. translation for coordinates, and in a way that would allow writing into place? + +;; can i just defmethod getf for nonexistent place? + +(defparameter *test-arena* (make-arena 100 110 7)) +(setf (aref (slot-value *test-arena* 'grid) 0 4) #\*) + +;; or just do through macros? nah, try functions first, it's just i liked idea of aref +;; (x y) -> (row col) in grid +(defun translate-coords (arena x y) + (list y (- x (leftmost arena)))) + +(destructuring-bind (rrow ccol) (translate-coords *test-arena* 104 2) + (list rrow ccol)) + +(defun get-place (arena x y) + (destructuring-bind (row col) (translate-coords arena x y) + (aref (slot-value arena 'grid) row col))) + +(get-place *test-arena* 104 0) + +(defun set-place (arena x y value) + (destructuring-bind (row col) (translate-coords arena x y) + (setf (aref (slot-value arena 'grid) row col) value))) + +(set-place *test-arena* 104 1 #\&) +(slot-value *test-arena* 'grid) + +;; ok. now funciton that would add lines from input? +(ql:quickload 'cl-ppcre) +(defun input-line-to-rock-coords (line) + (mapcar (lambda (coords) (mapcar #'parse-integer (cl-ppcre:split "," coords))) + (cl-ppcre:split " -> " line))) + +(input-line-to-rock-coords " 503,4 -> 502,4 -> 502,9 -> 494,9 ") + +;; now. i want to do first pass and find leftmost, rightmost and bottommost + +(defparameter *day14-input-file* "day14-test.txt") + +(mapcar #'input-line-to-rock-coords (uiop:read-file-lines *day14-input-file*)) +;; now find flatten list in Alexandria + +(ql:quickload 'alexandria) +(defparameter *test-input-flat-coords* + (alexandria:flatten (mapcar #'input-line-to-rock-coords (uiop:read-file-lines *day14-input-file*)))) +;; well, it fully flattens it by default. maybe there it's configurable? +;; using destructuring of pairs would be better +(loop + for (x y) on *test-input-flat-coords* + by #'cddr + do (format t "x:~a y:~a~%" x y)) + +(loop + for (x y) on *test-input-flat-coords* + by #'cddr + minimize x + do (print x)) + +(loop + for (x y) on *test-input-flat-coords* + by #'cddr + minimize y + do (print y)) + +(loop + for (x y) on *test-input-flat-coords* + by #'cddr + collect x into x-es + collect y into y-es + finally (return (list (apply #'min x-es) (apply #'min y-es)))) +;; this : I was forgetting :by #'cddr + +;; here's limits +(let ((flat-coords + (alexandria:flatten (mapcar #'input-line-to-rock-coords (uiop:read-file-lines *day14-input-file*)) + ))) + (loop + for (x y) on flat-coords + by #'cddr + minimize x into leftmost + maximize x into rightmost + maximize y into bottommost + finally (return (list leftmost rightmost bottommost)))) + +;; next - build arena + +;; building empty arena +(setq *test-arena* + (let ((flat-coords + (alexandria:flatten (mapcar #'input-line-to-rock-coords (uiop:read-file-lines *day14-input-file*))))) + (destructuring-bind (leftmost rightmost bottommost) + (loop + for (x y) on flat-coords + by #'cddr + minimize x into leftmost + maximize x into rightmost + maximize y into bottommost + finally (return (list leftmost rightmost bottommost))) + (make-arena leftmost rightmost bottommost)))) + +(slot-value *test-arena* 'grid) + +;; now adding rock lines from coord list +'(((498 4) (498 6) (496 6)) ((503 4) (502 4) (502 9) (494 9))) +;; and one line is +(defparameter *test-rock-line-coords* '((498 4) (498 6) (496 6))) + +(loop + for (x y) in *test-rock-line-coords* + do (format t "x:~a y:~a~%" x y)) +;; can i take two at a time? +;; well, maybe use DO +(do* ((coords-list *test-rock-line-coords* (cdr coords-list)) + (first-coords (first *test-rock-line-coords*) (first coords-list)) + (second-coords (second *test-rock-line-coords*) (second coords-list))) + ((not second-coords) "end") + (format t "~a -> ~a, in ~a~%" first-coords second-coords coords-list)) + +;; yup. +;; now in this DO i have "start point" -> "end point" +;; let's do separate function that + +(let ((start-x 10) + (start-y 1) + (end-x 25) + (end-y 1)) + (loop for x from start-x to end-x do + (loop for y from start-y to end-y do + (format t "(~a, ~a), " x y))) + (terpri)) + +(let ((start-x 100) + (start-y 2) + (end-x 100) + (end-y 11)) + (loop for x from start-x to end-x do + (loop for y from start-y to end-y do + (format t "(~a, ~a), " x y))) + (terpri)) + +;; that works + +(defun put-rock-line (arena start-x end-x start-y end-y) + (loop for x from start-x to end-x do + (progn + (loop for y from start-y to end-y + do (set-place arena x y #\#))))) +;; yas. +;; now do this for each pair of coords + +(put-rock-line *test-arena* 101 109 1 1) + + +(setq *test-arena* (make-arena 100 110 7)) +(slot-value *test-arena* 'grid) + +(get-place *test-arena* 101 1) + +(put-rock-line *test-arena* 101 109 1 1) +(set-place *test-arena* 101 1 #\#) +(set-place *test-arena* 102 1 #\#) + +;; copy over previous per-2-coords-loop: +(loop + for (x y) in *test-rock-line-coords* + do (format t "x:~a y:~a~%" x y)) +;; can i take two at a time? +;; well, maybe use DO +(do* ((coords-list *test-rock-line-coords* (cdr coords-list)) + (first-coords (first *test-rock-line-coords*) (first coords-list)) + (second-coords (second *test-rock-line-coords*) (second coords-list))) + ((not second-coords) "end") + (destructuring-bind ((start-x start-y) (end-x end-y)) + (list first-coords second-coords) + (put-rock-line *test-arena* start-x end-x start-y end-y) )) +;; yes + +(array-dimension (make-array '(3 10)) 1) + +(get-place *test-arena* 101 3) +(get-place *test-arena* 0 0) + +(slot-value *test-arena* 'grid) +;; oh, it doesn't work when numbers are in wrong order. +;; ugh the LOOP for i from 9 to 3 +;; how do i deal with that? + +(defun put-rock-line (arena start-x end-x start-y end-y) + (let ((start-x (min start-x end-x)) + (end-x (max start-x end-x)) + (start-y (min start-y end-y)) + (end-y (max start-y end-y))) + (loop for x from start-x to end-x do + (progn + (loop for y from start-y to end-y + do (set-place arena x y #\#)))))) + +(defun put-rock-chain (arena rock-coods-chain) + (do* ((coords-list rock-coods-chain (cdr coords-list)) + (first-coords (first rock-coods-chain) (first coords-list)) + (second-coords (second rock-coods-chain) (second coords-list))) + ((not second-coords) "end") + (destructuring-bind ((start-x start-y) (end-x end-y)) + (list first-coords second-coords) + (put-rock-line arena start-x end-x start-y end-y) ))) + +(defun put-rock-lines (arena rock-coords-lines) + (loop + for rock-coord-line in rock-coords-lines + do (put-rock-chain arena rock-coord-line))) + +(defparameter *test-input-coords-chains* nil) +(setq *test-input-coords-chains* + (mapcar #'input-line-to-rock-coords + (uiop:read-file-lines *day14-input-file*))) + + +(slot-value *test-arena* 'grid) + +(put-rock-lines *test-arena* *test-input-coords-chains*) + +;; i think this works. +;; seems to be the complete initialization + +;; now for the sand simulation part. +;; again, envision this as lots of loops + +;; inner loop - one new sand +;; created at (500, 0), starts dropping +;; either until out-of-bounds +;; or settled by checks of lower-part + + +;; out-of-bounds is air +;; and . char is air +(defun is-point-air (x y arena) + (or (eq #\. (get-place arena x y)) + (not (get-place arena x y)))) +(is-point-air 498 4 *test-arena*) +(is-point-air 498 3 *test-arena*) +(is-point-air 500 0 *test-arena*) +(is-point-air 502 3 *test-arena*) +(is-point-air 502 4 *test-arena*) + +(defun sand-check-next-move (sand-x sand-y arena) + (let* ((next-y (1+ sand-y)) + (possible-next-steps (list (list sand-x next-y) + (list (1- sand-x) next-y) + (list (1+ sand-x) next-y)))) + (first (remove-if-not (lambda (coords) + (is-point-air (first coords) (second coords) arena)) + possible-next-steps)))) +;; let's return next step or nil if it rests + +(sand-check-next-move 502 3 *test-arena*) +(get-place *test-arena* 501 4) +(get-place *test-arena* 503 4) + +(sand-check-next-move 500 7 *test-arena*) +(sand-check-next-move 500 8 *test-arena*) +(get-place *test-arena* 501 4) +(get-place *test-arena* 503 4) + +(aref (slot-value *test-arena* 'grid) 0) +(array-storage-vector (slot-value *test-arena* 'grid)) + +(defun out-of-bounds (x y arena) + (not (get-place arena x y))) + +;; well, this seems to work +;; now the one grain loop +(let ((arena *test-arena*)) + (do* + ((prev-coords nil sand-coords) + (sand-coords '(500 0) (sand-check-next-move + (first sand-coords) (second sand-coords) arena))) + ((or (not sand-coords) ; sand rests + (out-of-bounds (first sand-coords) (second sand-coords) arena)) ; end condition - rest or out of bounds + (when (not sand-coords) + (set-place arena (first prev-coords) (second prev-coords) #\x))) + (format t "sc: ~a, prev: ~a~%" sand-coords prev-coords))) + +(init-arena) +(slot-value *test-arena* 'grid) +(not (get-place *test-arena* 500 0 )) +(set-place *test-arena* 500 0 #\x) + +(defun drop-sand-unit (arena) + (do* + ((prev-coords nil sand-coords) + (sand-coords '(500 0) (sand-check-next-move + (first sand-coords) (second sand-coords) arena))) + ((or (not sand-coords) ; sand rests + (out-of-bounds (first sand-coords) (second sand-coords) arena)) ; end condition - rest or out of bounds + (when (not sand-coords) + (set-place arena (first prev-coords) (second prev-coords) #\x))) + (format t "sc: ~a, prev: ~a~%" sand-coords prev-coords))) + + +;; ok, i got inner loop +;; now what? run that loop until the grain of sand ends up our of bounds +;; what are we calculating? + +;; "how many units of sand come to rest until they start falling into abyss" +;; so. do ? +(let ((arena *test-arena*)) + (do ((sand-units 0 (1+ sand-units)) + (drop-result (drop-sand-unit arena) (drop-sand-unit arena))) + ((not drop-result) + sand-units))) + +(slot-value *test-arena* 'grid) + +(defun drop-sand-unit-abyss (arena) + (do ((sand-units 0 (1+ sand-units)) + (drop-result (drop-sand-unit arena) (drop-sand-unit arena))) + ((not drop-result) + sand-units))) diff --git a/day14-test.txt b/day14-test.txt new file mode 100644 index 0000000..4e87bb5 --- /dev/null +++ b/day14-test.txt @@ -0,0 +1,2 @@ +498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9 diff --git a/day14.lisp b/day14.lisp new file mode 100644 index 0000000..e06e3a7 --- /dev/null +++ b/day14.lisp @@ -0,0 +1,142 @@ +;; https://adventofcode.com/2022/day/14 +(ql:quickload 'cl-ppcre) +(ql:quickload 'alexandria) + +(defparameter *day14-input-file* "day14-input.txt") + +(defclass arena () + ((grid :initarg :grid :initform (error "supply value for :grid")) + (leftmost :initarg :left :accessor leftmost) + (bottommost :initarg :bottom :accessor bottommost) + (rightmost :initarg :right :accessor rightmost))) + +(defun make-arena (left right bottom) + (let ((cols-num (1+ (- right left))) + (rows-num (1+ bottom))) + (make-instance 'arena :left left :right right :bottom bottom + :grid (make-array (list rows-num cols-num) + :initial-element #\.)))) + +(defparameter *test-arena* nil) + + +;; (x y) -> (row col) in grid +(defun translate-coords (arena x y) + (list y (- x (leftmost arena)))) + +(defun get-place (arena x y) + (destructuring-bind (row col) (translate-coords arena x y) + (when (and (<= 0 row) (<= 0 col) + (< row (array-dimension (slot-value arena 'grid) 0)) + (< col (array-dimension (slot-value arena 'grid) 1)) + ) + (aref (slot-value arena 'grid) row col)))) + +(defun set-place (arena x y value) + (destructuring-bind (row col) (translate-coords arena x y) + (when (and (<= 0 row) (<= 0 col) + (< row (array-dimension (slot-value arena 'grid) 0)) + (< col (array-dimension (slot-value arena 'grid) 1)) + ) + (setf (aref (slot-value arena 'grid) row col) value)))) + +(defun input-line-to-rock-coords (line) + (mapcar (lambda (coords) (mapcar #'parse-integer (cl-ppcre:split "," coords))) + (cl-ppcre:split " -> " line))) + +(defparameter *test-input-flat-coords* nil) + + +(defun put-rock-line (arena start-x end-x start-y end-y) + (let ((start-x (min start-x end-x)) + (end-x (max start-x end-x)) + (start-y (min start-y end-y)) + (end-y (max start-y end-y))) + (loop for x from start-x to end-x do + (progn + (loop for y from start-y to end-y + do (set-place arena x y #\#)))))) + +(defun put-rock-lines (arena rock-coords-lines) + (loop + for rock-coord-line in rock-coords-lines + do (put-rock-chain arena rock-coord-line))) + + +(defparameter *test-input-coords-chains* nil) + +(defun put-rock-chain (arena rock-coods-chain) + (do* ((coords-list rock-coods-chain (cdr coords-list)) + (first-coords (first rock-coods-chain) (first coords-list)) + (second-coords (second rock-coods-chain) (second coords-list))) + ((not second-coords) "end") + (destructuring-bind ((start-x start-y) (end-x end-y)) + (list first-coords second-coords) + (put-rock-line arena start-x end-x start-y end-y) ))) + +;; reinit things +(init-arena) +(defun init-arena () + + (setq *test-input-flat-coords* + (alexandria:flatten (mapcar #'input-line-to-rock-coords + (uiop:read-file-lines *day14-input-file*)))) + ;; building empty arena + (setq *test-arena* + (let ((flat-coords + (alexandria:flatten (mapcar #'input-line-to-rock-coords (uiop:read-file-lines *day14-input-file*))))) + (destructuring-bind (leftmost rightmost bottommost) + (loop + for (x y) on flat-coords + by #'cddr + minimize x into leftmost + maximize x into rightmost + maximize y into bottommost + finally (return (list leftmost rightmost bottommost))) + (make-arena leftmost rightmost bottommost)))) + + (setq *test-input-coords-chains* + (mapcar #'input-line-to-rock-coords + (uiop:read-file-lines *day14-input-file*))) + + ; this is second step of initialization + (put-rock-lines *test-arena* *test-input-coords-chains*)) + +(defun is-point-air (x y arena) + (or (eq #\. (get-place arena x y)) + (not (get-place arena x y)))) +(defun sand-check-next-move (sand-x sand-y arena) + (let* ((next-y (1+ sand-y)) + (possible-next-steps (list (list sand-x next-y) + (list (1- sand-x) next-y) + (list (1+ sand-x) next-y)))) + (first (remove-if-not (lambda (coords) + (is-point-air (first coords) (second coords) arena)) + possible-next-steps)))) +(defun out-of-bounds (x y arena) + (not (get-place arena x y))) + + +(defun drop-sand-unit (arena) + (do* + ((prev-coords nil sand-coords) + (sand-coords '(500 0) (sand-check-next-move + (first sand-coords) (second sand-coords) arena))) + ((or (not sand-coords) ; sand rests + (out-of-bounds (first sand-coords) (second sand-coords) arena)) ; end condition - rest or out of bounds + (when (not sand-coords) + (set-place arena (first prev-coords) (second prev-coords) #\x))) + ;; (format t "sc: ~a, prev: ~a~%" sand-coords prev-coords) + )) + +(defun drop-sand-unit-abyss (arena) + (do ((sand-units 0 (1+ sand-units)) + (drop-result (drop-sand-unit arena) (drop-sand-unit arena))) + ((not drop-result) + sand-units))) + +;; ok, could return arena +(init-arena) +(drop-sand-unit-abyss *test-arena*) ; 901 + +;;; PART 2