File tree Expand file tree Collapse file tree 1 file changed +32
-15
lines changed Expand file tree Collapse file tree 1 file changed +32
-15
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,14 @@ class Solve10 extends FileReader {
9
9
} )
10
10
. catch ( ( err ) => console . log ( err ) ) ;
11
11
}
12
+ screenW = 40
13
+ screenH = 6
14
+ screen : Array < Array < string > > = new Array ( this . screenH )
15
+ printScreen = ( ) => {
16
+ for ( let i = 0 ; i < this . screenH ; i ++ ) {
17
+ console . log ( this . screen [ i ] . join ( '' ) )
18
+ }
19
+ }
12
20
13
21
process = ( data : string [ ] ) => {
14
22
let pc = 0
@@ -17,30 +25,39 @@ class Solve10 extends FileReader {
17
25
let hold = false
18
26
let param : number | undefined = undefined
19
27
let result = 0
20
-
28
+
29
+ for ( let i = 0 ; i < this . screenH ; i ++ ) {
30
+ this . screen [ i ] = new Array ( this . screenW ) . fill ( '.' )
31
+ }
32
+
21
33
while ( hold || pc < data . length ) {
22
- cycles += 1
23
- if ( ( cycles - 20 ) % 40 === 0 ) {
24
- result += cycles * X
34
+ let crtPos = cycles % 40 + 1
35
+ let crtRow = Math . floor ( cycles / 40 )
36
+ cycles += 1
37
+ if ( ( cycles - 20 ) % 40 === 0 ) {
38
+ result += cycles * X
39
+ }
40
+ if ( crtPos >= X && crtPos < X + 3 ) {
41
+ this . screen [ crtRow ] [ crtPos - 1 ] = "#"
42
+ }
43
+ if ( hold ) {
44
+ if ( param !== undefined ) {
45
+ X += param
25
46
}
26
-
27
- if ( hold ) {
28
- if ( param !== undefined ) {
29
- X += param
30
- }
31
47
hold = false
32
- } else {
48
+ } else {
33
49
const row = data [ pc ++ ]
34
50
const tmp = row . split ( ' ' )
35
51
const op = tmp [ 0 ]
36
52
if ( "addx" === op ) {
37
- param = parseInt ( tmp [ 1 ] , 10 )
38
- hold = true
53
+ param = parseInt ( tmp [ 1 ] , 10 )
54
+ hold = true
39
55
}
56
+ }
40
57
}
41
- }
42
- console . log ( result )
43
- } ;
58
+ console . log ( result )
59
+ this . printScreen ( )
60
+ } ;
44
61
}
45
62
46
63
new Solve10 ( ) ;
You can’t perform that action at this time.
0 commit comments