Day 11 part 1

This commit is contained in:
2022-12-12 23:10:17 -05:00
parent 2d0f5c1937
commit c1196e8dc4
5 changed files with 224 additions and 0 deletions

View File

@@ -26,6 +26,8 @@
<None Remove="Problems\AOC2022\Day10\sara.txt" /> <None Remove="Problems\AOC2022\Day10\sara.txt" />
<None Remove="Problems\AOC2022\Day10\simple.txt" /> <None Remove="Problems\AOC2022\Day10\simple.txt" />
<None Remove="Problems\AOC2022\Day10\test.txt" /> <None Remove="Problems\AOC2022\Day10\test.txt" />
<None Remove="Problems\AOC2022\Day11\input.txt" />
<None Remove="Problems\AOC2022\Day11\test.txt" />
<None Remove="Problems\AOC2022\Day3\input.txt" /> <None Remove="Problems\AOC2022\Day3\input.txt" />
<None Remove="Problems\AOC2022\Day3\test.txt" /> <None Remove="Problems\AOC2022\Day3\test.txt" />
<None Remove="Problems\AOC2022\Day4\input.txt" /> <None Remove="Problems\AOC2022\Day4\input.txt" />

View File

@@ -0,0 +1,73 @@
using System.Numerics;
namespace AdventOfCode.Problems.AOC2022.Day11;
internal class Monkey
{
public int MonkeyNumber { get; set; }
public int InspectionCount { get; private set; }
public List<BigInteger> Items { get; set; }
private MonkeyOperator _operator;
private uint _operand;
private uint _divisor;
private int _trueTarget;
private int _falseTarget;
public Monkey(string[] lines)
{
Items = new List<BigInteger>();
MonkeyNumber = int.Parse(lines[0].Split(' ')[^1][0..^1]);
Items = lines[1].Split(": ")[^1].Split(", ").Select(v => BigInteger.Parse(v)).ToList();
var operation = lines[2].Split("= old ")[^1];
_operator = operation[0] switch
{
'*' => MonkeyOperator.Multiply,
'+' => MonkeyOperator.Add,
_ => MonkeyOperator.Add
};
if (!uint.TryParse(operation[1..], out _operand))
_operator = MonkeyOperator.Power;
_divisor = uint.Parse(lines[3].Split(' ')[^1]);
_trueTarget = int.Parse(lines[4].Split(' ')[^1]);
_falseTarget = int.Parse(lines[5].Split(' ')[^1]);
}
public BigInteger Inspect(BigInteger value, uint worryOffset = 3)
{
InspectionCount++;
value = Operate(value);
if(worryOffset != 0)
value /= worryOffset;
return value;
}
public int GetThrowTarget(BigInteger value)
{
return value % _divisor == 0 ? _trueTarget : _falseTarget;
}
private BigInteger Operate(BigInteger value)
{
return _operator switch
{
MonkeyOperator.Multiply => value * _operand,
MonkeyOperator.Add => value + _operand,
MonkeyOperator.Power => value * value,
_ => value
};
}
private enum MonkeyOperator
{
Multiply,
Add,
Power
}
public override string ToString()
{
return $"{MonkeyNumber}: ({InspectionCount}) [{string.Join(", ",Items)}]";
}
}

View File

@@ -0,0 +1,67 @@
using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day11;
[ProblemInfo(2022, 11, "Monkey in the Middle")]
internal class MonkeyInTheMiddle : Problem<int, int>
{
private Monkey[] _monkeysPart1 = Array.Empty<Monkey>();
private Monkey[] _monkeysPart2 = Array.Empty<Monkey>();
public override void CalculatePart1()
{
Simulate(_monkeysPart1, 20);
Part1 = _monkeysPart1.OrderByDescending(m => m.InspectionCount)
.Take(2)
.Select(m => m.InspectionCount)
.Aggregate((a, b) => a * b);
}
public override void CalculatePart2()
{
Simulate(_monkeysPart2, 10000, 0);
Part2 = _monkeysPart2.OrderByDescending(m => m.InspectionCount)
.Take(2)
.Select(m => m.InspectionCount)
.Aggregate((a, b) => a * b);
}
public override void LoadInput()
{
var lines = ReadInputLines("test.txt").Chunk(7);
_monkeysPart1 = lines.Select(ln => new Monkey(ln)).ToArray();
_monkeysPart2 = lines.Select(ln => new Monkey(ln)).ToArray();
}
private static void Simulate(Monkey[] monkeys, int rounds, uint worry = 3)
{
for (int i = 0; i < rounds; i++)
{
SimulateRound(monkeys, worry);
}
}
private static void SimulateRound(Monkey[] monkeys, uint worry = 3)
{
foreach (var monkey in monkeys)
SimulateTurn(monkey, monkeys, worry);
}
private static void SimulateTurn(Monkey monkey, Monkey[] monkeys, uint worry = 3) {
for (int i = 0; i < monkey.Items.Count; i++)
{
var item = monkey.Inspect(monkey.Items[i], worry);
var target = monkey.GetThrowTarget(item);
monkeys[target].Items.Add(item);
}
monkey.Items.Clear();
}
}

View File

@@ -0,0 +1,55 @@
Monkey 0:
Starting items: 80
Operation: new = old * 5
Test: divisible by 2
If true: throw to monkey 4
If false: throw to monkey 3
Monkey 1:
Starting items: 75, 83, 74
Operation: new = old + 7
Test: divisible by 7
If true: throw to monkey 5
If false: throw to monkey 6
Monkey 2:
Starting items: 86, 67, 61, 96, 52, 63, 73
Operation: new = old + 5
Test: divisible by 3
If true: throw to monkey 7
If false: throw to monkey 0
Monkey 3:
Starting items: 85, 83, 55, 85, 57, 70, 85, 52
Operation: new = old + 8
Test: divisible by 17
If true: throw to monkey 1
If false: throw to monkey 5
Monkey 4:
Starting items: 67, 75, 91, 72, 89
Operation: new = old + 4
Test: divisible by 11
If true: throw to monkey 3
If false: throw to monkey 1
Monkey 5:
Starting items: 66, 64, 68, 92, 68, 77
Operation: new = old * 2
Test: divisible by 19
If true: throw to monkey 6
If false: throw to monkey 2
Monkey 6:
Starting items: 97, 94, 79, 88
Operation: new = old * old
Test: divisible by 5
If true: throw to monkey 2
If false: throw to monkey 7
Monkey 7:
Starting items: 77, 85
Operation: new = old + 6
Test: divisible by 13
If true: throw to monkey 4
If false: throw to monkey 0

View File

@@ -0,0 +1,27 @@
Monkey 0:
Starting items: 79, 98
Operation: new = old * 19
Test: divisible by 23
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1