mirror of
https://github.com/ckaczor/Advent2019.git
synced 2026-01-13 17:22:15 -05:00
Day 4
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day1/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day2/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day3/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day3/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day4/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
63
Day4/Day4.cs
Normal file
63
Day4/Day4.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Advent
|
||||
{
|
||||
public static class Day4
|
||||
{
|
||||
public static void Execute()
|
||||
{
|
||||
//Console.WriteLine(IsValidPassword("112233"));
|
||||
//Console.WriteLine(IsValidPassword("123444"));
|
||||
//Console.WriteLine(IsValidPassword("111122"));
|
||||
|
||||
var count = 0;
|
||||
|
||||
for (var i = 109165; i <= 576723; i++)
|
||||
{
|
||||
if (IsValidPassword(i.ToString()))
|
||||
count++;
|
||||
}
|
||||
|
||||
Console.WriteLine(count);
|
||||
}
|
||||
|
||||
private static bool IsValidPassword(string password)
|
||||
{
|
||||
var digits = password.ToCharArray();
|
||||
|
||||
var sortedDigits = password.ToCharArray().OrderBy(c => c);
|
||||
|
||||
if (!digits.SequenceEqual(sortedDigits))
|
||||
return false;
|
||||
|
||||
var hasDouble = false;
|
||||
var index = 0;
|
||||
var letter = digits[0];
|
||||
|
||||
while (index < 6)
|
||||
{
|
||||
var count = 1;
|
||||
|
||||
while (index < 5 && digits[index + 1] == letter)
|
||||
{
|
||||
count++;
|
||||
index++;
|
||||
}
|
||||
|
||||
if (count == 2)
|
||||
{
|
||||
hasDouble = true;
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
|
||||
if (index < 6)
|
||||
letter = digits[index];
|
||||
}
|
||||
|
||||
return hasDouble;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,8 @@
|
||||
{
|
||||
//Day1.Execute();
|
||||
//Day2.Execute();
|
||||
Day3.Execute();
|
||||
//Day3.Execute();
|
||||
Day4.Execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user