Skip to content

Instantly share code, notes, and snippets.

@aloisdg
Last active December 1, 2022 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloisdg/782f2cf1ba9dedafc5aa0be2ff7270d8 to your computer and use it in GitHub Desktop.
Save aloisdg/782f2cf1ba9dedafc5aa0be2ff7270d8 to your computer and use it in GitHub Desktop.
Advent_2022_1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var s = @"1000
2000
3000
4000
5000
6000
7000
8000
9000
10000";
var maxCalories = s
.Split("\n\n")
.Max(x => x.Split('\n').Sum(int.Parse));
Console.WriteLine(maxCalories);
}
}
// https://tio.run/##LYzLDsIgEEX3fMWkm0LUCb41Td278A@6KLZEayoQQNO/R6bxLk7uIzMv9VWh84OLK2N7nVJnTYgQoIZ2LaVkG8KWwHYz98TDbI/EE@E8ZzqQbcUYPbGjxtE@@E3FJ77VxBExYHDjEHnRmMYUIteOT1BfYPoPZWNKgV73n05zrpZwFzQ75YO@mshzXECupciqUvoB
const s = `1000
2000
3000
4000
5000
6000
7000
8000
9000
10000`;
console.log(Math.max(...s.split("\n\n").map(x => x.split('\n').reduce((a, b) => parseInt(b) + a, 0))));
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var s = @"1000
2000
3000
4000
5000
6000
7000
8000
9000
10000";
var maxCalories = s
.Split("\n\n")
.Select(x => x.Split('\n').Sum(int.Parse))
.OrderDescending()
.Take(3)
.Sum();
Console.WriteLine(maxCalories);
}
}
// https://tio.run/##bY3PDsIgDIfvPEXjZRA3gs6/Webdd9hhbCNmBoEAmr39pNODBy9f2v7ar3f5kqH3o4uFsYOa596aECFADe1GCEG2iBJBdgv3yMNSHpEnxHnp8UC0FSEfiU@SwIPTY6SrxjRmxfhDOjpBfYHpG2SNyRj3anj2ilKZQ8cwdtIHdTWRpnYNaSwYq4jnwfr4s9ZBATIF@NBqxbW90bSkxyQTOZR/zDL5uiSb5zc
const s = `1000
2000
3000
4000
5000
6000
7000
8000
9000
10000`;
const r = s.split("\n\n").map(x => x.split('\n').reduce((a, b) => parseInt(b) + a, 0));
r.sort((a, b) => b - a);
console.log(r.slice(0, 3).reduce((a, b) => a + b));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment