public static void SolvePartTwo() { var clayLocations = GetInput(); var sourceLocation = new XY(500, 0); var map = new ClayMap(clayLocations); var streamBuilder = new WaterStreamBuilder(sourceLocation, map); var stream = streamBuilder.GetStream(); var result = stream.CountStableWater(); result.WriteLine("Day 17, Part 2: "); }
private static StringBuilder CreateMap(XY sourceLocation, ClayMap clayMap, IDictionary <XY, Water> existingWater) { var topBoundary = 0; var bottomBoundary = clayMap.ClayLocations.MinBy(l => l.Y).Y - 1; var leftBoundary = clayMap.ClayLocations.MinBy(l => l.X).X - 5; var rightBoundary = clayMap.ClayLocations.MaxBy(l => l.X).X + 5; var yLocations = Enumerable.Range(bottomBoundary, topBoundary - bottomBoundary + 1).Reverse(); var xLocations = Enumerable.Range(leftBoundary, rightBoundary - leftBoundary + 1); var builder = new StringBuilder(); foreach (var y in yLocations) { foreach (var x in xLocations) { var l = new XY(x, y); if (clayMap.IsClayAtLocation(l)) { builder.Append('#'); } else if (existingWater.ContainsKey(l)) { if (l == sourceLocation) { builder.Append('+'); } else if (existingWater[l].IsStable) { builder.Append('~'); } else { builder.Append('|'); } } else { builder.Append(' '); } } builder.AppendLine(); } return(builder); }
public Water[] SpreadStreamAtLocation(XY location, ClayMap clayMap) { var water = GetWater(location); var directionsToFlow = GetFlowDirections(water.Location, clayMap); var newAddedWater = new Water[directionsToFlow.Length]; for (var i = 0; i < directionsToFlow.Length; i++) { var newWater = water.FlowToDirection(directionsToFlow[i]); newAddedWater[i] = newWater; AddWater(newWater); } return(newAddedWater); }
public static void SolvePartOne() { var clayLocations = GetInput(); var sourceLocation = new XY(500, 0); var map = new ClayMap(clayLocations); var stream = new WaterStreamBuilder(sourceLocation, map); var water = stream.GetStream(); var result = water.Select(w => w.Location).Count(map.IsInClayArea); //File.WriteAllText( // Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "map.txt"), // CreateMap(sourceLocation, map, water).ToString()); result.WriteLine("Day 17, Part 1: "); }
private XY[] GetFlowDirections(XY location, ClayMap clayMap) { bool CanFlowToLocation(XY loc) => !clayMap.IsClayAtLocation(loc) && !HasWaterAtLocation(loc); var locationBelow = location + XY.Down; if (CanFlowToLocation(locationBelow)) { return(XY.Down.RepeatOnce().ToArray()); } var isWaterBelow = TryGetWater(locationBelow, out var waterBelow); if (!isWaterBelow || waterBelow.IsStable) { return(Water.GetPossibleFlowDirections() .Where(d => CanFlowToLocation(location + d)) .ToArray()); } return(new XY[0]); }
public WaterStreamBuilder(XY sourceLocation, ClayMap clayMap) { this.sourceLocation = sourceLocation; this.clayMap = clayMap; }