Day12 part 1

This commit is contained in:
2018-12-14 07:13:05 +01:00
parent e9139526a5
commit cd6a78eb75
6 changed files with 287 additions and 1 deletions

View File

@@ -70,6 +70,7 @@
<Compile Include="Day09_Tests.cs" /> <Compile Include="Day09_Tests.cs" />
<Compile Include="Day10_Tests.cs" /> <Compile Include="Day10_Tests.cs" />
<Compile Include="Day11_Tests.cs" /> <Compile Include="Day11_Tests.cs" />
<Compile Include="Day12_Tests.cs" />
<Compile Include="GuardEvent_Tests.cs" /> <Compile Include="GuardEvent_Tests.cs" />
<Compile Include="MarbleGame_Tests.cs" /> <Compile Include="MarbleGame_Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -0,0 +1,36 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AdventOfCode2018.Tests
{
[TestClass()]
public class Day12_Tests
{
[TestMethod()]
public void ResolvePart1__Test()
{
Day12 day = new Day12();
string result = day.ResolvePart1(new string[]
{
"initial state: #..#.#..##......###...###",
"",
"...## => #",
"..#.. => #",
".#... => #",
".#.#. => #",
".#.## => #",
".##.. => #",
".#### => #",
"#.#.# => #",
"#.### => #",
"##.#. => #",
"##.## => #",
"###.. => #",
"###.# => #",
"####. => #",
});
Assert.AreEqual("325", result);
}
}
}

View File

@@ -54,6 +54,7 @@
<Compile Include="Day09.cs" /> <Compile Include="Day09.cs" />
<Compile Include="Day10.cs" /> <Compile Include="Day10.cs" />
<Compile Include="Day11.cs" /> <Compile Include="Day11.cs" />
<Compile Include="Day12.cs" />
<Compile Include="IDay.cs" /> <Compile Include="IDay.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@@ -92,6 +93,9 @@
<Content Include="inputs\Day10.txt"> <Content Include="inputs\Day10.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="inputs\Day12.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

210
AdventOfCode2018/Day12.cs Normal file
View File

