From 31f44a275f52068d8f38d80971e5cd0113afd969 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Thu, 21 Dec 2023 19:57:43 +0200 Subject: [PATCH] Add broken diamond counting solution to 2023 day 21 part 2 --- .../eu/sim642/adventofcode2023/Day21.scala | 120 +++++++++++++++++- .../sim642/adventofcode2023/Day21Test.scala | 8 +- 2 files changed, 120 insertions(+), 8 deletions(-) diff --git a/src/main/scala/eu/sim642/adventofcode2023/Day21.scala b/src/main/scala/eu/sim642/adventofcode2023/Day21.scala index 6b886029..3334fc6d 100644 --- a/src/main/scala/eu/sim642/adventofcode2023/Day21.scala +++ b/src/main/scala/eu/sim642/adventofcode2023/Day21.scala @@ -68,19 +68,125 @@ object Day21 { //println((q, r)) val x1 = 0 - val y1 = NaivePart2Solution.countReachableExactlyInfinite(grid, r) + val y0 = NaivePart2Solution.countReachableExactlyInfinite(grid, r) val x2 = 1 - val y2 = NaivePart2Solution.countReachableExactlyInfinite(grid, r + 131) + val y1 = NaivePart2Solution.countReachableExactlyInfinite(grid, r + 131) val x3 = 2 - val y3 = NaivePart2Solution.countReachableExactlyInfinite(grid, r + 2 * 131) + val y2 = NaivePart2Solution.countReachableExactlyInfinite(grid, r + 2 * 131) + + //def f(x: Long): Long = { + // y0 * (x - x2) * (x - x3) / (x1 - x2) / (x1 - x3) + + // y1 * (x - x1) * (x - x3) / (x2 - x1) / (x2 - x3) + + // y2 * (x - x1) * (x - x2) / (x3 - x1) / (x3 - x2) + //} def f(x: Long): Long = { - y1 * (x - x2) * (x - x3) / (x1 - x2) / (x1 - x3) + - y2 * (x - x1) * (x - x3) / (x2 - x1) / (x2 - x3) + - y3 * (x - x1) * (x - x2) / (x3 - x1) / (x3 - x2) + y0 * (x - 1) * (x - 2) / 2 - y1 * x * (x - 2) + y2 * x * (x - 1) / 2 + } + /* + + /\ + \/ + + + */ + + println(y0) + println(y1) + println(y2) + + // y0 0 (1) [1, 0, 0] = innerEven + // y1 1 (9) [1, 4, 4] = innerEven + 4 * outer + 4 * innerOdd + // y2 2 (25) [9, 12, 4] = innerEven + 4 * outer + 4 * innerOdd + 8 * outer + 8 * innerEven + + // y4 3 (49) [9, 24, 16] = innerEven + 4 * outer + 4 * innerOdd + 8 * outer + 8 * innerEven + 12 * outer + 12 * innerOdd + + + /* + f(0) = 1A + AA + AAAA + AAAA + AA + + f(1) = 1A + 4B + 2X + 2Y + BB + BBBB + BBBB + XY BB YX + XXYY YYXX + YYXX XXYY + BB YX AA XY BB + BBBB AAAA BBBB + BBBB AAAA BBBB + BB YX AA XY BB + YYXX XXYY + XXYY YYXX + XY BB YX + BBBB + BBBB + BB + + f(2) = 9A + 4B + 6X + 6Y + AA + AAAA + AAAA + YX AA XY + YYXX XXYY + XXYY YYXX + AA XY BB YX AA + AAAA BBBB AAAA + AAAA BBBB AAAA + YX AA XY BB YX AA XY + YYXX XXYY YYXX XXYY + XXYY YYXX XXYY YYXX + AA XY BB YX AA XY BB YX AA +AAAA BBBB AAAA BBBB AAAA +AAAA BBBB AAAA BBBB AAAA + AA XY BB YX AA XY BB YX AA + XXYY YYXX XXYY YYXX + YYXX XXYY YYXX XXYY + YX AA XY BB YX AA XY + AAAA BBBB AAAA + AAAA BBBB AAAA + AA XY BB YX AA + XXYY YYXX + YYXX XXYY + YX AA XY + AAAA + AAAA + AA + + f(3) = 9A + 16B + + */ + + + def f2(n: Long): Long = { + val a = y0 // A + val xy = (y2 - y1 - 8 * a).toDouble / 4 // X + Y + val b = (y1 - y0 - 2 * xy) / 4 // B + //val xy = (-8 * y0 - y1 + y2) / 4 + //val b = (6 * y0 + 3 * y1 - y2) / 8 + println((a, b, xy)) + + val c = (2 * n + 1) * (2 * n + 1) + // n: 0, 1, 2, 3, ... + // ca: 1, 1, 9, 9, ... + // cb: 0, 4, 4, 16, ... + val ca = (2 * (n / 2) + 1) * (2 * (n / 2) + 1) + //val cb = inners - ca + val cb = (2 * ((n + 1) / 2)) * (2 * ((n + 1) / 2)) + val cxy = (c - ca - cb) / 2 + val r = ca * a + cb * b + cxy * xy + println((n, r)) + println((ca, cb, cxy)) + r.toLong } - f(q) + //assert(f2(0) == y0) + //assert(f2(1) == y1) + //assert(f2(2) == y2) + f2(q) } } diff --git a/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala b/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala index deb5f6eb..74295d2d 100644 --- a/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala +++ b/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala @@ -72,11 +72,17 @@ object Day21Test { //(512, 228690), //(1024, 912913), - (131, 15130), + //(131, 15130), //(2 * 131, 60085), //(3 * 131, 134866), //(4 * 131, 239473), //(5 * 131, 373906), + + (65, 3778), + (65 + 1 * 131, 33695), + (65 + 2 * 131, 93438), + (65 + 3 * 131, 183007), + (65 + 4 * 131, 302402), ) forAll(stepsExpectedReachable) { (steps, expectedReachable) =>