Skip to content

Commit f924a74

Browse files
committed
day 7 part 2
1 parent 8679ed9 commit f924a74

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

07/main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,20 @@ func (w *World) IdealFuelRequired() (fuel int) {
6565
return w.amountOfFuelRequired
6666
}
6767

68+
// Now fuel required goes 1,3,6,10,15
6869
func (w *World) FuelRequired(pos int) (fuel int) {
6970
for _, c := range w.crabs {
7071
if c < pos {
71-
fuel += (pos - c)
72+
fuel += triangularNumber(pos - c)
7273
} else {
73-
fuel += (c - pos)
74+
fuel += triangularNumber(c - pos)
7475
}
7576
}
7677
return fuel
7778
}
79+
80+
// triangularNumber returns the amount of fuel required to reach a positional difference
81+
// based on the pattern 1,3,6,10,15....
82+
func triangularNumber(n int) int {
83+
return (n * (n + 1)) / 2
84+
}

0 commit comments

Comments
 (0)