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
| class KnightAviableMove{
[int]$Id
[int[]]$Rows
[int[]]$Columns
[int]$Shift
KnightAviableMove ([int]$Id, [int[]]$Rows, [int[]]$Columns, [int]$Shift){
$this.Id = $id
$this.Rows = $Rows
$this.Columns = $Columns
$this.Shift = $Shift
}
}
class Edge
{
[int]$Node0
[int]$Node1
[nullable[bool]]$Direction
Edge ([int]$Node0, [int]$Node1,[nullable[bool]]$Direction){
$this.Node0 = $Node0
$this.Node1 = $Node1
$this.Direction = $Direction
}
}
class KnightMoveGraph
{
[int[]]$Nodes
[KnightAviableMove[]]$KnightAviableMoves
[Edge[]]$Edges
KnightMoveGraph(){
$this.nodes = 0..63
$this.KnightAviableMoves +=, [KnightAviableMove]::New(1, 0..5, 0..6, 17)
$this.KnightAviableMoves +=, [KnightAviableMove]::New(2, 0..6, 0..5, 10)
$this.KnightAviableMoves +=, [KnightAviableMove]::New(3, 1..7, 0..5, -6)
$this.KnightAviableMoves +=, [KnightAviableMove]::New(4, 2..7, 0..6, -15)
$this.KnightAviableMoves +=, [KnightAviableMove]::New(5, 2..7, 1..7, -17)
$this.KnightAviableMoves +=, [KnightAviableMove]::New(6, 1..7, 2..7, -10)
$this.KnightAviableMoves +=, [KnightAviableMove]::New(7, 0..6, 2..7, 6)
$this.KnightAviableMoves +=, [KnightAviableMove]::New(8, 0..5, 1..7, 15)
$this.FillEdges()
}
[void] print(){
$this.Nodes | Group-Object {[Math]::Truncate($_/$this.boardSize)} | ForEach-Object {
(($_.Gruup | ForEach-Object {
$_
}) -join ' ') | Write-Host
}
}
[void] FillEdges(){
foreach ($node in $this.nodes){
foreach ($KnightAviableMove in $This.KnightAviableMoves){
if ($KnightAviableMove.Columns -contains ($node%8) -and $KnightAviableMove.Rows -contains [math]::Truncate($node/8)){
$this.Edges +=, [Edge]::New($node, ($node + $KnightAviableMove.Shift), 1)
}
}
}
}
static [int]StringAddressToNode ([String]$Address){
[string]$column, [int]$row = $Address.ToCharArray()
$LetterToInt = @{
a = 0
b = 1
c = 2
d = 3
e = 4
f = 5
g = 6
h = 7
}
return $LetterToInt[$column] + ($row-49)*8
}
[String]Check([String]$Move){
if (-not (([Regex]'[A-Ha-h][1-8]-[A-Ha-h][1-8]').Match($Move)).Success){
return "ERROR"
}
$cell0, $cell1 = $move -split "-"
return ('NO','YES')[($this.Edges | Where-Object {$_.Node0 -eq [KnightMoveGraph]::StringAddressToNode($cell0) -and $_.Node1 -eq [KnightMoveGraph]::StringAddressToNode($cell1)}).Count]
}
}
class problem_006 {
static solve ([String]$inputFileName, [string]$outputFileName){
$instanceOfKnightMoveGraph = [KnightMoveGraph]::New()
$instanceOfKnightMoveGraph.Check((Get-Content $inputFileName)) |
Out-File $outputFileName
}
}
[problem_006]::solve('C:\PROBLEMS\006\INPUT.TXT', 'C:\PROBLEMS\006\OUTPUT.TXT') |