AdventOfCode2022/Kalender/day10.cs

134 lines
3.7 KiB
C#
Raw Normal View History

2022-12-10 08:36:39 +01:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kalender
{
internal class day10
{
static string file = "../../../Kalender/files/day10.txt";
2022-12-10 17:20:31 +01:00
static string[] input = File.ReadAllLines(file);
2022-12-10 21:19:15 +01:00
2022-12-10 08:36:39 +01:00
static public void part1()
{
2022-12-10 17:19:18 +01:00
Dictionary<int, int> cycles = new Dictionary<int, int>();
int cycle = 1;
2022-12-10 08:36:39 +01:00
int x = 1;
int result = 0;
2022-12-10 17:19:18 +01:00
int endResult = 0;
cycles.Add(cycle, x);
2022-12-10 17:20:31 +01:00
foreach (string line in input)
2022-12-10 08:36:39 +01:00
{
2022-12-10 17:19:18 +01:00
if (line != "noop")
{
cycle++;
cycles.Add(cycle, 0);
cycle++;
cycles.Add(cycle, int.Parse(line.Substring(4)));
}
else
2022-12-10 08:36:39 +01:00
{
2022-12-10 17:19:18 +01:00
cycle++;
cycles.Add(cycle, 0);
2022-12-10 08:36:39 +01:00
}
2022-12-10 17:19:18 +01:00
}
2022-12-10 08:36:39 +01:00
2022-12-10 17:19:18 +01:00
foreach (var item in cycles)
{
result += item.Value;
2022-12-10 08:36:39 +01:00
2022-12-10 17:19:18 +01:00
switch (item.Key)
2022-12-10 08:36:39 +01:00
{
case 20:
2022-12-10 17:19:18 +01:00
endResult += (item.Key * result);
2022-12-10 08:36:39 +01:00
break;
case 60:
2022-12-10 17:19:18 +01:00
endResult += (item.Key * result);
2022-12-10 08:36:39 +01:00
break;
case 100:
2022-12-10 17:19:18 +01:00
endResult += (item.Key * result);
2022-12-10 08:36:39 +01:00
break;
case 140:
2022-12-10 17:19:18 +01:00
endResult += (item.Key * result);
2022-12-10 08:36:39 +01:00
break;
case 180:
2022-12-10 17:19:18 +01:00
endResult += (item.Key * result);
2022-12-10 08:36:39 +01:00
break;
case 220:
2022-12-10 17:19:18 +01:00
endResult += (item.Key * result);
2022-12-10 08:36:39 +01:00
break;
}
}
2022-12-10 17:19:18 +01:00
Console.WriteLine($"Part 1: {endResult}");
2022-12-10 08:36:39 +01:00
}
2022-12-10 21:19:15 +01:00
public static void part2()
{
var q = new List<(int cycle, int val)>();
for (int i = 0; i < input.Length; i++)
{
var prog = input[i].Split(' ');
var instr = prog[0];
var val = prog.Length > 1 ? int.Parse(prog[1]) : 0;
var cycle = q.Count > 0 ? q[q.Count - 1].cycle : 0;
if (instr == "noop")
{
q.Add((cycle + 1, 0));
}
else
{
q.Add((cycle + 1, 0));
q.Add((cycle + 2, val));
}
}
var samplecycles = new HashSet<int> { 20, 60, 100, 140, 180, 220 };
int res = 0;
int reg = 1;
bool[,] screen = new bool[6, 40];
for (int i = 0; i < q.Count; i++)
{
var cur = q[i];
if (samplecycles.Contains(cur.cycle))
{
res += (cur.cycle * reg);
}
int curpos = (cur.cycle - 1) % 40;
if (curpos >= reg - 1 && curpos <= reg + 1)
{
screen[(cur.cycle - 1) / 40, (cur.cycle - 1) % 40] = true;
}
reg += cur.val;
}
// Part1 result
Console.WriteLine(res);
// Draw part2
for (int i = 0; i < screen.GetLength(0); i++)
{
for (int j = 0; j < screen.GetLength(1); j++)
{
if (screen[i, j])
Console.Write("#");
else Console.Write(".");
}
Console.WriteLine();
}
}
2022-12-10 08:36:39 +01:00
}
}