-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.cs
139 lines (127 loc) · 5.12 KB
/
Day11.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
class Day11
{
public static List<string> ApplyRulesPart1(List<string> input)
{
List<string> outList = new List<string>(input);
for (int row = 0; row < input.Count(); row++)
{
for (int col = 0; col < input[row].Length; col++)
{
if (input[row][col] != '.')
{
int occupied = 0;
bool topEdge = (row == 0);
bool botEdge = (row == input.Count()-1);
bool leftEdge = (col == 0);
bool rightEdge = (col == input[row].Count()-1);
for (int i = (topEdge ? 0 : -1); i <= (botEdge ? 0 : 1); i++)
{
for (int j = (leftEdge ? 0 : -1); j <= (rightEdge ? 0 : 1); j++)
{
if ((i != 0 || j != 0) && input[row+i][col+j] == '#')
occupied++;
}
}
if (input[row][col] == 'L' && occupied == 0)
{
char[] temp = outList[row].ToCharArray();
temp[col] = '#';
outList[row] = new string(temp);
}
else if (input[row][col] == '#' && occupied >= 4)
{
char[] temp = outList[row].ToCharArray();
temp[col] = 'L';
outList[row] = new string(temp);
}
}
}
}
return outList;
}
public static int Part1(List<string> input)
{
List<string> nextIteration = new List<string>(input);
do
{
input = new List<string>(nextIteration);
nextIteration = ApplyRulesPart1(nextIteration);
} while (String.Join("\n", nextIteration) != String.Join("\n", input));
int occupiedCount = String.Join("", input).Length - String.Join("", input).Replace("#", "").Length;
return occupiedCount;
}
public static List<string> ApplyRulesPart2(List<string> input)
{
List<string> outList = new List<string>(input);
for (int row = 0; row < input.Count(); row++)
{
for (int col = 0; col < input[row].Length; col++)
{
if (input[row][col] != '.')
{
int occupied = 0;
bool topEdge = (row == 0);
bool botEdge = (row == input.Count()-1);
bool leftEdge = (col == 0);
bool rightEdge = (col == input[row].Count()-1);
for (int i = (topEdge ? 0 : -1); i <= (botEdge ? 0 : 1); i++)
{
for (int j = (leftEdge ? 0 : -1); j <= (rightEdge ? 0 : 1); j++)
{
int distance = 1;
try
{
for (int tempDistance = 1;; tempDistance++)
{
if (input[row+(i*tempDistance)][col+(j*tempDistance)] != '.')
{
distance = tempDistance;
break;
}
}
}
catch (ArgumentOutOfRangeException) {}
catch (IndexOutOfRangeException) {}
if ((i != 0 || j != 0) && input[row+(i*distance)][col+(j*distance)] == '#')
occupied++;
}
}
if (input[row][col] == 'L' && occupied == 0)
{
char[] temp = outList[row].ToCharArray();
temp[col] = '#';
outList[row] = new string(temp);
}
else if (input[row][col] == '#' && occupied >= 5)
{
char[] temp = outList[row].ToCharArray();
temp[col] = 'L';
outList[row] = new string(temp);
}
}
}
}
return outList;
}
public static int Part2(List<string> input)
{
List<string> nextIteration = new List<string>(input);
do
{
input = new List<string>(nextIteration);
nextIteration = ApplyRulesPart2(nextIteration);
} while (String.Join("\n", nextIteration) != String.Join("\n", input));
int occupiedCount = String.Join("", input).Length - String.Join("", input).Replace("#", "").Length;
return occupiedCount;
}
public static void Main(string[] args)
{
List<string> puzzleInput = File.ReadLines("input.txt").ToList();
Console.WriteLine($"Part 1: {Part1(puzzleInput)}");
Console.WriteLine($"Part 2: {Part2(puzzleInput)}");
}
}