aoc2021/aoc2021/Day02.cs

61 lines
1.3 KiB
C#
Raw Normal View History

2021-12-02 05:11:44 +00:00
namespace aoc2021;
/// <summary>
/// Day 2: <see href="https://adventofcode.com/2021/day/2"/>
/// </summary>
public sealed class Day02 : Day
{
public Day02() : base(2, "Dive!")
{
}
2021-12-12 20:09:41 +00:00
public override object Part1()
2021-12-02 05:11:44 +00:00
{
int horiz = 0, depth = 0;
foreach (var line in Input)
{
var s = line.Split(' ');
var x = int.Parse(s[1]);
switch (s[0])
{
case "forward":
horiz += x;
break;
case "down":
depth += x;
break;
case "up":
depth -= x;
break;
}
}
2021-12-12 20:09:41 +00:00
return horiz * depth;
2021-12-02 05:11:44 +00:00
}
2021-12-12 20:09:41 +00:00
public override object Part2()
2021-12-02 05:11:44 +00:00
{
int aim = 0, depth = 0, horiz = 0;
foreach (var line in Input)
{
var s = line.Split(' ');
var x = int.Parse(s[1]);
switch (s[0])
{
case "forward":
horiz += x;
depth += aim * x;
break;
case "down":
aim += x;
break;
case "up":
aim -= x;
break;
}
}
2021-12-12 20:09:41 +00:00
return horiz * depth;
2021-12-02 05:11:44 +00:00
}
}