-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.chpl
148 lines (129 loc) · 3.43 KB
/
day22.chpl
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// day22.chpl
use IO;
// annoying that some lines don't have as many characters as others
// will need to pad with spaces
iter readInput() {
for line in stdin.lines() {
yield line;
}
}
var arrayOfLineStrings = readInput();
writeln(arrayOfLineStrings);
var numRows = arrayOfLineStrings.size-2;
writeln("numRows = ", numRows);
var rowSize : int = max reduce [i in 0..#numRows] arrayOfLineStrings[i].size;
writeln("rowSize = ", rowSize);
rowSize -= 1; // taking off newline
var dom = {1..numRows,1..rowSize};
var grid : [dom] string;
for (r,c) in dom {
//writeln("r,c = ", r, ", ", c, ", arrayOfLineStrings[r-1].size = ", arrayOfLineStrings[r-1].size);
if c-1<arrayOfLineStrings[r-1].size-1 { // last -1 to take off newline
grid[r,c] = arrayOfLineStrings[r-1][c-1];
} else {
grid[r,c] = " ";
}
}
writeln(grid);
var directions = arrayOfLineStrings[arrayOfLineStrings.size-1];
writeln(directions);
writeln("directions.size = ", directions.size);
var tokens = tokenize(directions);
writeln("tokens = ", tokens);
enum face {right=0, down=1, left=2, up=3};
var facing : face;
var curr : 2*int = findStart();
writeln("curr = ", curr);
// move around grid
for tok in tokens {
//writeln();
//writeln("tok = ", tok);
select tok {
when "R" { facing = rotateRight( facing ); }
when "L" { facing = rotateLeft( facing ); }
otherwise {
// move forward for given count
var count = tok : int;
while count > 0 {
curr = findNextCoord(curr, facing);
count -= 1;
//writeln("curr = ", curr);
}
}
}
writeln("tok end: facing = ", facing);
writeln("tok end: curr = ", curr);
}
var (r,c) = curr;
writeln("answer = ", 1000*r + 4*c + facing:int);
proc wrapAroundIfNeeded( curr : 2*int ) : 2*int {
var (r,c) = curr;
if r<1 { r=numRows; }
else if r>numRows { r = 1; }
else if c<1 { c=rowSize; }
else if c>rowSize { c = 1; }
return (r,c);
}
proc findNextCoord(curr : 2*int, facing : face) : 2*int {
var incr = (0,0);
select facing {
when face.right do incr = (0,1);
when face.down do incr = (1,0);
when face.left do incr = (0,-1);
when face.up do incr = (-1,0);
}
var next = curr;
do {
next = wrapAroundIfNeeded(next + incr);
} while grid[next]==" ";
//writeln("next finding first nonblank = ", next);
if grid[next] == "#" {
return curr;
} else {
return next;
}
}
proc rotateLeft( curr : face ) : face {
select curr {
when face.right do return face.up;
when face.down do return face.right;
when face.left do return face.down;
when face.up do return face.left;
}
return face.right; // shouldn't get here
}
proc rotateRight( curr : face ) : face {
select curr {
when face.right do return face.down;
when face.down do return face.left;
when face.left do return face.up;
when face.up do return face.right;
}
return face.right; // shouldn't get here
}
iter tokenize(directions : string) {
var i = 0;
while i<directions.size-1 { // don't want newline
var tok = "";
var j=i;
while directions[j].isDigit() {
tok += directions[j];
j += 1;
}
if tok.size>0 {
i=j;
yield tok;
} else {
i += 1;
yield directions[i-1];
}
}
}
// FIXME: trouble specifying formal parameters, reading globals
//proc findStart(grid : [dom] string) : 2*int {
proc findStart() : 2*int {
for (r,c) in dom {
if grid[r,c] == "." { return (r,c); }
}
return (0,0); // shouldn't get here
}