79 lines
2.1 KiB
C#
79 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AdventOfCode2021
|
|
{
|
|
internal class day2
|
|
{
|
|
string[] input;
|
|
|
|
public day2(string day)
|
|
{
|
|
input = getInput(day);
|
|
part1();
|
|
part2();
|
|
}
|
|
public string[] getInput(string day)
|
|
{
|
|
string filepath = $"../../../inputs/{day}.txt";
|
|
string[] file = File.ReadAllLines(filepath);
|
|
|
|
return file;
|
|
}
|
|
|
|
|
|
public void part1()
|
|
{
|
|
int result = 0;
|
|
int horizontal = 0;
|
|
int depth = 0;
|
|
|
|
foreach(string line in input)
|
|
{
|
|
if (line.StartsWith("forward"))
|
|
{
|
|
horizontal += int.Parse(line.Split(' ')[1]);
|
|
}
|
|
else if (line.StartsWith("down"))
|
|
{
|
|
depth += int.Parse(line.Split(' ')[1]);
|
|
}
|
|
else if (line.StartsWith("up"))
|
|
{
|
|
depth -= int.Parse(line.Split(' ')[1]);
|
|
}
|
|
}
|
|
result = horizontal * depth;
|
|
Console.WriteLine($"Part 1: {result}");
|
|
|
|
}
|
|
public void part2()
|
|
{
|
|
int result = 0;
|
|
int horizontal = 0;
|
|
int aim = 0;
|
|
int depht = 0;
|
|
foreach (string line in input)
|
|
{
|
|
if (line.StartsWith("forward"))
|
|
{
|
|
horizontal += int.Parse(line.Split(' ')[1]);
|
|
depht += int.Parse(line.Split(' ')[1]) * aim;
|
|
}
|
|
else if (line.StartsWith("down"))
|
|
{
|
|
aim += int.Parse(line.Split(' ')[1]);
|
|
}
|
|
else if (line.StartsWith("up"))
|
|
{
|
|
aim -= int.Parse(line.Split(' ')[1]);
|
|
}
|
|
}
|
|
result = horizontal * depht;
|
|
Console.WriteLine($"Part 2: {result}");
|
|
}
|
|
}
|
|
} |