From 401bd4201bc8fad9bf8275f7c69379592a9dfb85 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 13 Dec 2024 20:36:10 +0000 Subject: [PATCH] refactor a little --- day-13/internal/one/solve.go | 12 +++++------- day-13/internal/two/solve.go | 12 +++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/day-13/internal/one/solve.go b/day-13/internal/one/solve.go index 537010d..1e9f188 100644 --- a/day-13/internal/one/solve.go +++ b/day-13/internal/one/solve.go @@ -18,22 +18,20 @@ func Solve(buf []byte) (int, error) { var total int for _, m := range machines { - denominator := ((m.A.X * m.B.Y) - (m.B.X * m.A.Y)) - a_numerator := ((m.Prize.X * m.B.Y) - (m.Prize.Y * m.B.X)) - b_numerator := ((m.Prize.Y * m.A.X) - (m.Prize.X * m.A.Y)) + denominator := m.A.X*m.B.Y - m.B.X*m.A.Y + a_numerator := m.Prize.X*m.B.Y - m.Prize.Y*m.B.X + b_numerator := m.Prize.Y*m.A.X - m.Prize.X*m.A.Y a := float64(a_numerator) / float64(denominator) b := float64(b_numerator) / float64(denominator) - if a != math.Round(a) || b != math.Round(b) { + if a != math.Round(a) || a < 0 || b != math.Round(b) || b < 0 { continue } log.Debug(a, b) - if a > 0 && b > 0 { - total += 3*int(a) + int(b) - } + total += 3*int(a) + int(b) } return total, nil diff --git a/day-13/internal/two/solve.go b/day-13/internal/two/solve.go index 59ef429..73ea9f3 100644 --- a/day-13/internal/two/solve.go +++ b/day-13/internal/two/solve.go @@ -23,22 +23,20 @@ func Solve(buf []byte) (int, error) { m.Prize.X += augment m.Prize.Y += augment - denominator := ((m.A.X * m.B.Y) - (m.B.X * m.A.Y)) - a_numerator := ((m.Prize.X * m.B.Y) - (m.Prize.Y * m.B.X)) - b_numerator := ((m.Prize.Y * m.A.X) - (m.Prize.X * m.A.Y)) + denominator := m.A.X*m.B.Y - m.B.X*m.A.Y + a_numerator := m.Prize.X*m.B.Y - m.Prize.Y*m.B.X + b_numerator := m.Prize.Y*m.A.X - m.Prize.X*m.A.Y a := float64(a_numerator) / float64(denominator) b := float64(b_numerator) / float64(denominator) - if a != math.Round(a) || b != math.Round(b) { + if a != math.Round(a) || a < 0 || b != math.Round(b) || b < 0 { continue } log.Debug(a, b) - if a > 0 && b > 0 { - total += 3*int(a) + int(b) - } + total += 3*int(a) + int(b) } return total, nil