-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.ps1
102 lines (64 loc) · 2.43 KB
/
day10.ps1
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
# Day 10: Cathode-Ray Tube
$in = Get-Content input.txt
$signals = new-object "system.collections.arraylist"
$x = 1
$cyclecount = 0
$markers = @(20,60,100,100,140,180,220)
foreach ($instr in $in) {
$opcode,$operand = $instr -split " "
if ($opcode -eq 'noop' ) {
$cyclecount += 1
if ($cyclecount -in $markers){ "cycle {0}: {1}" -f $cyclecount, ($x * $cyclecount); $signals.add(($x * $cyclecount)) }
}
if ($opcode -eq 'addx') {
$cyclecount += 1;
if ($cyclecount -in $markers){ "cycle {0}: {1}" -f $cyclecount, ($x * $cyclecount); $signals.add(($x * $cyclecount)) }
$cyclecount += 1;
if ($cyclecount -in $markers){ "cycle {0}: {1}" -f $cyclecount, ($x * $cyclecount); $signals.add(($x * $cyclecount)) }
$x += $operand
}
}
$total = ($signals | Measure-Object -Sum).sum
"total: {0}" -f $total
##
# Part 2
##
$in = Get-Content input.txt
$screen = (new-object 'char[]' 240)
# clear screen
for($x=0; $x -lt 240; $x++) {
$screen[$x] = "."
if ($x % 40 -eq 0) {Write-Host}
write-host $screen[$x] -NoNewline
}
$x = 1
$cyclecount = 0
[int]$spriteloc = 1
[int]$column = 0
[int]$row = [math]::DivRem($cyclecount,40,[ref]$column)
foreach ($instr in $in) {
$opcode,$operand = $instr -split " "
if ($opcode -eq 'noop' ) {
if ($cyclecount % 40 -in (($spriteloc-1),$spriteloc,($spriteloc+1))) { $screen[$cyclecount]="#" } else {$screen[$cyclecount] = "."}
$cyclecount += 1
$row = [math]::DivRem($cyclecount,40,[ref]$column)
}
if ($opcode -eq 'addx') {
if ($cyclecount % 40 -in (($spriteloc-1),$spriteloc,($spriteloc+1))) { $screen[$cyclecount]="#" } else {$screen[$cyclecount] = "."}
$cyclecount += 1;
$row = [math]::DivRem($cyclecount,40,[ref]$column)
if ($cyclecount %40 -in (($spriteloc-1),$spriteloc,($spriteloc+1))) { $screen[$cyclecount]="#" } else {$screen[$cyclecount] = "."}
$cyclecount += 1;
$row = [math]::DivRem($cyclecount,40,[ref]$column)
$x += $operand
$spriteloc = $x
"sprite posistion: {0} after cycle {1}" -f $spriteloc,$cyclecount
"row:column: {0}:{1}" -f $row,$column
}
}
# Draw the screen
for($x=0; $x -lt 240; $x++) {
#$screen[$x] = "."
if ($x % 40 -eq 0) {Write-Host}
write-host $screen[$x] -NoNewline
}