chore(*): Move old solutions into a separate folder

This commit is contained in:
Josh Creek
2023-11-30 12:51:45 +00:00
parent ed3eb9cb54
commit b78d79a3af
117 changed files with 141 additions and 141 deletions
+9
View File
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>Day_01</RootNamespace>
</PropertyGroup>
</Project>
+99
View File
@@ -0,0 +1,99 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace Day_01
{
class Program
{
static void Main(string[] args)
{
//Part1();
Part2();
//Test();
}
static void Part1()
{
// Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
var totalFuel = 0;
var lines = File.ReadAllLines("input.txt");
foreach (var line in lines)
{
var mass = Int32.Parse(line);
//divide by 3
decimal dividedAnswer = mass / 3;
// round down & subtract 2
var answer = Convert.ToInt32(Math.Floor(dividedAnswer)) - 2;
// add to total fuel count
totalFuel += answer;
}
// The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
// What is the sum of the fuel requirements for all of the modules on your spacecraft?
Console.WriteLine("Part 1: The sum of the fuel requirements for all of the modules on your spacecraft is " + totalFuel);
}
static int CalculateFuelNeeded(int mass)
{
//divide by 3
decimal dividedAnswer = mass / 3;
// round down & subtract 2
var answer = Convert.ToInt32(Math.Floor(dividedAnswer)) - 2;
// add to total fuel count
return answer;
}
static void Part2()
{
List<int> fuels = new List<int>();
var lines = File.ReadAllLines("input.txt");
foreach (var line in lines)
{
var mass = Int32.Parse(line);
// Calculate the fuel needed for the mass
var originalMassFuel = CalculateFuelNeeded(mass);
var nextFuel = CalculateFuelNeeded(originalMassFuel);
var totalFuel = originalMassFuel;
// Now calculate the fuel needed for the mass of the fuel, ad nauseum
// A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
// At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
var keepCalculating = true;
if(nextFuel > 0)
{
Console.WriteLine(nextFuel);
while(keepCalculating)
{
totalFuel += nextFuel;
nextFuel = CalculateFuelNeeded(nextFuel);
Console.WriteLine(nextFuel);
if(nextFuel <= 0)
{
keepCalculating = false;
}
}
}
fuels.Add(totalFuel);
}
// The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
int finalTotal = fuels.Sum(x => x);
// What is the sum of the fuel requirements for all of the modules on your spacecraft?
Console.WriteLine("Part 2: The sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel is " + finalTotal);
}
}
}
+20
View File
@@ -0,0 +1,20 @@
--- Day 1: The Tyranny of the Rocket Equation ---
Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
The Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
For example:
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
For a mass of 1969, the fuel required is 654.
For a mass of 100756, the fuel required is 33583.
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
What is the sum of the fuel requirements for all of the modules on your spacecraft?
+11
View File
@@ -0,0 +1,11 @@
--- Part Two ---
During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.
Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:
A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)
+100
View File
@@ -0,0 +1,100 @@
94794
58062
112067
139512
147400
99825
142617
107263
86294
97000
140204
72573
134981
111385
88303
79387
129111
122976
130685
75100
146566
73191
107641
109940
65518
102028
57370
144556
64017
64384
145114
115853
87939
90791
133443
139050
140657
85738
133749
92466
142918
96679
125035
127629
87906
104478
105147
121741
70312
73732
60838
82292
102931
103000
135903
78678
86314
50772
115673
106179
60615
105152
76550
140591
120916
62094
111273
63542
102974
78837
94840
89126
63150
52503
108530
101458
59660
116913
66440
83306
50693
58377
62005
130663
124304
79726
63001
73380
64395
124277
69742
63465
93172
142068
120081
119872
52801
100693
79229
90365
+9
View File
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>Day_02</RootNamespace>
</PropertyGroup>
</Project>
+114
View File
@@ -0,0 +1,114 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace Day_02
{
class Program
{
static void Main(string[] args)
{
//Part1();
Part2();
}
static List<int> RunProgram(List<int> inputList)
{
// Once you're done processing an opcode, move to the next one by stepping forward 4 positions.
for (int i = 0; i < inputList.Count(); i+=4)
{
int num1position = inputList[i+1];
int num1 = inputList[num1position];
int num2position = inputList[i+2];
int num2 = inputList[num2position];
switch (inputList[i])
{
case 1:
// Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.
// For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.
int calculatedValue1 = num1 + num2;
int calculatedValue1Position = inputList[i+3];
inputList[calculatedValue1Position] = calculatedValue1;
break;
case 2:
// Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.
int calculatedValue2 = num1 * num2;
int calculatedValue2Position = inputList[i+3];
inputList[calculatedValue2Position] = calculatedValue2;
break;
default:
// Finally return the list
return inputList;
}
}
// Finally return the list
return inputList;
}
static void Part1()
{
// There is only one line so get it
var input = File.ReadAllLines("input.txt")[0];
List<int> inputList = input.Split(',').Select(int.Parse).ToList();
// Replace position 1 with value 12
inputList[1] = 12;
// Replace position 2 with value 2
inputList[2] = 2;
// Run the program stored in the input
List<int> outputList = RunProgram(inputList);
Console.WriteLine("Part 1: The value is left at position 0 after the program halts is " + outputList[0]);
}
static void Part2()
{
// There is only one line so get it
var input = File.ReadAllLines("input.txt")[0];
List<int> inputList = input.Split(',').Select(int.Parse).ToList();
int finalNoun = 0;
int finalVerb = 0;
List<int> inputListModified = new List<int>(){};
for (int noun = 0; noun < 100; noun++)
{
for (int verb = 0; verb < 100; verb++)
{
// Reset the input list - .ToList() is needed here to ensure it's actually a new list, not just a reference to the original list
inputListModified = inputList.ToList();
// Replace position 1 with noun
inputListModified[1] = noun;
// Replace position 2 with verb
inputListModified[2] = verb;
// Run the program stored in the input
List<int> outputList = RunProgram(inputListModified);
if(outputList[0] == 19690720)
{
finalNoun = noun;
finalVerb = verb;
Console.WriteLine(noun + " - " + verb);
}
}
}
Console.WriteLine("Part 2: The pair of inputs that produce the output 19690720 are " + finalNoun + " and " + finalVerb);
Console.WriteLine($"100 * noun + verb is {100 * finalNoun + finalVerb}");
}
}
}
+45
View File
@@ -0,0 +1,45 @@
--- Day 2: 1202 Program Alarm ---
On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.
You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"
An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.
Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.
For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.
Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.
Once you're done processing an opcode, move to the next one by stepping forward 4 positions.
For example, suppose you have the following program:
1,9,10,3,2,3,11,0,99,30,40,50
For the purposes of illustration, here is the same program split into multiple lines:
1,9,10,3,
2,3,11,0,
99,
30,40,50
The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:
1,9,10,70,
2,3,11,0,
99,
30,40,50
Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:
3500,9,10,70,
2,3,11,0,
99,
30,40,50
Stepping forward 4 more positions arrives at opcode 99, halting the program.
Here are the initial and final states of a few more small programs:
1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
Once you have a working computer, the first step is to restore the gravity assist program (your puzzle input) to the "1202 program alarm" state it had just before the last computer caught fire. To do this, before running the program, replace position 1 with the value 12 and replace position 2 with the value 2. What value is left at position 0 after the program halts?
+18
View File
@@ -0,0 +1,18 @@
--- Part Two ---
"Good, the new computer seems to be working correctly! Keep it nearby during this mission - you'll probably use it again. Real Intcode computers support many more features than your new one, but we'll let you know what they are as you need them."
"However, your current priority should be to complete your gravity assist around the Moon. For this mission to succeed, we should settle on some terminology for the parts you've already built."
Intcode programs are given as a list of integers; these values are used as the initial state for the computer's memory. When you run an Intcode program, make sure to start by initializing memory to the program's values. A position in memory is called an address (for example, the first value in memory is at "address 0").
Opcodes (like 1, 2, or 99) mark the beginning of an instruction. The values used immediately after an opcode, if any, are called the instruction's parameters. For example, in the instruction 1,2,3,4, 1 is the opcode; 2, 3, and 4 are the parameters. The instruction 99 contains only an opcode and has no parameters.
The address of the current instruction is called the instruction pointer; it starts at 0. After an instruction finishes, the instruction pointer increases by the number of values in the instruction; until you add more instructions to the computer, this is always 4 (1 opcode + 3 parameters) for the add and multiply instructions. (The halt instruction would increase the instruction pointer by 1, but it halts the program instead.)
"With terminology out of the way, we're ready to proceed. To complete the gravity assist, you need to determine what pair of inputs produces the output 19690720."
The inputs should still be provided to the program by replacing the values at addresses 1 and 2, just like before. In this program, the value placed in address 1 is called the noun, and the value placed in address 2 is called the verb. Each of the two input values will be between 0 and 99, inclusive.
Once the program has halted, its output is available at address 0, also just like before. Each time you try a pair of inputs, make sure you first reset the computer's memory to the values in the program (your puzzle input) - in other words, don't reuse memory from a previous attempt.
Find the input noun and verb that cause the program to produce the output 19690720. What is 100 * noun + verb? (For example, if noun=12 and verb=2, the answer would be 1202.)
+1
View File
@@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,5,19,23,2,6,23,27,1,27,5,31,2,9,31,35,1,5,35,39,2,6,39,43,2,6,43,47,1,5,47,51,2,9,51,55,1,5,55,59,1,10,59,63,1,63,6,67,1,9,67,71,1,71,6,75,1,75,13,79,2,79,13,83,2,9,83,87,1,87,5,91,1,9,91,95,2,10,95,99,1,5,99,103,1,103,9,107,1,13,107,111,2,111,10,115,1,115,5,119,2,13,119,123,1,9,123,127,1,5,127,131,2,131,6,135,1,135,5,139,1,139,6,143,1,143,6,147,1,2,147,151,1,151,5,0,99,2,14,0,0
+9
View File
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>Day_03</RootNamespace>
</PropertyGroup>
</Project>
+197
View File
@@ -0,0 +1,197 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace Day_03
{
class Program
{
static void Main(string[] args)
{
//Part1();
Part2();
}
public class WireTouchPoint
{
public int x { get; set; }
public int y { get; set; }
}
static List<WireTouchPoint> FindAllWireTouchPoints(List<string> wireCommands)
{
List<WireTouchPoint> touchPoints = new List<WireTouchPoint>(){};
int currentX = 0;
int currentY = 0;
WireTouchPoint initialWTP = new WireTouchPoint();
initialWTP.x = currentX;
initialWTP.y = currentY;
touchPoints.Add(initialWTP);
// For each command, record every co-ordinate the wire touches
foreach(string wireCommand in wireCommands)
{
// Not bothering with error catching for converting strings to ints as the data cannot be wrongly typed in the input file
int distance = System.Convert.ToInt32(wireCommand.Substring(1));
// Handle the different commands in the first letter of each command string
switch (wireCommand[0])
{
case 'U':
// Up
for(int i = 0; i < distance; i++)
{
currentY += 1;
WireTouchPoint wtp = new WireTouchPoint();
wtp.x = currentX;
wtp.y = currentY;
touchPoints.Add(wtp);
}
break;
case 'D':
// Down
for(int i = 0; i < distance; i++)
{
currentY -= 1;
WireTouchPoint wtp = new WireTouchPoint();
wtp.x = currentX;
wtp.y = currentY;
touchPoints.Add(wtp);
}
break;
case 'L':
// Left
for(int i = 0; i < distance; i++)
{
currentX -= 1;
WireTouchPoint wtp = new WireTouchPoint();
wtp.x = currentX;
wtp.y = currentY;
touchPoints.Add(wtp);
}
break;
case 'R':
// Right
for(int i = 0; i < distance; i++)
{
currentX += 1;
WireTouchPoint wtp = new WireTouchPoint();
wtp.x = currentX;
wtp.y = currentY;
touchPoints.Add(wtp);
}
break;
default:
break;
}
}
return touchPoints;
}
internal class WireTouchPointComparer : IEqualityComparer<WireTouchPoint>
{
public bool Equals(WireTouchPoint wtp1, WireTouchPoint wtp2)
{
if(wtp1.x == wtp2.x && wtp1.y == wtp2.y)
{
return true;
}
return false;
}
// public int GetHashCode(WireTouchPoint obj)
// {
// return obj.x.GetHashCode();
// }
public int GetHashCode(WireTouchPoint obj)
{
unchecked
{
int hash = 17;
hash = hash * 23 + obj.x.GetHashCode();
hash = hash * 23 + obj.y.GetHashCode();
return hash;
}
}
}
static void Part1()
{
// Each line is one wire's path trace
var wire1 = File.ReadAllLines("input.txt")[0];
var wire2 = File.ReadAllLines("input.txt")[1];
List<string> wire1Commands = wire1.Split(',').ToList();
List<string> wire2Commands = wire2.Split(',').ToList();
// Find all points each wire touches
List<WireTouchPoint> wire1TouchPoints = FindAllWireTouchPoints(wire1Commands);
List<WireTouchPoint> wire2TouchPoints = FindAllWireTouchPoints(wire2Commands);
// Find all points where the two wires cross
var crossingPoints = wire1TouchPoints.Intersect(wire2TouchPoints, new WireTouchPointComparer()).Where(cp => cp.x != 0 && cp.y != 0);
// Declare a large default shortest distance, probably should be setting as null and null-checking later but multitasking while writing this so cba
int shortestManhattanDistance = 9999999;
// For each crossing point, work out the Manhattan distance to the central port
foreach(WireTouchPoint crossingPoint in crossingPoints)
{
// Convert co-ordinates to be positive numbers for calculating distance
int xVal = crossingPoint.x > 0 ? crossingPoint.x : crossingPoint.x * -1;
int yVal = crossingPoint.y > 0 ? crossingPoint.y : crossingPoint.y * -1;
int manhattanDistance = (xVal - 0) + (yVal - 0);
// Work out if it is shorter than the previously found one
if(manhattanDistance < shortestManhattanDistance)
{
shortestManhattanDistance = manhattanDistance;
}
}
Console.WriteLine("Part 1: The Manhattan distance from the central port to the closest intersection is " + shortestManhattanDistance);
}
static void Part2()
{
// Each line is one wire's path trace
var wire1 = File.ReadAllLines("input.txt")[0];
var wire2 = File.ReadAllLines("input.txt")[1];
List<string> wire1Commands = wire1.Split(',').ToList();
List<string> wire2Commands = wire2.Split(',').ToList();
// Find all points each wire touches
List<WireTouchPoint> wire1TouchPoints = FindAllWireTouchPoints(wire1Commands);
List<WireTouchPoint> wire2TouchPoints = FindAllWireTouchPoints(wire2Commands);
// Find all points where the two wires cross
var crossingPoints = wire1TouchPoints.Intersect(wire2TouchPoints, new WireTouchPointComparer()).Where(cp => cp.x != 0 && cp.y != 0);
// Declare a large default minimum steps, probably should be setting as null and null-checking later but multitasking while writing this so cba
int minimumSteps = 9999999;
foreach(WireTouchPoint crossingPoint in crossingPoints)
{
// Calculate the number of steps each wire takes to reach each the intersection
// If a wire visits a position on the grid multiple times, use the steps value from the first time it visits that position when calculating the total value of a specific intersection.
// The number of steps a wire takes is the total number of grid squares the wire has entered to get to that location, including the intersection being considered.
// Calculate the minimum number of steps each wire takes to reach the crossing point (using the index rather than index + 1 as the lists are zero-indexed ordered by the route taken but we want to ignore the start co-ordinates) - this will always return the first (i.e. closest) index
int steps1 = wire1TouchPoints.FindIndex(tp => tp.x == crossingPoint.x && tp.y == crossingPoint.y);
int steps2 = wire2TouchPoints.FindIndex(tp => tp.x == crossingPoint.x && tp.y == crossingPoint.y);
int totalSteps = steps1 + steps2;
// Work out if it is fewer steps than the previously found one
if(totalSteps < minimumSteps)
{
minimumSteps = totalSteps;
}
}
Console.WriteLine("Part 2: The fewest combined steps the wires must take to reach an intersection is " + minimumSteps);
}
}
}
+40
View File
@@ -0,0 +1,40 @@
--- Day 3: Crossed Wires ---
The gravity assist was successful, and you're well on your way to the Venus refuelling station. During the rush back on Earth, the fuel management system wasn't completely installed, so that's next on the priority list.
Opening the front panel reveals a jumble of wires. Specifically, two wires are connected to a central port and extend outward on a grid. You trace the path each wire takes as it leaves the central port, one wire per line of text (your puzzle input).
The wires twist and turn, but the two wires occasionally cross paths. To fix the circuit, you need to find the intersection point closest to the central port. Because the wires are on a grid, use the Manhattan distance for this measurement. While the wires do technically cross right at the central port where they both start, this point does not count, nor does a wire count as crossing with itself.
For example, if the first wire's path is R8,U5,L5,D3, then starting from the central port (o), it goes right 8, up 5, left 5, and finally down 3:
...........
...........
...........
....+----+.
....|....|.
....|....|.
....|....|.
.........|.
.o-------+.
...........
Then, if the second wire's path is U7,R6,D4,L4, it goes up 7, right 6, down 4, and left 4:
...........
.+-----+...
.|.....|...
.|..+--X-+.
.|..|..|.|.
.|.-X--+.|.
.|..|....|.
.|.......|.
.o-------+.
...........
These wires cross at two locations (marked X), but the lower-left one is closer to the central port: its distance is 3 + 3 = 6.
Here are a few more examples:
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83 = distance 159
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = distance 135
What is the Manhattan distance from the central port to the closest intersection?
+28
View File
@@ -0,0 +1,28 @@
--- Part Two ---
It turns out that this circuit is very timing-sensitive; you actually need to minimize the signal delay.
To do this, calculate the number of steps each wire takes to reach each intersection; choose the intersection where the sum of both wires' steps is lowest. If a wire visits a position on the grid multiple times, use the steps value from the first time it visits that position when calculating the total value of a specific intersection.
The number of steps a wire takes is the total number of grid squares the wire has entered to get to that location, including the intersection being considered. Again consider the example from above:
...........
.+-----+...
.|.....|...
.|..+--X-+.
.|..|..|.|.
.|.-X--+.|.
.|..|....|.
.|.......|.
.o-------+.
...........
In the above example, the intersection closest to the central port is reached after 8+5+5+2 = 20 steps by the first wire and 7+6+4+3 = 20 steps by the second wire for a total of 20+20 = 40 steps.
However, the top-right intersection is better: the first wire takes only 8+5+2 = 15 and the second wire takes only 7+6+2 = 15, a total of 15+15 = 30 steps.
Here are the best steps for the extra examples from above:
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83 = 610 steps
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = 410 steps
What is the fewest combined steps the wires must take to reach an intersection?
+2
View File
@@ -0,0 +1,2 @@
R1008,U336,R184,D967,R451,D742,L235,U219,R57,D439,R869,U207,L574,U670,L808,D675,L203,D370,L279,U448,L890,U297,R279,D613,L411,D530,L372,D88,R986,U444,R319,D95,L385,D674,R887,U855,R794,U783,R633,U167,L587,D545,L726,D196,R681,U609,R677,U881,R153,D724,L63,U246,R343,U315,R580,U872,L516,U95,R463,D809,R9,U739,R540,U670,L434,D699,L158,U47,L383,D483,L341,U61,R933,D269,R816,D589,R488,D169,R972,D534,L995,D277,L887,D657,R628,D322,R753,U813,L284,D237,R676,D880,L50,D965,L401,D619,R858,U313,L156,U535,R664,U447,L251,U168,L352,U881,L734,U270,L177,D903,L114,U998,L102,D149,R848,D586,L98,D157,R942,U496,R857,U362,R398,U86,R469,U358,L721,D631,R176,D365,L112,U472,L557,D153,R97,D639,L457,U566,R570,U106,R504,D292,L94,U499,R358,U653,L704,D627,R544,D24,L407,U513,R28,U643,L510,U579,R825,D376,L867,U999,R134,D734,R654,D989,L920,U872,R64,U626,R751,D425,R620,U274,L471,D83,R979,U577,L43,D320,R673,D187,R300,U134,L451,D717,R857,U576,R570,U988,R745,U840,R799,U809,R573,U354,L208,D976,L417,U473,L555,U563,L955,U823,R712,D869,L145,D735,L780,D74,R421,U42,L158,U689,R718,D455,L670,U128,L744,U401,R149,U102,L122,U832,R872,D40,R45,D325,L553,U980,L565,D497,L435,U647,L209,D822,L593,D28,R936,U95,R349,U511,L243,U895,R421,U336,L986,U264,R376,D183,R480,D947,R416,D706,R118,D799,R424,D615,R384,U185,L338,U14,R576,D901,L734,D417,L62,D254,R784,D973,R987,D848,R32,D72,L535,D633,L668,D664,R308,D474,L418,D39,L473,U388,L518,D544,R118,D948,L844,D956,R605,U14,L948,D78,L689,U443,L996,U932,R81,D879,R556,D633,R131
L993,U227,L414,U228,L304,U53,R695,U765,R162,D264,L530,U870,R771,D395,R27,D200,L235,D834,L559,D128,R284,U912,L959,U358,R433,U404,L539,U799,R271,D734,L104,U261,R812,D15,L474,U887,R606,U366,L694,U156,R385,D667,L329,D434,R745,U776,L319,U756,R208,D457,R705,U999,R284,U98,L657,U214,R639,D937,R675,U444,L891,D587,L914,D4,R294,D896,R534,D584,L887,U878,L807,U202,R505,U234,L284,D5,R667,U261,L127,D482,R777,D223,L707,D468,L606,U345,L509,D967,R437,U995,R28,D376,L2,D959,L814,U702,L38,D154,L79,D439,L259,U143,L376,U700,R894,U165,L300,D58,R631,D47,R684,U935,R262,D857,R797,D599,L705,U792,R439,D444,R398,D887,L81,D40,R671,U332,L820,U252,R691,U412,L794,D661,R810,U157,R858,U566,R892,D543,R10,D725,L653,D812,R733,D804,R816,U862,R994,U221,L33,U271,R766,D591,R575,D970,R152,D693,L916,U404,L658,D847,L605,D433,L583,U587,L129,D103,R407,U780,L901,D676,L846,D687,L9,D47,R295,D597,L808,U134,L186,D676,L62,U305,L73,D369,L468,U30,L472,U280,L413,U961,L98,D966,R308,D178,L21,D789,L871,D671,R665,U927,L906,U633,L135,D894,R110,D205,R324,D665,R143,D450,L978,U385,R442,D853,L518,U542,R211,U857,R119,D872,L246,U380,L874,U463,R153,U982,R832,D784,L652,U545,R71,U386,R427,D827,R986,U870,R959,U232,R509,U675,R196,U389,R944,U149,R152,U571,R527,U495,L441,U511,L899,D996,L707,D455,L358,D423,L14,D427,R144,D703,L243,U157,R876,D538,R26,D577,L385,U622,L149,D852,L225,U475,L811,D520,L226,U523,L338,D79,R565,U766,L609,U496,L189,D446,R63,U396,L629,U312,L841,D639,R466,U808,L60,D589,L146,U114,R165,U96,L809,D704,L61