static Inhabitants CalculateTotals(char[,] m) { Inhabitants totals = new Inhabitants(); int width = m.GetLength(0); int height = m.GetLength(1); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { char c = m[x, y]; if (c == open) { totals.open++; } else if (c == tree) { totals.tree++; } else if (c == lumber) { totals.lumber++; } } } return(totals); }
static void PartB() { map = ReadInput(); //PrintMap(map); //Console.WriteLine(); int iMax = 1000000000; bool done = false; int iter = 0; int indexFound = -1; for (int i = 0; (i < iMax) && !done; i++) { //Console.Clear(); //PrintMap(map); //Thread.Sleep(100); if ((indexFound = PreviousEqualMap()) > 0) { done = true; iter = i; } else { IterateMap(); } } int offs = indexFound; int cycle = iter - indexFound; int cOffs = (iMax - offs) % cycle; int iMaxIndex = offs + cOffs; Inhabitants totals = CalculateTotals(maps[iMaxIndex]); int value = totals.tree * totals.lumber; Console.WriteLine("Part B: Result is " + value + " (took " + iter + " iterations)."); }
static void IterateMap() { char[,] nextMap = (char[, ])map.Clone(); int width = map.GetLength(0); int height = map.GetLength(1); Position p = new Position(); for (p.y = 0; p.y < height; p.y++) { for (p.x = 0; p.x < width; p.x++) { Inhabitants n = CalculateNeighbors(p); if (map[p.x, p.y] == open) { nextMap[p.x, p.y] = (n.tree >= 3) ? tree : open; } else if (map[p.x, p.y] == tree) { nextMap[p.x, p.y] = (n.lumber >= 3) ? lumber : tree; } else if (map[p.x, p.y] == lumber) { nextMap[p.x, p.y] = ((n.lumber >= 1) && (n.tree >= 1)) ? lumber : open; } } } map = nextMap; }
static Inhabitants CalculateNeighbors(Position p) { Inhabitants neighbors = new Inhabitants(); int xMax = map.GetLength(0) - 1; int yMax = map.GetLength(1) - 1; for (int x = p.x - 1; x <= p.x + 1; x++) { if ((x >= 0) && (x <= xMax)) { if (p.y - 1 >= 0) { UpdateNeighbors(map[x, p.y - 1], ref neighbors); } if (p.y + 1 <= yMax) { UpdateNeighbors(map[x, p.y + 1], ref neighbors); } } } if (p.x - 1 >= 0) { UpdateNeighbors(map[p.x - 1, p.y], ref neighbors); } if (p.x + 1 <= xMax) { UpdateNeighbors(map[p.x + 1, p.y], ref neighbors); } return(neighbors); }
static void UpdateNeighbors(char c, ref Inhabitants neighbors) { if (c == open) { neighbors.open++; } else if (c == tree) { neighbors.tree++; } else if (c == lumber) { neighbors.lumber++; } }
static void PartA() { map = ReadInput(); //PrintMap(map); //Console.WriteLine(); int iMax = 10; for (int i = 0; i < iMax; i++) { IterateMap(); } //PrintMap(map); Inhabitants totals = CalculateTotals(map); int value = totals.tree * totals.lumber; Console.WriteLine("Part A: Result is " + value + "."); }
static int PreviousEqualMap() { Inhabitants a = CalculateTotals(map); if (mapTotals.Count > 0) { for (int i = 0; i < mapTotals.Count; i++) { if (a.Equals(mapTotals[i])) { if (MapsEqual(map, maps[i])) { return(i); } } } } mapTotals.Add(a); maps.Add(map); return(-1); }
public bool Equals(Inhabitants i) { return((open == i.open) && (tree == i.tree) && (lumber == i.lumber)); }