@@ -0,0 +1,18 @@
+using System.Text;
+
+namespace Day15;
+public static class AsciiHash
+{
+ public static byte Hash(string input)
+ {
+ byte result = 0;
+ foreach (var b in Encoding.ASCII.GetBytes(input))
+ result += b;
+ result = (byte)((result * 17) % 256);
+ }
+ return result;
+}
@@ -0,0 +1,9 @@
+namespace Day15;
+public record InitializationStep(string Instruction)
+ public byte GetHash()
+ return AsciiHash.Hash(Instruction);
@@ -7,13 +7,12 @@ public partial class Parser
[GeneratedRegex(@".*")]
private partial Regex LineMatch();
- public void Parse(string inputFile)
+ public IEnumerable<InitializationStep> Parse(string inputFile)
{
- using var reader = File.OpenText(inputFile);
-
- while (!reader.EndOfStream)
+ var text = File.ReadAllText(inputFile);
+ foreach (var step in text.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries))
- var line = reader.ReadLine()!;
+ yield return new InitializationStep(step);
}
@@ -16,5 +16,17 @@ if (args.Length < 1)
var inputFile = args[0];
var parser = new Parser();
+var steps = parser.Parse(inputFile);
+var sum = 0;
+foreach (var s in steps)
+ var hash = s.GetHash();
+ Console.WriteLine(hash);
+ sum += hash;
+Console.WriteLine();
+Console.WriteLine($"Total: {sum}");
return 0;
@@ -0,0 +1 @@
+rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7