generated from MartinZikmund/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5750e06
commit f48d30d
Showing
23 changed files
with
631 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"profiles": { | ||
"Run": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace PulsePropagation; | ||
|
||
public enum PulseType | ||
{ | ||
Low, | ||
High | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.