You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.5 KiB
70 lines
1.5 KiB
namespace AdventOfCode._2021;
|
|
|
|
public class Day2
|
|
{
|
|
public void Part1()
|
|
{
|
|
var input = File.ReadAllLines("2021/input/day2.txt");
|
|
|
|
int h = 0;
|
|
int d = 0;
|
|
|
|
foreach (var s in input)
|
|
{
|
|
var split = s.Split(' ');
|
|
(string direction, int units) = (split[0], int.Parse(split[1]));
|
|
|
|
if (direction is "forward")
|
|
{
|
|
h += units;
|
|
}
|
|
else if (direction is "down")
|
|
{
|
|
d += units;
|
|
}
|
|
else
|
|
{
|
|
d -= units;
|
|
}
|
|
}
|
|
|
|
Console.WriteLine(h * d);
|
|
}
|
|
|
|
public void Part2()
|
|
{
|
|
var input = File.ReadAllLines("2021/input/day2.txt");
|
|
|
|
int h = 0;
|
|
int d = 0;
|
|
int aim = 0;
|
|
|
|
foreach (var s in input)
|
|
{
|
|
var split = s.Split(' ');
|
|
(char dir, int units) = (split[0][0], int.Parse(split[1]));
|
|
|
|
switch (dir)
|
|
{
|
|
case 'f':
|
|
h += units;
|
|
d += aim * units;
|
|
break;
|
|
|
|
case 'd':
|
|
aim += units;
|
|
break;
|
|
|
|
case 'u':
|
|
aim -= units;
|
|
break;
|
|
|
|
default:
|
|
throw new Exception(dir.ToString());
|
|
}
|
|
}
|
|
|
|
Console.WriteLine(h * d); // first input isn't counted.
|
|
}
|
|
}
|