Skip to content

Instantly share code, notes, and snippets.

@Stevie-O
Last active December 1, 2024 21:22
Show Gist options
  • Save Stevie-O/c8c4f38c41ddc8023826e58ad9e6ba82 to your computer and use it in GitHub Desktop.
Save Stevie-O/c8c4f38c41ddc8023826e58ad9e6ba82 to your computer and use it in GitHub Desktop.
#load "../common/aoc-input-util.linq"
#load "../common/aoc-parsers.linq"
void Main()
{
using (var tr =
//OpenDataFile()
GetSampleInput()
)
{
var num_scores = new Dictionary<int, (int left_count, int right_count)>();
var total_score = 0;
// does exactly what you think it does
var input = RegexInputParser(tr, new Regex(@"^(\d+)\s+(\d+)$"),
m => (loc0: int.Parse(m.Groups[1].Value), loc1: int.Parse(m.Groups[2].Value))
);
foreach (var (col1, col2) in input)
{
// add column 1
if (!num_scores.TryGetValue(col1, out var col1_counts))
{
// col1 has not appeared yet in either column
// therefore, its contribution to the final answer is ZERO
num_scores[col1] = (1, 0);
}
else
{
num_scores[col1] = (col1_counts.left_count + 1, col1_counts.right_count);
// this adds col1 to the total score
total_score += (col1 * col1_counts.right_count);
}
if (!num_scores.TryGetValue(col2, out var col2_counts))
{
// col2 has not appeared yet in either column
// therefore, its contribution to the final answer is ZERO
num_scores[col2] = (0, 1);
}
else
{
num_scores[col2] = (col2_counts.left_count, col2_counts.right_count + 1);
total_score += col2 * col2_counts.left_count;
}
Console.WriteLine("after ({0}, {1}) total score is {2}", col1, col2, total_score);
}
total_score.Dump();
}
}
const string EXAMPLE_1 = @"
3 4
4 3
2 5
1 3
3 9
3 3
";
TextReader GetSampleInput()
{
return new StringReader(EXAMPLE_1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment