worked on day 11 p 2

This commit is contained in:
Rob 2022-12-15 22:56:10 +01:00
parent 389a85869b
commit ee6da383ca
10 changed files with 217 additions and 157 deletions

View File

@ -31,7 +31,7 @@ Monkey 3:
If true: throw to monkey 0 If true: throw to monkey 0
If false: throw to monkey 1"; If false: throw to monkey 1";
Answerable answerable = new Day11Part1(); Answerable answerable = new Day11Part2();
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile); byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
dataArray = Encoding.UTF8.GetBytes(_demoData); dataArray = Encoding.UTF8.GetBytes(_demoData);

View File

@ -15,9 +15,6 @@
internal class RopeWalker internal class RopeWalker
{ {
private readonly VirtualPoint HeadLocation = new();
private VirtualPoint TailLocation = new();
private List<string> TailVistedLocations = new(); private List<string> TailVistedLocations = new();
internal int ProcessMovement(string[] movements, int ropeLength) internal int ProcessMovement(string[] movements, int ropeLength)

View File

@ -17,27 +17,29 @@
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++) for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
{ {
monkeys[monkeyIndex] = new(); monkeys[monkeyIndex] = new(monkeyIndex, 3, monkeyString[monkeyIndex].Split(NewLine));
monkeys[monkeyIndex].MonkeyId = monkeyIndex;
} }
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++) for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
{ {
string[] monkey = monkeyString[monkeyIndex].Split(NewLine); string[] monkey = monkeyString[monkeyIndex].Split(NewLine);
int monkeyId = Convert.ToInt32(monkey[0].Remove(monkey[0].Length - 1).Split(' ')[^1]);
monkeys[monkeyId].Items = monkey[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt32(s.Trim())).ToList();
string[] operation = monkey[2].Split(' ');
monkeys[monkeyIndex].SetOperation(operation[^2][0], operation[^1]);
monkeys[monkeyIndex].SetTestValue(Convert.ToInt32(monkey[3].Split(' ')[^1]));
int trueMonkey = Convert.ToInt32(monkey[4].Split(' ')[^1]); int trueMonkey = Convert.ToInt32(monkey[4].Split(' ')[^1]);
int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]); int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]);
monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]); monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]);
} }
return string.Empty; for(int round = 0; round < 20; round++)
{
for (int monkey = 0; monkey < monkeys.Length; monkey++)
{
monkeys[monkey].DoTurn();
}
}
long[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
return $"{mostInspects[0] * mostInspects[1]}";
} }
} }
} }

View File

@ -1,5 +1,6 @@
namespace AdventOfCodeLibrary._2022 namespace AdventOfCodeLibrary._2022
{ {
using AdventOfCodeLibrary._2022.Day_11;
using AdventOfCodeLibrary.Shared; using AdventOfCodeLibrary.Shared;
public class Day11Part2 : Answerable public class Day11Part2 : Answerable
@ -10,7 +11,47 @@
public override string GetAnswer(byte[] data) public override string GetAnswer(byte[] data)
{ {
return string.Empty; string[] monkeyString = GetAsString(data).Split(NewLine + NewLine);
Monkey[] monkeys = new Monkey[monkeyString.Length];
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
{
monkeys[monkeyIndex] = new(monkeyIndex, 1, monkeyString[monkeyIndex].Split(NewLine));
}
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
{
string[] monkey = monkeyString[monkeyIndex].Split(NewLine);
int trueMonkey = Convert.ToInt32(monkey[4].Split(' ')[^1]);
int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]);
monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]);
}
for (int round = 1; round <= 20; round++)
{
for (int monkey = 0; monkey < monkeys.Length; monkey++)
{
monkeys[monkey].DoTurn();
}
//if (round == 1 || round == 20 || round == 1000 || round == 2000 || round == 3000 || round == 4000 || round == 5000)
{
Console.WriteLine($"== After round {round} ==");
foreach (Monkey monkey in monkeys.OrderBy(m => m.MonkeyId))
{
Console.WriteLine($"Monkey {monkey.MonkeyId} inspected items {monkey.TotalInspections} times.");
}
Console.WriteLine();
}
}
long[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
return $"{mostInspects[0] * mostInspects[1]}";
} }
} }
} }

