Skip to content

Commit

Permalink
Add broken diamond counting solution to 2023 day 21 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 21, 2023
1 parent 48db598 commit 31f44a2
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 8 deletions.
120 changes: 113 additions & 7 deletions src/main/scala/eu/sim642/adventofcode2023/Day21.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down

0 comments on commit 31f44a2

Please sign in to comment.