-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.ps1
More file actions
59 lines (52 loc) · 1.39 KB
/
day8.ps1
File metadata and controls
59 lines (52 loc) · 1.39 KB
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
. .\day8input.ps1
$quit = $false
$changeIndex=0
while( -not $quit)
{
$ops = $source.Clone()
while( (-not $ops[$changeIndex].Contains( "nop" )) -and ( -not $ops[$changeIndex].Contains( "jmp") ))
{
$changeIndex+=1
}
if($ops[$changeIndex].Contains("nop"))
{
$ops[$changeIndex] = $ops[$changeIndex].Replace("nop","jmp")
}
elseif($ops[$changeIndex].Contains("jmp"))
{
$ops[$changeIndex] = $ops[$changeIndex].Replace("jmp","nop")
}
$changeIndex+=1
$acc = 0
$index = 0
$done = @{}
while($true)
{
if($index -ge $ops.Count){
$quit = $true
break
}
if($done.ContainsKey($index)){
break
}
$done.Add($index, 1)
$parts = $ops[$index].Split(' ')
$action = $parts[0]
switch($action)
{
"nop"{ $index += 1 }
"acc"{
$chars = $parts[1].ToCharArray()
if($chars[0] -eq '+'){ $acc += $parts[1].SubString(1) -as [int] }
else{ $acc -= $parts[1].SubString(1) -as [int] }
$index += 1
}
"jmp"{
$chars = $parts[1].ToCharArray()
if($chars[0] -eq '+'){ $index += $parts[1].SubString(1) -as [int] }
else{ $index -= $parts[1].SubString(1) -as [int] }
}
}
}
}
$acc