---
title: "Discussion on \"Advent Of Code 2021 - Day 7\" | Hashnode"
description: "Discussion on \"Advent Of Code 2021 - Day 7\". C# Solution for Day 7 of Advent of Code 2021"
url: "https://hashnode.com/posts/advent-of-code-2021-day-7/61b755a90059f90a57a572a5"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Advent Of Code 2021 - Day 7](https://markestedt.hashnode.dev/advent-of-code-2021-day-7?utm_source=hashnode&utm_medium=feed)

C# Solution for Day 7 of Advent of Code 2021

Dec 13, 2021 · 3 min read · markestedt.hashnode.dev  
Read the article

-   [#csharp](https://hashnode.com/tag/csharp)
-   [#net](https://hashnode.com/tag/net)
-   [#challenge](https://hashnode.com/tag/challenge)
-   [#coding-challenge](https://hashnode.com/tag/coding-challenge)

## Written by

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

@markestedt · Sweden

  
[View profile](https://hashnode.com/@markestedt)

## Responses 1

Markdown supported · ⌘↵ to post

[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! :)
