Skip to content

Commit

Permalink
Day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Feb 7, 2024
1 parent 5750e06 commit f48d30d
Show file tree
Hide file tree
Showing 23 changed files with 631 additions and 0 deletions.
12 changes: 12 additions & 0 deletions AdventOfCode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day19_1", "Day19_1\Day19_1.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day19_2", "Day19_2\Day19_2.csproj", "{22D5E3FE-D797-4E36-981F-689CC04E4FCD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day20_1", "Day20_1\Day20_1.csproj", "{FF6A3C1A-CF4C-4A4D-8919-33B50F479482}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day20_2", "Day20_2\Day20_2.csproj", "{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -243,6 +247,14 @@ Global
{22D5E3FE-D797-4E36-981F-689CC04E4FCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22D5E3FE-D797-4E36-981F-689CC04E4FCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22D5E3FE-D797-4E36-981F-689CC04E4FCD}.Release|Any CPU.Build.0 = Release|Any CPU
{FF6A3C1A-CF4C-4A4D-8919-33B50F479482}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF6A3C1A-CF4C-4A4D-8919-33B50F479482}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF6A3C1A-CF4C-4A4D-8919-33B50F479482}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF6A3C1A-CF4C-4A4D-8919-33B50F479482}.Release|Any CPU.Build.0 = Release|Any CPU
{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 16 additions & 0 deletions Day20_1/Broadcaster.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace PulsePropagation;

internal class Broadcaster : Module
{
public Broadcaster(Machine machine, string name, string[] outputs) : base(machine, name, outputs)
{
}

public override void ProcessPulse(string sourceModuleName, PulseType pulse)
{
foreach (var output in Outputs)
{
SendPulse(Name, output, pulse);
}
}
}
26 changes: 26 additions & 0 deletions Day20_1/Conjunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace PulsePropagation;

internal class Conjunction : Module
{
public Conjunction(Machine machine, string name, string[] outputs) : base(machine, name, outputs)
{
}

public Dictionary<string, PulseType> LastInputPulses { get; } = new();

public override void AddInput(string inputModule)
{
LastInputPulses.Add(inputModule, PulseType.Low);
base.AddInput(inputModule);
}

public override void ProcessPulse(string sourceModuleName, PulseType pulse)
{
LastInputPulses[sourceModuleName] = pulse;
var pulseToSend = LastInputPulses.All(p => p.Value == PulseType.High) ? PulseType.Low : PulseType.High;
foreach (var output in Outputs)
{
SendPulse(Name, output, pulseToSend);
}
}
}
24 changes: 24 additions & 0 deletions Day20_1/Day20_1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="input.txt" />
</ItemGroup>

<ItemGroup>
<Content Include="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Tools\Tools.csproj" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions Day20_1/FlipFlop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace PulsePropagation;

internal class FlipFlop : Module
{
private bool _isOn = false;

public FlipFlop(Machine machine, string name, string[] outputs) : base(machine, name, outputs)
{
}

public override void ProcessPulse(string sourceModuleName, PulseType pulse)
{
if (pulse == PulseType.High)
{
return;
}

_isOn = !_isOn;
foreach (var output in Outputs)
{
SendPulse(Name, output, _isOn ? PulseType.High : PulseType.Low);
}
}
}
67 changes: 67 additions & 0 deletions Day20_1/Machine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@


namespace PulsePropagation;

internal class Machine
{
private readonly Dictionary<string, Module> _modules = new();

private readonly Queue<(string sourceModuleName, string target, PulseType pulse)> _pulseQueue = new();

public long LowPulseCount { get; private set; }

public long HighPulseCount { get; private set; }

public Module this[string moduleName] => _modules[moduleName];

public void AddModule(Module module)
{
_modules.Add(module.Name, module);
}

public void SendPulse(string sourceModuleName, string target, PulseType pulse)
{
if (_modules.TryGetValue(target, out var module))
{
module.ProcessPulse(sourceModuleName, pulse);
}
}

internal void AddInputs()
{
foreach (var module in _modules.Values)
{
var outputs = module.Outputs;
foreach (var output in outputs)
{
if (_modules.TryGetValue(output, out var targetModule))
{
targetModule.AddInput(module.Name);
}
}
}
}

internal void EnqueuePulse(string sourceModuleName, string target, PulseType pulse)
{
if (pulse == PulseType.Low)
{
LowPulseCount++;
}
else
{
HighPulseCount++;
}

_pulseQueue.Enqueue((sourceModuleName, target, pulse));
}

internal void ProcessPulses()
{
while (_pulseQueue.Count > 0)
{
var (sourceModuleName, target, pulse) = _pulseQueue.Dequeue();
SendPulse(sourceModuleName, target, pulse);
}
}
}
28 changes: 28 additions & 0 deletions Day20_1/Module.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace PulsePropagation;

internal abstract class Module
{

protected readonly List<string> _inputs = new();
private readonly Machine _machine;

public Module(Machine machine, string name, string[] outputs)
{
_machine = machine;
Name = name;
Outputs = outputs;
}

protected void SendPulse(string sourceModuleName, string target, PulseType pulse)
{
_machine.EnqueuePulse(sourceModuleName, target, pulse);
}

public string Name { get; }

public string[] Outputs { get; }

public virtual void AddInput(string inputModule) => _inputs.Add(inputModule);

public abstract void ProcessPulse(string sourceModuleName, PulseType pulse);
}
37 changes: 37 additions & 0 deletions Day20_1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using PulsePropagation;

Machine machine = new Machine();
var input = File.ReadAllLines("input.txt");
foreach (var line in input)
{
var parts = line.Split(" -> ");
var designation = parts[0];
var outputs = parts[1].Split(", ");
if (designation == "broadcaster")
{
Broadcaster broadcaster = new Broadcaster(machine, designation, outputs);
machine.AddModule(broadcaster);
}
else
{
var name = designation.Substring(1);
Module module = designation[0] switch
{
'%' => new FlipFlop(machine,name, outputs),
'&' => new Conjunction(machine, name, outputs),
_ => throw new InvalidOperationException()
};
machine.AddModule(module);
}
}

machine.AddInputs();

for (int i = 0; i < 1000; i++)
{
machine.EnqueuePulse("button", "broadcaster", PulseType.Low);
machine.ProcessPulses();

}

Console.WriteLine(machine.LowPulseCount * machine.HighPulseCount);
7 changes: 7 additions & 0 deletions Day20_1/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"Run": {
"commandName": "Project"
}
}
}
7 changes: 7 additions & 0 deletions Day20_1/PulseType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PulsePropagation;

