-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask4.cs
24 lines (23 loc) · 931 Bytes
/
Task4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
namespace adventofcode_2021.Task4
{
public class Solution
{
/// <summary>
/// Solution for the second https://adventofcode.com/2021/day/2/ task
/// </summary>
public static int Function(IEnumerable<string> input)
{
return input.Aggregate((distance: 0, aim: 0, depth: 0), (r, next) =>
next.Split(' ') switch { var t => (t[0], int.Parse(t[1])) } switch
{
("forward", var val) => (r.distance + val, r.aim, r.depth + r.aim * val),
("up", var val) => (r.distance, r.aim - val, r.depth),
("down", var val) => (r.distance, r.aim + val, r.depth),
_ => throw new ArgumentException("Invalid string value for command"),
}, result => result.distance * result.depth);
}
}
}