Advent-of-Code-2019-Day-4

  題目:Advent of Code 2019 Day 4
  使用語言:C#

  
  第四天的題目讓我卡超久……我覺得一定是題目敘述的問題不是我英文太差的問題(?!)。
  簡單來說就是給定一些規則推算所有可能的密碼,算出會有幾組可能的密碼就可以了。這題我除了暴力法還真的想不到其他比較聰明的解法,所以就直接暴力法處理了。
  卡最久的是Part2,一開始看題目理解成是只要有數字相鄰重複超過三個就不行,範例給的123444因為4重複了三次,所以就是不合規則的密碼。
  但不管怎麼算就是差一點,後來還是靠Google看大家的討論才知道,如果只有一個相鄰數字是不超過兩個也可以,譬如說112444,這個算合法的密碼。知道這個之後馬上就改完算對了……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static int _lagerDitgit = 0;
public static List<DigitPostion> _repeatDigits = new List<DigitPostion>();

public static void Main()
{
var minPassword = 134792;
var maxPassword = 675810;
var result = new List<int>();

for(int i = minPassword;i<=maxPassword;i++)
{
var checkValue = i.ToString();
_lagerDitgit = 0;
_repeatDigits = new List<DigitPostion>();

// Check Six-digit
if(!checkValue.Length.Equals(6)) continue;

// Check Rule
if(!CheckRule(checkValue)) continue;

result.Add(i);
}

Console.WriteLine(result.Count);
}

public static bool CheckRule(string value)
{
var result = false;

for(int i = 0;i<value.Length - 1; i++)
{
var rule1 = false;
var rule2 = false;

var firstDigit = value.Substring(i, 1);
var secondDigit = value.Substring(i+1, 1);


rule1 = CheckTwoAdjacentDigitsAreTheSame(firstDigit, secondDigit);

rule2 = CheckNeverDecrease(firstDigit, secondDigit);

if(rule1)
{
result = true;

var tmp = new DigitPostion();
tmp.digit = Convert.ToInt32(firstDigit);
tmp.pos = i;
_repeatDigits.Add(tmp);
tmp = new DigitPostion();
tmp.digit = Convert.ToInt32(secondDigit);
tmp.pos = i + 1;
_repeatDigits.Add(tmp);
}

if(!rule2)
{
result = false;
break;
}
}

if(result)
{
// 取得所有相鄰有重複的數字
var ruleJudgeDigit = _repeatDigits.Select(x=>x.digit).Distinct().ToList();

foreach(var digit in ruleJudgeDigit)
{
// 判斷相鄰數字是否超過2個
var ruleJudgeData = _repeatDigits.Where(x=>x.digit.Equals(digit)).Distinct().ToList();

int tempPos = 0;
int count = 0;

for(int i = 0; i < ruleJudgeData.Count; i++)
{
if(tempPos == (ruleJudgeData[i]).pos - 1 && (ruleJudgeData[i]).pos != tempPos)
count++;
else
tempPos = (ruleJudgeData[i]).pos;
}

if(count <= 1)
{
// 只要有一個相鄰數字是不超過兩個就可以 e.g. 112444
result = true;
break;
}

result = false;
}
}

return result;
}

public static bool CheckTwoAdjacentDigitsAreTheSame(string digit1, string digit2)
{
if(!digit1.Equals(digit2)) return false;

if(Convert.ToInt32(digit1) > _lagerDitgit) _lagerDitgit = Convert.ToInt32(digit1);

return true;
}

public static bool CheckNeverDecrease(string digit1, string digit2)
{
return (Convert.ToInt32(digit1) <= Convert.ToInt32(digit2));
}

public class DigitPostion
{
public int digit {get;set;}
public int pos {get;set;}
}
}
Advent-of-Code-2019-Day-5 Advent-of-Code-2019-Day-3

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×