public enum PulseType
{
Low,
High
}
58 changes: 58 additions & 0 deletions Day20_1/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
%pr -> ql
&jg -> mg
&mg -> rx
%mq -> gz, nt
%db -> ff, dz
%dx -> zs, bm
%bd -> nt, lj
%qj -> hj
%xs -> zs, dx
%xd -> nt
%gb -> fx, th
&nt -> ds, hj, ht, rh, qj
%ht -> nt, vp
&rh -> mg
%sq -> th, cd
%tt -> pq
%dh -> sh
%rz -> zc
%cx -> xr, nt
%zq -> tt, th
&jm -> mg
%lj -> nt, cx
%mp -> ff, bq
%dz -> ff, gd
%fz -> bk, th
%hj -> mq
broadcaster -> gb, ht, vk, zz
%zc -> dh
%pj -> xs
%bn -> fz
%mr -> bf
%mj -> th, sq
%gg -> pj, zs
%sh -> mr, zs
%bf -> zs, gg
&hf -> mg
%bm -> zs
%bk -> zg
%pq -> th, mj
%xf -> ff, db
&th -> bn, gb, tt, hf, bk
%fx -> th, bn
&ff -> vd, bq, pr, vk, ql, jm
%xr -> nt, xd
%bq -> pr
%zz -> rz, zs
%gz -> nt, ds
&zs -> mr, pj, zz, dh, jg, zc, rz
%vd -> xf
%vk -> mp, ff
%cv -> ff
%cd -> th
%zg -> th, zq
%gd -> ff, cv
%ql -> lt
%lt -> ff, vd
%ds -> bd
%vp -> nt, qj
Empty file added Day20_1/output.txt
Empty file.
16 changes: 16 additions & 0 deletions Day20_2/Broadcaster.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace PulsePropagation;

internal class Broadcaster : Module
{
public Broadcaster(Machine machine, string name, string[] outputs) : base(machine, name, outputs)
{
}

public override void ProcessPulse(string sourceModuleName, PulseType pulse)
{
foreach (var output in Outputs)
{
SendPulse(Name, output, pulse);
}
}
}
33 changes: 33 additions & 0 deletions Day20_2/Conjunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace PulsePropagation;

internal class Conjunction : Module
{
public Conjunction(Machine machine, string name, string[] outputs) : base(machine, name, outputs)
{
}

public Dictionary<string, PulseType> LastInputPulses { get; } = new();

public Dictionary<string, int> HighPulses { get; } = new();

public override void AddInput(string inputModule)
{
LastInputPulses.Add(inputModule, PulseType.Low);
base.AddInput(inputModule);
}

public override void ProcessPulse(string sourceModuleName, PulseType pulse)
{
LastInputPulses[sourceModuleName] = pulse;
if (pulse == PulseType.High && !HighPulses.ContainsKey(sourceModuleName))
{
HighPulses[sourceModuleName] = _machine.ButtonPressCount;
}

var pulseToSend = LastInputPulses.All(p => p.Value == PulseType.High) ? PulseType.Low : PulseType.High;
foreach (var output in Outputs)
{
SendPulse(Name, output, pulseToSend);
}
}
}
Loading

0 comments on commit f48d30d

Please sign in to comment.