I think that has been an outlier so far, its good to have one that makes you re-evaluate your solution once in a while.
Definitely a time sink though!
C#
Not too bad - I just scored every hand for the first part so I could easily sort it.
For the second part I just brute forced the replacements for the hand type matchinge (first digit of score)
Task1
public class Day7Task1:IRunnable
{
public static Dictionary CardValues = new Dictionary()
{
{ '2', "01" },
{ '3', "02" },
{ '4', "03" },
{ '5', "04" },
{ '6', "05" },
{ '7', "06" },
{ '8', "07" },
{ '9', "08" },
{ 'T', "09" },
{ 'J', "10" },
{ 'Q', "11" },
{ 'K', "12" },
{ 'A', "13" }
};
public void Run()
{
//var inputLines = File.ReadAllLines("Days/Seven/Day7ExampleInput.txt");
var inputLines = File.ReadAllLines("Days/Seven/Day7Input.txt");
var hands = inputLines.Select(line =>
{
var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
return new Hand(split[0], split[1] );
}).ToList();
var sortedHands = hands.OrderBy(hand => hand.Score).ToList();
long resultValue = 0;
for (int i = 1; i < hands.Count()+1; i++)
{
resultValue += i * sortedHands[i-1].Bid;
}
Console.WriteLine("Result:" + resultValue);
}
public class Hand
{
public Hand(string cards, string bid)
{
Cards = cards;
Bid = int.Parse(bid);
Score = GenerateScore();
}
public string Cards { get; set; }
public int Bid { get; set; }
public long Score { get; }
private long GenerateScore()
{
var resultString = new StringBuilder();
var cardGroups = Cards.GroupBy(c => c).ToList();
var groupCounts = cardGroups.OrderByDescending(g => g.Count()).Select(g => g.Count()).ToList();
if (cardGroups.Count() == 1)
{
resultString.Append("7");
}
else if(cardGroups.Count() == 2 && (cardGroups[0].Count() == 4 || cardGroups[0].Count() == 1))
{
resultString.Append("6");
}
else if(cardGroups.Count() == 2 && (cardGroups[0].Count() == 3 || cardGroups[0].Count() == 2))
{
resultString.Append("5");
}
else if(cardGroups.Count() == 3 && (cardGroups[0].Count() == 3 || cardGroups[1].Count() == 3 || cardGroups[2].Count() == 3))
{
resultString.Append("4");
}
else if(cardGroups.Count() == 3 && groupCounts[0] == 2 && groupCounts[1] == 2 && groupCounts[2] == 1)
{
resultString.Append("3");
}
else if(cardGroups.Count() == 4 )
{
resultString.Append("2");
}
else
{
resultString.Append("1");
}
foreach (var card in Cards)
{
resultString.Append(Day7Task1.CardValues[card]);
}
Console.WriteLine("Cards:{0} Score:{1}",Cards,resultString);
return long.Parse(resultString.ToString());
}
}
}
Task2
public class Day7Task2:IRunnable
{
public static Dictionary CardValues = new Dictionary()
{
{ '2', "01" },
{ '3', "02" },
{ '4', "03" },
{ '5', "04" },
{ '6', "05" },
{ '7', "06" },
{ '8', "07" },
{ '9', "08" },
{ 'T', "09" },
{ 'J', "00" },
{ 'Q', "11" },
{ 'K', "12" },
{ 'A', "13" }
};
public void Run()
{
//var inputLines = File.ReadAllLines("Days/Seven/Day7ExampleInput.txt");
var inputLines = File.ReadAllLines("Days/Seven/Day7Input.txt");
var hands = inputLines.Select(line =>
{
var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
return new Hand(split[0], split[1] );
}).ToList();
var sortedHands = hands.OrderBy(hand => hand.Score).ToList();
long resultValue = 0;
for (int i = 1; i < hands.Count()+1; i++)
{
resultValue += i * sortedHands[i-1].Bid;
}
Console.WriteLine("Result:" + resultValue);
}
public class Hand
{
public Hand(string cards, string bid)
{
Cards = cards;
Bid = int.Parse(bid);
Score = GenerateScore();
}
public string Cards { get; set; }
public int Bid { get; set; }
public long Score { get; }
private long GenerateScore()
{
var generateFirstDigit = new Func(cards =>
{
var cardGroups = cards.GroupBy(c => c).ToList();
var groupCounts = cardGroups.OrderByDescending(g => g.Count()).Select(g => g.Count()).ToList();
if (cardGroups.Count() == 1)
{
return 7;
}
else if (cardGroups.Count() == 2 && (cardGroups[0].Count() == 4 || cardGroups[0].Count() == 1))
{
return 6;
}
else if (cardGroups.Count() == 2 && (cardGroups[0].Count() == 3 || cardGroups[0].Count() == 2))
{
return 5;
}
else if (cardGroups.Count() == 3 && (cardGroups[0].Count() == 3 || cardGroups[1].Count() == 3 || cardGroups[2].Count() == 3))
{
return 4;
}
else if (cardGroups.Count() == 3 && groupCounts[0] == 2 && groupCounts[1] == 2 && groupCounts[2] == 1)
{
return 3;
}
else if (cardGroups.Count() == 4)
{
return 2;
}
else
{
return 1;
}
});
var resultString = new StringBuilder();
var maxFistDigit = Day7Task2.CardValues.Keys.Select(card => generateFirstDigit(Cards.Replace('J', card))).Max();
resultString.Append(maxFistDigit);
foreach (var card in Cards)
{
resultString.Append(Day7Task2.CardValues[card]);
}
Console.WriteLine("Cards:{0} Score:{1}",Cards,resultString);
return long.Parse(resultString.ToString());
}
}
}
on my first attempt my computer couldn't run Spotify at the same time because it was paging so hard >.<
final go was in the order of 20 seconds or so I think (with practically 0 memory usage)
Even mint forces you to do major version upgrades, and eventually something breaks enough that I have to do a clean install anyway.
I imagine replacing the battery in your light switch in the dark (because you can't turn the lights on) is probably rather annoying. This sounds like a cool idea.
But you have control of the network with a majority of mining right? So it's very possible that one or more organisations could control it for long enough that it's not trusted?
And how does proof of stake work cryptography?
The historic high salary for COBOL Devs etc is also partially due to them mostly being old and extremely experienced senior devs
OK.
.
.
.
I'm doing them because it's an excuse to do some interesting challenges however you want and have people to talk about them with.
Edit: Sorry, should have read your code first, you made it work too. if it works it works, Recursive solutions just click for me over other solutions.
I made the recursion work, went to a depth of 24 for my input set.
Recursive C#
internal class Day4Task2 : IRunnable
{
private Regex _regex = new Regex("Card\\s*\\d*: ([\\d\\s]{2} )*\\|( [\\d\\s]{2})*");
private Dictionary _matchCountCache = new Dictionary();
private int _maxDepth = 0;
public void Run()
{
var inputLines = File.ReadAllLines("Days/Four/Day4Input.txt");
int sumScore = 0;
for (int i = 0; i < inputLines.Length; i++)
{
sumScore += ScoreCard(i, inputLines, 0);
Console.WriteLine("!!!" + i + "!!!");
}
Console.WriteLine("Sum:"+sumScore.ToString());
Console.WriteLine("Max Recursion Depth:"+ _maxDepth.ToString());
}
private int ScoreCard(int lineId, string[] inputLines, int depth)
{
if( depth > _maxDepth )
{
_maxDepth = depth;
}
if(lineId >= inputLines.Length)
{
return 0;
}
int matchCount = 0;
if (!_matchCountCache.ContainsKey(lineId)) {
var winningSet = new HashSet();
var matches = _regex.Match(inputLines[lineId]);
foreach (Capture capture in matches.Groups[1].Captures)
{
winningSet.Add(capture.Value.Trim());
}
foreach (Capture capture in matches.Groups[2].Captures)
{
if (winningSet.Contains(capture.Value.Trim()))
{
matchCount++;
}
}
_matchCountCache[lineId] = matchCount;
}
matchCount = _matchCountCache[lineId];
int totalCards = 1;
while(matchCount > 0)
{
totalCards += ScoreCard(lineId+matchCount, inputLines, depth+1);
matchCount--;
}
//Console.WriteLine("Finished processing id: " + lineId + " Sum is: " + totalCards);
return totalCards;
}
}
It's a good thing I killed my CPU attempt after 2 hours then!
I would have definitely died in that sandstorm!