mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-04-06 13:03:46 +01:00
Compare commits
2 Commits
f7534c78d1
...
401bd4201b
Author | SHA1 | Date | |
---|---|---|---|
401bd4201b | |||
054d372470 |
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user