134 lines
3.7 KiB
C#
134 lines
3.7 KiB
C#
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";
|
|
static string[] input = File.ReadAllLines(file);
|
|
|
|
|
|
static public void part1()
|
|
{
|
|
Dictionary<int, int> cycles = new Dictionary<int, int>();
|
|
int cycle = 1;
|
|
int x = 1;
|
|
int result = 0;
|
|
int endResult = 0;
|
|
cycles.Add(cycle, x);
|
|
foreach (string line in input)
|
|
{
|
|
if (line != "noop")
|
|
{
|
|
cycle++;
|
|
cycles.Add(cycle, 0);
|
|
cycle++;
|
|
cycles.Add(cycle, int.Parse(line.Substring(4)));
|
|
}
|
|
else
|
|
{
|
|
cycle++;
|
|
cycles.Add(cycle, 0);
|
|
}
|
|
}
|
|
|
|
foreach (var item in cycles)
|
|
{
|
|
result += item.Value;
|
|
|
|
switch (item.Key)
|
|
{
|
|
case 20:
|
|
endResult += (item.Key * result);
|
|
break;
|
|
case 60:
|
|
endResult += (item.Key * result);
|
|
break;
|
|
case 100:
|
|
endResult += (item.Key * result);
|
|
break;
|
|
case 140:
|
|
endResult += (item.Key * result);
|
|
break;
|
|
case 180:
|
|
endResult += (item.Key * result);
|
|
break;
|
|
case 220:
|
|
endResult += (item.Key * result);
|
|
break;
|
|
}
|
|
}
|
|
Console.WriteLine($"Part 1: {endResult}");
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|