refactor a little

This commit is contained in:
onyx-and-iris 2024-12-13 20:36:10 +00:00
parent 054d372470
commit 401bd4201b
2 changed files with 10 additions and 14 deletions

View File

@ -18,22 +18,20 @@ func Solve(buf []byte) (int, error) {
var total int var total int
for _, m := range machines { for _, m := range machines {
denominator := ((m.A.X * m.B.Y) - (m.B.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)) 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)) b_numerator := m.Prize.Y*m.A.X - m.Prize.X*m.A.Y
a := float64(a_numerator) / float64(denominator) a := float64(a_numerator) / float64(denominator)
b := float64(b_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 continue
} }
log.Debug(a, b) log.Debug(a, b)
if a > 0 && b > 0 { total += 3*int(a) + int(b)
total += 3*int(a) + int(b)
}
} }
return total, nil return total, nil

View File

@ -23,22 +23,20 @@ func Solve(buf []byte) (int, error) {
m.Prize.X += augment m.Prize.X += augment
m.Prize.Y += augment m.Prize.Y += augment
denominator := ((m.A.X * m.B.Y) - (m.B.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)) 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)) b_numerator := m.Prize.Y*m.A.X - m.Prize.X*m.A.Y
a := float64(a_numerator) / float64(denominator) a := float64(a_numerator) / float64(denominator)
b := float64(b_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 continue
} }
log.Debug(a, b) log.Debug(a, b)
if a > 0 && b > 0 { total += 3*int(a) + int(b)
total += 3*int(a) + int(b)
}
} }
return total, nil return total, nil