Initial Commit
This commit is contained in:
242
2022/Day21CSharp/Advent2022_Day21.cs
Normal file
242
2022/Day21CSharp/Advent2022_Day21.cs
Normal file
@@ -0,0 +1,242 @@
|
||||
using System.Numerics;
|
||||
|
||||
var exampleData = new ProblemData("example-input.txt");
|
||||
var puzzleData = new ProblemData("puzzle-input.txt");
|
||||
|
||||
Console.WriteLine("!! === Part01 === !!");
|
||||
Console.WriteLine("Example root yells: {0}", exampleData.GetMonkeyValuePart01("root"));
|
||||
Console.WriteLine("Puzzle root yells: {0}", puzzleData.GetMonkeyValuePart01("root"));
|
||||
|
||||
Console.WriteLine("!! === Part02 === !!");
|
||||
Console.WriteLine("Example human yells: {0}", exampleData.GetMonkeyValuePart02());
|
||||
Console.WriteLine("Puzzle human yells: {0}", puzzleData.GetMonkeyValuePart02());
|
||||
|
||||
abstract class MonkeyCommand
|
||||
{
|
||||
public MonkeyCommand(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public abstract BigInteger GetValue();
|
||||
|
||||
public string Name { get; protected set; }
|
||||
|
||||
public static bool DebugPrint { get; set; } = false;
|
||||
}
|
||||
|
||||
class MonkeyYellCommand
|
||||
: MonkeyCommand
|
||||
{
|
||||
public MonkeyYellCommand(string name, BigInteger val)
|
||||
: base(name)
|
||||
{
|
||||
Value = val;
|
||||
}
|
||||
|
||||
public BigInteger Value { get; set; }
|
||||
public override BigInteger GetValue()
|
||||
{
|
||||
return Value;
|
||||
}
|
||||
}
|
||||
|
||||
class MonkeyMathCommand
|
||||
: MonkeyCommand
|
||||
{
|
||||
public MonkeyMathCommand(string name, Dictionary<string, MonkeyCommand> lookupTable, string left, string right, char op)
|
||||
: base(name)
|
||||
{
|
||||
LookupTable = lookupTable;
|
||||
Left = left;
|
||||
Right = right;
|
||||
if ((op != '-') && (op != '+') && (op != '*') && (op != '/') && (op != '=')) throw new InvalidDataException();
|
||||
Op = op;
|
||||
}
|
||||
|
||||
public string Left { get; set; }
|
||||
public string Right { get; set; }
|
||||
|
||||
public BigInteger RightValue
|
||||
{
|
||||
get
|
||||
{
|
||||
return LookupTable[Right].GetValue();
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger LeftValue
|
||||
{
|
||||
get
|
||||
{
|
||||
return LookupTable[Left].GetValue();
|
||||
}
|
||||
}
|
||||
|
||||
public char Op { get; set; }
|
||||
|
||||
public char InverseOp
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (Op)
|
||||
{
|
||||
case '+': return '-';
|
||||
case '-': return '+';
|
||||
case '*': return '/';
|
||||
case '/': return '*';
|
||||
default: throw new InvalidDataException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<string, MonkeyCommand> LookupTable { get; set; }
|
||||
|
||||
public override BigInteger GetValue()
|
||||
{
|
||||
BigInteger left = LeftValue;
|
||||
BigInteger right = RightValue;
|
||||
|
||||
switch (Op)
|
||||
{
|
||||
case '+':
|
||||
return left + right;
|
||||
case '-':
|
||||
return left - right;
|
||||
case '*':
|
||||
return left * right;
|
||||
case '/':
|
||||
return left / right;
|
||||
case '=':
|
||||
if (MonkeyCommand.DebugPrint && LookupTable["humn"].GetValue()%10000 == 0) Console.WriteLine("Comparing {0} v {1} with humn {2}", left, right, LookupTable["humn"].GetValue());
|
||||
return left - right;
|
||||
default: throw new InvalidDataException();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ProblemData
|
||||
{
|
||||
private Dictionary<string, MonkeyCommand> CommandLookupPart01 { get; set; }
|
||||
private Dictionary<string, MonkeyCommand> CommandLookupPart02 { get; set; }
|
||||
|
||||
public ProblemData(string filename)
|
||||
{
|
||||
CommandLookupPart01 = new Dictionary<string, MonkeyCommand>();
|
||||
CommandLookupPart02 = new Dictionary<string, MonkeyCommand>();
|
||||
|
||||
using (StreamReader reader = System.IO.File.OpenText(filename))
|
||||
{
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = reader.ReadLine();
|
||||
if (line == null) throw new InvalidDataException();
|
||||
|
||||
var split = line.Split(':', StringSplitOptions.TrimEntries);
|
||||
var name = split[0];
|
||||
|
||||
MonkeyCommand monkey, monkey2;
|
||||
|
||||
// First see if its a value being yelled by the goddamned monkeys
|
||||
int val;
|
||||
if (int.TryParse(split[1], out val))
|
||||
{
|
||||
// It's a value monkey
|
||||
monkey = new MonkeyYellCommand(name, val);
|
||||
monkey2 = new MonkeyYellCommand(name, val);
|
||||
}
|
||||
else
|
||||
{
|
||||
// It's a math monkey
|
||||
var mathline = split[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
monkey = new MonkeyMathCommand(name, CommandLookupPart01, mathline[0], mathline[2], mathline[1][0]);
|
||||
|
||||
char op = mathline[1][0];
|
||||
if (name == "root")
|
||||
op = '=';
|
||||
monkey2 = new MonkeyMathCommand(name, CommandLookupPart02, mathline[0], mathline[2], op);
|
||||
}
|
||||
|
||||
CommandLookupPart01[name] = monkey;
|
||||
CommandLookupPart02[name] = monkey2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger GetMonkeyValuePart01(string name)
|
||||
{
|
||||
return CommandLookupPart01[name].GetValue();
|
||||
}
|
||||
|
||||
public BigInteger GetMonkeyValuePart02()
|
||||
{
|
||||
MonkeyMathCommand rootCmd = (MonkeyMathCommand)CommandLookupPart02["root"];
|
||||
|
||||
// Find the side the human is on
|
||||
bool isHumanOnLeft = IsHumanOnLeft(CommandLookupPart02, rootCmd);
|
||||
BigInteger targetValue;
|
||||
|
||||
if (isHumanOnLeft)
|
||||
{
|
||||
targetValue = rootCmd.RightValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
targetValue = rootCmd.LeftValue;
|
||||
}
|
||||
|
||||
var humnCmd = (MonkeyYellCommand)CommandLookupPart02["humn"];
|
||||
BigInteger result = 0;
|
||||
BigInteger high = long.MaxValue, low = long.MinValue;
|
||||
BigInteger mid = low + (high - low) / 2;
|
||||
|
||||
bool incrementUp = false;
|
||||
humnCmd.Value = 1;
|
||||
var first = isHumanOnLeft ? rootCmd.LeftValue : rootCmd.RightValue;
|
||||
humnCmd.Value = 100;
|
||||
var second = isHumanOnLeft ? rootCmd.LeftValue : rootCmd.RightValue;
|
||||
|
||||
if (second > first) incrementUp = true;
|
||||
|
||||
while ((isHumanOnLeft ? rootCmd.LeftValue : rootCmd.RightValue) != targetValue)
|
||||
{
|
||||
humnCmd.Value = mid;
|
||||
|
||||
var newVal = isHumanOnLeft ? rootCmd.LeftValue : rootCmd.RightValue;
|
||||
|
||||
if ((incrementUp && (newVal > targetValue)) || (!incrementUp && (newVal < targetValue)))
|
||||
{
|
||||
high = low + (high - low) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
low = low + (high - low) / 2;
|
||||
}
|
||||
|
||||
mid = low + (high - low) / 2;
|
||||
}
|
||||
|
||||
return CommandLookupPart02["humn"].GetValue();
|
||||
}
|
||||
|
||||
private static bool IsHumanOnLeft(Dictionary<string,MonkeyCommand> dict, MonkeyMathCommand command)
|
||||
{
|
||||
MonkeyYellCommand humnCmd = (MonkeyYellCommand)dict["humn"];
|
||||
|
||||
var ogRight = command.RightValue;
|
||||
var ogLeft = command.LeftValue;
|
||||
|
||||
humnCmd.Value += 10;
|
||||
|
||||
var newRight = command.RightValue;
|
||||
var newLeft = command.LeftValue;
|
||||
|
||||
humnCmd.Value -= 10;
|
||||
|
||||
if (ogLeft == newLeft) return false;
|
||||
else if (ogRight == newRight) return true;
|
||||
else throw new InvalidDataException();
|
||||
}
|
||||
|
||||
}
|
||||
10
2022/Day21CSharp/Day21CSharp.csproj
Normal file
10
2022/Day21CSharp/Day21CSharp.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
8
2022/Day21CSharp/Properties/launchSettings.json
Normal file
8
2022/Day21CSharp/Properties/launchSettings.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Day21CSharp": {
|
||||
"commandName": "Project",
|
||||
"workingDirectory": "C:\\dev\\DevSandbox\\AdventOfCode\\2022\\Day21CSharp"
|
||||
}
|
||||
}
|
||||
}
|
||||
15
2022/Day21CSharp/example-input.txt
Normal file
15
2022/Day21CSharp/example-input.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
root: pppw + sjmn
|
||||
dbpl: 5
|
||||
cczh: sllz + lgvd
|
||||
zczc: 2
|
||||
ptdq: humn - dvpt
|
||||
dvpt: 3
|
||||
lfqf: 4
|
||||
humn: 5
|
||||
ljgn: 2
|
||||
sjmn: drzm * dbpl
|
||||
sllz: 4
|
||||
pppw: cczh / lfqf
|
||||
lgvd: ljgn * ptdq
|
||||
drzm: hmdt - zczc
|
||||
hmdt: 32
|
||||
58
2022/Day21CSharp/problem.txt
Normal file
58
2022/Day21CSharp/problem.txt
Normal file
@@ -0,0 +1,58 @@
|
||||
--- Day 21: Monkey Math ---
|
||||
|
||||
The monkeys are back! You're worried they're going to try to steal your stuff again, but it seems like they're just holding their ground and making various monkey noises at you.
|
||||
|
||||
Eventually, one of the elephants realizes you don't speak monkey and comes over to interpret. As it turns out, they overheard you talking about trying to find the grove; they can show you a shortcut if you answer their riddle.
|
||||
|
||||
Each monkey is given a job: either to yell a specific number or to yell the result of a math operation. All of the number-yelling monkeys know their number from the start; however, the math operation monkeys need to wait for two other monkeys to yell a number, and those two other monkeys might also be waiting on other monkeys.
|
||||
|
||||
Your job is to work out the number the monkey named root will yell before the monkeys figure it out themselves.
|
||||
|
||||
For example:
|
||||
|
||||
root: pppw + sjmn
|
||||
dbpl: 5
|
||||
cczh: sllz + lgvd
|
||||
zczc: 2
|
||||
ptdq: humn - dvpt
|
||||
dvpt: 3
|
||||
lfqf: 4
|
||||
humn: 5
|
||||
ljgn: 2
|
||||
sjmn: drzm * dbpl
|
||||
sllz: 4
|
||||
pppw: cczh / lfqf
|
||||
lgvd: ljgn * ptdq
|
||||
drzm: hmdt - zczc
|
||||
hmdt: 32
|
||||
|
||||
Each line contains the name of a monkey, a colon, and then the job of that monkey:
|
||||
|
||||
A lone number means the monkey's job is simply to yell that number.
|
||||
A job like aaaa + bbbb means the monkey waits for monkeys aaaa and bbbb to yell each of their numbers; the monkey then yells the sum of those two numbers.
|
||||
aaaa - bbbb means the monkey yells aaaa's number minus bbbb's number.
|
||||
Job aaaa * bbbb will yell aaaa's number multiplied by bbbb's number.
|
||||
Job aaaa / bbbb will yell aaaa's number divided by bbbb's number.
|
||||
|
||||
So, in the above example, monkey drzm has to wait for monkeys hmdt and zczc to yell their numbers. Fortunately, both hmdt and zczc have jobs that involve simply yelling a single number, so they do this immediately: 32 and 2. Monkey drzm can then yell its number by finding 32 minus 2: 30.
|
||||
|
||||
Then, monkey sjmn has one of its numbers (30, from monkey drzm), and already has its other number, 5, from dbpl. This allows it to yell its own number by finding 30 multiplied by 5: 150.
|
||||
|
||||
This process continues until root yells a number: 152.
|
||||
|
||||
However, your actual situation involves considerably more monkeys. What number will the monkey named root yell?
|
||||
|
||||
Your puzzle answer was 223971851179174.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
--- Part Two ---
|
||||
|
||||
Due to some kind of monkey-elephant-human mistranslation, you seem to have misunderstood a few key details about the riddle.
|
||||
|
||||
First, you got the wrong job for the monkey named root; specifically, you got the wrong math operation. The correct operation for monkey root should be =, which means that it still listens for two numbers (from the same two monkeys as before), but now checks that the two numbers match.
|
||||
|
||||
Second, you got the wrong monkey for the job starting with humn:. It isn't a monkey - it's you. Actually, you got the job wrong, too: you need to figure out what number you need to yell so that root's equality check passes. (The number that appears after humn: in your input is now irrelevant.)
|
||||
|
||||
In the above example, the number you need to yell to pass root's equality test is 301. (This causes root to get the same number, 150, from both of its monkeys.)
|
||||
|
||||
What number do you yell to pass root's equality test?
|
||||
2597
2022/Day21CSharp/puzzle-input.txt
Normal file
2597
2022/Day21CSharp/puzzle-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user