View File

@ -4,9 +4,12 @@
{ {
internal int MonkeyId; internal int MonkeyId;
internal long TotalInspections = 0;
internal List<int> Items; internal List<int> Items;
private int TestDevision; private int TestDevision;
private int WorryDevision;
private OperationEnum Operation; private OperationEnum Operation;
private int? OperationValue = null; private int? OperationValue = null;
@ -14,6 +17,17 @@
private Monkey ThrowTrueMonkey; private Monkey ThrowTrueMonkey;
private Monkey ThrowFalseMonkey; private Monkey ThrowFalseMonkey;
internal Monkey (int monkeyId, int worryDivirdNumber, string[] monkeyData)
{
MonkeyId = monkeyId;
WorryDevision = worryDivirdNumber;
Items = monkeyData[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt32(s.Trim())).ToList();
string[] operation = monkeyData[2].Split(' ');
SetOperation(operation[^2][0], operation[^1]);
SetTestValue(Convert.ToInt32(monkeyData[3].Split(' ')[^1]));
}
internal void DoTurn() internal void DoTurn()
{ {
if (!Items.Any()) if (!Items.Any())
@ -21,9 +35,10 @@
for (int worryIndex = 0; worryIndex < Items.Count; worryIndex++) for (int worryIndex = 0; worryIndex < Items.Count; worryIndex++)
{ {
int item = Items[0]; int item = Items[worryIndex];
item = GetNewWorry(item); item = GetNewWorry(item);
ThrowToMonkey(item); ThrowToMonkey(item);
TotalInspections++;
} }
Items.Clear(); Items.Clear();
@ -49,7 +64,7 @@
break; break;
} }
return secondValue / 3; return secondValue / WorryDevision;
} }
internal void ThrowToMonkey(int worry) internal void ThrowToMonkey(int worry)

View File

@ -1,137 +1,55 @@
addx 1 Monkey 0:
noop Starting items: 66, 71, 94
addx 2 Operation: new = old * 5
addx 5 Test: divisible by 3
addx 3 If true: throw to monkey 7
noop If false: throw to monkey 4
addx -1
addx 5 Monkey 1:
noop Starting items: 70
noop Operation: new = old + 6
addx 5 Test: divisible by 17
noop If true: throw to monkey 3
addx 3 If false: throw to monkey 0
noop
addx 6 Monkey 2:
addx -4 Starting items: 62, 68, 56, 65, 94, 78
noop Operation: new = old + 5
noop Test: divisible by 2
addx 5 If true: throw to monkey 3
noop If false: throw to monkey 1
addx -32
addx 35 Monkey 3:
addx 5 Starting items: 89, 94, 94, 67
addx -31 Operation: new = old + 2
addx 7 Test: divisible by 19
addx -13 If true: throw to monkey 7
addx 2 If false: throw to monkey 0
addx 2
addx 5 Monkey 4:
addx 6 Starting items: 71, 61, 73, 65, 98, 98, 63
addx -5 Operation: new = old * 7
addx 2 Test: divisible by 11
addx 5 If true: throw to monkey 5
addx 2 If false: throw to monkey 6
addx 2
addx -17 Monkey 5:
addx 18 Starting items: 55, 62, 68, 61, 60
addx 5 Operation: new = old + 7
addx 2 Test: divisible by 5
addx -30 If true: throw to monkey 2
addx 31 If false: throw to monkey 1
addx 2
addx 2 Monkey 6:
addx -32 Starting items: 93, 91, 69, 64, 72, 89, 50, 71
addx -1 Operation: new = old + 1
addx 10 Test: divisible by 13
addx -8 If true: throw to monkey 5
noop If false: throw to monkey 2
noop
addx 6 Monkey 7:
addx 16 Starting items: 76, 50
noop Operation: new = old * old
addx -11 Test: divisible by 7
addx 3 If true: throw to monkey 4
addx -2 If false: throw to monkey 6
addx 3
noop
addx 6
noop
addx -2
noop
addx 7
addx 3
addx -2
addx 4
addx -20
noop
addx -14
addx -2
addx 6
addx 2
addx 3
noop
addx 2
addx -1
addx 4
noop
addx 5
noop
addx 2
addx 3
addx -2
addx 3
noop
addx 4
noop
addx 5
noop
addx 2
addx -24
addx -15
addx 17
addx -10
addx 2
addx -5
addx 6
noop
addx 3
addx 2
addx 2
noop
addx 3
addx 2
addx 5
addx -2
addx 3
addx 2
addx 2
addx 5
addx 2
addx -18
addx -19
noop
addx 1
addx 2
addx 5
addx 3
noop
noop
noop
noop
addx 2
addx 5
addx 2
addx 3
noop
addx -8
addx 11
noop
noop
addx 2
addx 5
addx 2
addx 3
noop
addx -34
noop

