---
title: "Comment by sean rodman on \"Advent Of Code 2021 - Day 7\" | Hashnode"
description: "You can calculate the triangle number of Math.abs(crab - i) instead of summing a range from 1 to Math.abs(crab - i). It would look something like this: var posi"
url: "https://hashnode.com/posts/advent-of-code-2021-day-7/61b755a90059f90a57a572a5/comment/61b763150059f90a57a573dc"
---

[Discussion](https://hashnode.com/posts/advent-of-code-2021-day-7/61b755a90059f90a57a572a5)

Comment [Advent Of Code 2021 - Day 7](https://hashnode.com/posts/advent-of-code-2021-day-7/61b755a90059f90a57a572a5)

[sean rodman](https://hashnode.com/@srodman7689)

[Dec 13, 2021](https://hashnode.com/posts/advent-of-code-2021-day-7/61b755a90059f90a57a572a5/comment/61b763150059f90a57a573dc)

You can calculate the triangle number of Math.abs(crab - i) instead of summing a range from 1 to Math.abs(crab - i). It would look something like this:

```
var positionCost = crabs.Select(crab => {
  var dist = Math.abs(crab - i);
  return dist * (dist + 1) / 2;
}).Sum();
```

It gives the same answer and it is a lot faster. I found this out after reading your post. Thanks for posting! It helped a lot!

[Sebastian Markestedt](https://hashnode.com/@markestedt)

[Dec 13, 2021](https://hashnode.com/posts/advent-of-code-2021-day-7/61b755a90059f90a57a572a5/comment/61b763150059f90a57a573dc#reply-61b7673ad2a5c9036e6ef97a)

Cool, I'll give that a try. Thanks for reading and thanks for taking the time to comment! :)
