private static void ExecutePart1(IEnumerable <string> map) { var slope = new Slope(3, 1); var numberOfTrees = TraverseSlope(map, slope); Console.WriteLine($"Part1 - Number of trees: {numberOfTrees}"); }
static void Main(string[] args) { var text = System.IO.File.ReadAllLines("input.txt"); var slope = new Slope { Down = 1, Right = 3 }; var terrain = new Terrain(); terrain.Land = new List <bool> [text.LongLength]; var currentPosition = new Position(); //parse the terrain for (var i = 0; i < text.Length; i++) { var currentTerrain = new List <bool>(); foreach (char c in text[i]) { currentTerrain.Add(c == '#'); } terrain.Land[i] = currentTerrain; } Part1(slope, terrain, currentPosition); var slope1Position = new Position(); var slope1 = Part2(slope, terrain, slope1Position); var slope2Position = new Position(); slope = new Slope { Down = 1, Right = 1 }; var slope2 = Part2(slope, terrain, slope2Position); var slope3Position = new Position(); slope = new Slope { Down = 1, Right = 5 }; var slope3 = Part2(slope, terrain, slope3Position); var slope4Position = new Position(); slope = new Slope { Down = 1, Right = 7 }; var slope4 = Part2(slope, terrain, slope4Position); var slope5Position = new Position(); slope = new Slope { Down = 2, Right = 1 }; var slope5 = Part2(slope, terrain, slope5Position); Console.WriteLine(slope1 * slope2 * slope3 * slope4 * slope5); }
public static int CountTrees(Slope slope, Size vector) { Point pos = new Point(0, 0); int numTrees = 0; while (pos.Y < slope.Height) { if (slope.GetChar(pos) == '#') { numTrees++; } pos += vector; } return(numTrees); }
public static int TraverseSlope(IEnumerable <string> map, Slope slope) { var xPos = 0; for (int i = 0; i < map.Count(); i += slope.StepsDown) { var current = map.ElementAt(i); var currentChar = current.ToCharArray()[xPos % current.Length]; if (currentChar == '#') { slope.AddTree(); } xPos += slope.StepsRight; } return(slope.NumberOfTrees); }
public List <long> FindCollisions(IEnumerable <Pattern> patterns, Slope slope) { string tree = "#"; var hits = (from p in patterns select(long) 0).ToList(); for (int y = 0, x = 0; y < patterns.Count(); y += slope.Y, x += slope.X) { var p = patterns.ElementAt(y).Row; if (p.Substring(x % p.Length, 1) == tree) { hits[y]++; } } return(hits); }
static void Main(string[] args) { List <string> inputList = (args.Count() > 0) ? InputLoader.GetInputAsList(args[0]) : InputLoader.GetInputAsList("input.txt"); var slope = new Slope(inputList) { PositionX = 0, PositionY = 0, MovementRight = 3, MovementBottom = 1 }; slope.Start(); Console.WriteLine($"Trees encountered in part 1: {slope.TreesEncountered}"); // right 3, below 1 var slope31 = slope.TreesEncountered; // right 1, below 1 slope.PositionX = slope.PositionY = 0; slope.MovementRight = slope.MovementBottom = 1; slope.Start(); var slope11 = slope.TreesEncountered; // right 5, below 1 slope.PositionX = slope.PositionY = 0; slope.MovementRight = 5; slope.MovementBottom = 1; slope.Start(); var slope51 = slope.TreesEncountered; // right 7, below 1 slope.PositionX = slope.PositionY = 0; slope.MovementRight = 7; slope.MovementBottom = 1; slope.Start(); var slope71 = slope.TreesEncountered; // right 1, below 2 slope.PositionX = slope.PositionY = 0; slope.MovementRight = 1; slope.MovementBottom = 2; slope.Start(); var slope12 = slope.TreesEncountered; // part 2 Console.WriteLine($"Number for part 2: {slope11*slope31*slope51*slope71*slope12}"); Console.ReadLine(); }
private static void Part1(Slope slope, Terrain terrain, Position currentPosition) { for (long i = 0; i < terrain.Land.Length; i = i + slope.Down) { var row = terrain.Land[i]; int xPosition = currentPosition.x; if (currentPosition.x > row.Count) { xPosition = xPosition % row.Count; } if (row[xPosition]) { currentPosition.TreeCount += 1; } currentPosition.x = currentPosition.x + slope.Right; currentPosition.y = currentPosition.y + slope.Down; } Console.WriteLine("Part 1 " + currentPosition.TreeCount); }
private static long Part2(Slope slope, Terrain terrain, Position currentPosition) { //follow the tree down for (long i = 0; i < terrain.Land.Length; i = i + slope.Down) { var row = terrain.Land[i]; int xPosition = currentPosition.x; if (currentPosition.x >= row.Count) { xPosition = xPosition % row.Count; } if (row[xPosition]) { currentPosition.TreeCount += 1; } currentPosition.x = currentPosition.x + slope.Right; currentPosition.y = currentPosition.y + slope.Down; } Console.WriteLine("Part 2 " + currentPosition.TreeCount); return(currentPosition.TreeCount); }
static void Main(string[] args) { int numTrees = 0; long product = 0; Performance.TimeRun("Both stars", () => { var slope = new Slope(TextFile.ReadStringList("input.txt")); numTrees = CountTrees(slope, new Size(3, 1)); List <Size> vectors = new List <Size>() { new Size(1, 1), new Size(5, 1), new Size(7, 1), new Size(1, 2), }; product = numTrees; for (int i = 0; i < vectors.Count; i++) { product *= CountTrees(slope, vectors[i]); } }); Console.WriteLine($"Crashed into {numTrees} trees"); Console.WriteLine($"Product is {product}"); }