View File

@ -0,0 +1,16 @@
namespace AdventOfCodeLibrary._2022
{
using AdventOfCodeLibrary.Shared;
public class Day12Part1 : Answerable
{
public override int Year { get; set; } = 2022;
public override int Day { get; set; } = 12;
public override int Part { get; set; } = 1;
public override string GetAnswer(byte[] data)
{
return string.Empty;
}
}
}

View File

@ -0,0 +1,16 @@
namespace AdventOfCodeLibrary._2022
{
using AdventOfCodeLibrary.Shared;
public class Day12Part2 : Answerable
{
public override int Year { get; set; } = 2022;
public override int Day { get; set; } = 12;
public override int Part { get; set; } = 2;
public override string GetAnswer(byte[] data)
{
return string.Empty;
}
}
}

View File

@ -0,0 +1,55 @@
Monkey 0:
Starting items: 66, 71, 94
Operation: new = old * 5
Test: divisible by 3
If true: throw to monkey 7
If false: throw to monkey 4
Monkey 1:
Starting items: 70
Operation: new = old + 6
Test: divisible by 17
If true: throw to monkey 3
If false: throw to monkey 0
Monkey 2:
Starting items: 62, 68, 56, 65, 94, 78
Operation: new = old + 5
Test: divisible by 2
If true: throw to monkey 3
If false: throw to monkey 1
Monkey 3:
Starting items: 89, 94, 94, 67
Operation: new = old + 2
Test: divisible by 19
If true: throw to monkey 7
If false: throw to monkey 0
Monkey 4:
Starting items: 71, 61, 73, 65, 98, 98, 63
Operation: new = old * 7
Test: divisible by 11
If true: throw to monkey 5
If false: throw to monkey 6
Monkey 5:
Starting items: 55, 62, 68, 61, 60
Operation: new = old + 7
Test: divisible by 5
If true: throw to monkey 2
If false: throw to monkey 1
Monkey 6:
Starting items: 93, 91, 69, 64, 72, 89, 50, 71
Operation: new = old + 1
Test: divisible by 13
If true: throw to monkey 5
If false: throw to monkey 2
Monkey 7:
Starting items: 76, 50
Operation: new = old * old
Test: divisible by 7
If true: throw to monkey 4
If false: throw to monkey 6

View File

@ -14,13 +14,13 @@ namespace AdventOfCodeLibrary.Shared
public string DefaultInputFile => $"../../../../Advent Of Code Library/{Year}/Day {Day:00}/day-{Day:00}-input.txt"; public string DefaultInputFile => $"../../../../Advent Of Code Library/{Year}/Day {Day:00}/day-{Day:00}-input.txt";
protected static string NewLine => "\n"; protected static string NewLine => Environment.NewLine;
public abstract string GetAnswer(byte[] data); public abstract string GetAnswer(byte[] data);
internal static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes); internal static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes).ReplaceLineEndings();
internal static string[] GetAsStringArray(byte[] bytes) => Encoding.UTF8.GetString(bytes).Split(NewLine).Select(StripControlChars).ToArray(); internal static string[] GetAsStringArray(byte[] bytes) => GetAsString(bytes).Split(NewLine).ToArray();
private static string StripControlChars(string s) private static string StripControlChars(string s)
{ {