Skip to content

Instantly share code, notes, and snippets.

@gingemonster
Last active December 4, 2016 18:55
Show Gist options
  • Save gingemonster/8a27d120462c27a63486245fc070b11c to your computer and use it in GitHub Desktop.
Save gingemonster/8a27d120462c27a63486245fc070b11c to your computer and use it in GitHub Desktop.
Adventofcode Day 4 - Challenge 1
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
using(var input = File.OpenText("input.txt")){
string line;
var sum = 0;
while((line = input.ReadLine()) != null)
{
var room = new Room(line);
if(room.IsValidRoomName()){
sum += room.sectorid;
}
}
Console.WriteLine(sum);
}
}
}
public class Room{
public Room(string encryptedname){
this.parseEncryptedName(encryptedname);
}
public string name{
get;
set;
}
public int sectorid{
get;
set;
}
public string checksum{
get;
set;
}
public bool IsValidRoomName(){
return this.checksum == this.CalculateCheckSum();
}
private string CalculateCheckSum(){
return String.Join("",this.GetGroupsOfLetters(this.name));
}
private char[] GetGroupsOfLetters(string input){
var groups = input.GroupBy(c => c).OrderByDescending(g => g.Count()).ThenBy(g => g.Key).Select(g=>g.Key).Take(5).ToArray();
return groups;
}
private void parseEncryptedName(string encryptedname){
var pattern = new Regex(@"(.*)\-(\d*)\[(\w*)\]");
var match = pattern.Match(encryptedname);
name = match.Groups[1].Value.Replace("-","");
sectorid = Int32.Parse(match.Groups[2].Value);
checksum = match.Groups[3].Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment