Compare commits

...

2 Commits

Author SHA1 Message Date
401bd4201b refactor a little 2024-12-13 20:36:10 +00:00
054d372470 mv 2024-12-13 19:29:32 +00:00
3 changed files with 14 additions and 18 deletions
day-13/internal

@ -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

@ -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

@ -35,15 +35,15 @@ func ParseLines(r io.Reader) ([]*machine.Machine, error) {
matches = append(matches, m)
}
if err := scanner.Err(); err != nil {
return nil, err
}
var machines []*machine.Machine
for i := 0; i < len(matches); i += 3 {
machines = append(machines, machine.New(matches[i], matches[i+1], matches[i+2]))
}
if err := scanner.Err(); err != nil {
return nil, err
}
return machines, nil
}