AdventOfCode2021/day1.cs
2022-12-13 17:08:08 +01:00

83 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2021
{
internal class day1
{
string[] input;
public day1(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 temp = int.Parse(input[0]);
int counter = 0;
for(int i = 0; i < input.Length; i++)
{
if (int.Parse(input[i]) > temp)
{
temp = int.Parse(input[i]);
counter++;
}
else
{
temp = int.Parse(input[i]);
}
}
Console.WriteLine($"Part 1 : {counter}");
}
public void part2()
{
int temp = computeWindow(0);
int counter = 0;
for (int i = 3; i < input.Length; i++)
{
if(computeWindow(i -2) > temp)
{
counter++;
temp= computeWindow(i - 2);
}
else
{
temp = computeWindow(i - 2);
}
}
Console.WriteLine($"Part 2 : {counter}");
}
public int computeWindow(int index)
{
int window = int.Parse(input[index]) + int.Parse(input[index + 1]) + int.Parse(input[index + 2]);
return window;
}
}
}