@@ -0,0 +1,210 @@
using System;
using System.Collections.Generic;
namespace AdventOfCode2018
{
/*
--- Day 12: Subterranean Sustainability ---
The year 518 is significantly more underground than your history books implied. Either that, or you've arrived in a vast cavern network under the North Pole.
After exploring a little, you discover a long tunnel that contains a row of small pots as far as you can see to your left and right. A few of them contain plants - someone is trying to grow things in these geothermally-heated caves.
The pots are numbered, with 0 in front of you. To the left, the pots are numbered -1, -2, -3, and so on; to the right, 1, 2, 3.... Your puzzle input contains a list of pots from 0 to the right and whether they do (#) or do not (.) currently contain a plant, the initial state. (No other pots currently contain plants.) For example, an initial state of #..##.... indicates that pots 0, 3, and 4 currently contain plants.
Your puzzle input also contains some notes you find on a nearby table: someone has been trying to figure out how these plants spread to nearby pots. Based on the notes, for each generation of plants, a given pot has or does not have a plant based on whether that pot (and the two pots on either side of it) had a plant in the last generation. These are written as LLCRR => N, where L are pots to the left, C is the current pot being considered, R are the pots to the right, and N is whether the current pot will have a plant in the next generation. For example:
A note like ..#.. => . means that a pot that contains a plant but with no plants within two pots of it will not have a plant in it during the next generation.
A note like ##.## => . means that an empty pot with two plants on each side of it will remain empty in the next generation.
A note like .##.# => # means that a pot has a plant in a given generation if, in the previous generation, there were plants in that pot, the one immediately to the left, and the one two pots to the right, but not in the ones immediately to the right and two to the left.
It's not clear what these plants are for, but you're sure it's important, so you'd like to make sure the current configuration of plants is sustainable by determining what will happen after 20 generations.
For example, given the following input:
initial state: #..#.#..##......###...###
...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #
For brevity, in this example, only the combinations which do produce a plant are listed. (Your input includes all possible combinations.) Then, the next 20 generations will look like this:
1 2 3
0 0 0 0
0: ...#..#.#..##......###...###...........
1: ...#...#....#.....#..#..#..#...........
2: ...##..##...##....#..#..#..##..........
3: ..#.#...#..#.#....#..#..#...#..........
4: ...#.#..#...#.#...#..#..##..##.........
5: ....#...##...#.#..#..#...#...#.........
6: ....##.#.#....#...#..##..##..##........
7: ...#..###.#...##..#...#...#...#........
8: ...#....##.#.#.#..##..##..##..##.......
9: ...##..#..#####....#...#...#...#.......
10: ..#.#..#...#.##....##..##..##..##......
11: ...#...##...#.#...#.#...#...#...#......
12: ...##.#.#....#.#...#.#..##..##..##.....
13: ..#..###.#....#.#...#....#...#...#.....
14: ..#....##.#....#.#..##...##..##..##....
15: ..##..#..#.#....#....#..#.#...#...#....
16: .#.#..#...#.#...##...#...#.#..##..##...
17: ..#...##...#.#.#.#...##...#....#...#...
18: ..##.#.#....#####.#.#.#...##...##..##..
19: .#..###.#..#.#.#######.#.#.#..#.#...#..
20: .#....##....#####...#######....#.#..##.
The generation is shown along the left, where 0 is the initial state. The pot numbers are shown along the top, where 0 labels the center pot, negative-numbered pots extend to the left, and positive pots extend toward the right. Remember, the initial state begins at pot 0, which is not the leftmost pot used in this example.
After one generation, only seven plants remain. The one in pot 0 matched the rule looking for ..#.., the one in pot 4 matched the rule looking for .#.#., pot 9 matched .##.., and so on.
In this example, after 20 generations, the pots shown as # contain plants, the furthest left of which is pot -2, and the furthest right of which is pot 34. Adding up all the numbers of plant-containing pots after the 20th generation produces 325.
After 20 generations, what is the sum of the numbers of all pots which contain a plant?
*/
public class Day12 : IDay
{
public string ResolvePart1(string[] inputs)
{
Initialize(inputs);
int maxGenerations = 20;
bool[] field = GenerateInitialField(maxGenerations, _initialState, true);
bool[] nextField = GenerateInitialField(maxGenerations, _initialState, false);
for (int i = 0; i < maxGenerations; i++)
{
Console.Write("{0:000}: ", i);
ShowField(field);
SimulateGeneration(_rules, field, nextField);
bool[] aux = field;
field = nextField;
nextField = aux;
}
Console.Write("{0:000}: ", maxGenerations);
ShowField(field);
int sum = 0;
int offset = 2 + maxGenerations;
for (int i = 0; i < field.Length; i++)
{
if (field[i])
{
sum += (i - offset);
}
}
return sum.ToString();
}
public string ResolvePart2(string[] inputs)
{
return null;
}
private class PlantRule
{
public bool Minus2 { get; set; }
public bool Minus1 { get; set; }
public bool Current { get; set; }
public bool Plus1 { get; set; }
public bool Plus2 { get; set; }
public bool Result { get; set; }
}
private List<bool> _initialState = new List<bool>();
private List<PlantRule> _rules = new List<PlantRule>();
private void Initialize(string[] inputs)
{
_initialState.Clear();
foreach (char c in inputs[0].Substring("initial state: ".Length))
{
_initialState.Add(c == '#');
}
_rules.Clear();
for (int i = 2; i < inputs.Length; i++)
{
if (string.IsNullOrEmpty(inputs[i])) { continue; }
string[] parts = inputs[i].Split(new string[] { " => " }, StringSplitOptions.RemoveEmptyEntries);
_rules.Add(new PlantRule
{
Minus2 = (parts[0][0] == '#'),
Minus1 = (parts[0][1] == '#'),
Current = (parts[0][2] == '#'),
Plus1 = (parts[0][3] == '#'),
Plus2 = (parts[0][4] == '#'),
Result = (parts[1][0] == '#'),
});
}
}
private static bool[] GenerateInitialField(int maxGenerations, List<bool> initialState, bool initialize)
{
int maxSize = (2 + maxGenerations) * 2 + initialState.Count;
bool[] field = new bool[maxSize];
if (initialize)
{
int offset = 2 + maxGenerations;
for (int i = 0; i < initialState.Count; i++)
{
field[i + offset] = initialState[i];
}
}
return field;
}
private static void SimulateGeneration(List<PlantRule> rules, bool[] field, bool[] finalField)
{
for (int i = 2; i < (field.Length - 2); i++)
{
bool minus2 = field[i - 2];
bool minus1 = field[i - 1];
bool current = field[i];
bool plus1 = field[i + 1];
bool plus2 = field[i + 2];
bool result = false;
foreach (PlantRule rule in rules)
{
if (
rule.Minus2 == minus2 &&
rule.Minus1 == minus1 &&
rule.Current == current &&
rule.Plus1 == plus1 &&
rule.Plus2 == plus2 &&
true
)
{
result = rule.Result;
break;
}
}
finalField[i] = result;
}
}
private static void ShowField(bool[] field)
{
foreach (bool plant in field)
{
Console.Write(plant ? "#" : ".");
}
Console.WriteLine();
}
}
}

View File

@@ -7,7 +7,7 @@ namespace AdventOfCode2018
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
int currentDayNumber = 11; int currentDayNumber = 12;
IDay currentDay = null; IDay currentDay = null;
switch (currentDayNumber) switch (currentDayNumber)
@@ -23,6 +23,7 @@ namespace AdventOfCode2018
case 9: currentDay = new Day09(); break; case 9: currentDay = new Day09(); break;
case 10: currentDay = new Day10(); break; case 10: currentDay = new Day10(); break;
case 11: currentDay = new Day11(); break; case 11: currentDay = new Day11(); break;
case 12: currentDay = new Day12(); break;
} }
string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber)); string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber));

View File

@@ -0,0 +1,34 @@
initial state: ##.#..#.#..#.####.#########.#...#.#.#......##.#.#...##.....#...#...#.##.#...##...#.####.##..#.#..#.
..#.. => .
..#.# => .
#.#.. => .
.#..# => .
#.... => .
....# => .
.#.#. => #
#.### => .
####. => .
..... => .
.#... => #
##### => #
.#### => .
#..#. => #
#...# => #
.###. => .
###.# => #
...## => #
#.##. => #
.#.## => #
##.#. => #
...#. => .
..### => #
###.. => #
##... => .
..##. => .
.##.# => .
##.## => .
.##.. => .
##..# => #
#.#.# => .
#..## => #