-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.chpl
157 lines (129 loc) · 3.8 KB
/
day23.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
149
150
151
152
153
154
155
156
// day 23
use IO, Set, List, Map;
//---- Part 1
var rowSize : int;
var trees = readForest(rowSize);
writeln(trees);
var gridDomain = {1..trees.size/rowSize, 1..rowSize};
var grid = reshape(trees, gridDomain);
writeln(grid);
writeln();
iter readForest(ref rowSize : int) {
var line : string;
while readLine(line) {
rowSize = line.strip().size;
for treeHeight in line.strip() do
yield treeHeight;
}
}
// rounds of moving elves
config const numRounds = 10;
var current : set(2*int);
var proposed : set(2*int);
for (r,c) in gridDomain {
if grid[r,c] == "#" {
current.add((r,c));
}
}
writeln(current);
var allDirections : set(2*int);
var northDirections : set(2*int);
var southDirections : set(2*int);
var westDirections : set(2*int);
var eastDirections : set(2*int);
northDirections.add( (-1,-1) ); // NW
northDirections.add( (-1,0) ); //N
northDirections.add( (-1,1) ); // NE
southDirections.add( (1,-1) ); // SW
southDirections.add( (1,0) ); //S
southDirections.add( (1,1) ); // SE
westDirections.add( (-1,-1) ); // NW
westDirections.add( (0,-1) ); //W
westDirections.add( (1,-1) ); // SW
eastDirections.add( (1,1) ); // SE
eastDirections.add( (0,1) ); //E
eastDirections.add( (-1,1) ); // NE
allDirections = northDirections + southDirections + westDirections + eastDirections;
var setList : list(set(2*int));
setList.append(northDirections);
setList.append(southDirections);
setList.append(westDirections);
setList.append(eastDirections);
var dirList : list(2*int); // parallel with set list
dirList.append((-1,0));
dirList.append((1,0));
dirList.append((0,-1));
dirList.append((0,1));
for round in 1..numRounds {
doRound();
}
// accessing a lot of globals
proc doRound() : bool {
var nextSet : set(2*int);
var proposedSet : set(2*int);
var proposedMap : map(2*int,2*int);
var dontMove : set(2*int);
for (r,c) in current {
//writeln();
//writeln("r,c = ", (r,c));
var neighbors : set(2*int);
for (diffR,diffC) in allDirections do neighbors.add( (diffR,diffC) + (r,c));
//writeln(neighbors);
//writeln(neighbors & current);
// as long as there is at least one elf neighbor
var neighborElves = neighbors & current;
if neighborElves.size>0 {
for (dir,dirSet) in zip(dirList,setList) {
var dirNeighbors : set(2*int);
for (diffR,diffC) in dirSet do dirNeighbors.add( (diffR,diffC) + (r,c));
var dirElves = current & dirNeighbors;
if dirElves.size==0 {
var proposed = (r,c) + dir;
if proposedSet.contains(proposed) {
proposedSet.remove(proposed);
} else {
proposedSet.add( proposed );
proposedMap[(r,c)] = proposed;
}
break;
}
} // set over possible directions
}
}
//writeln("proposedSet = ", proposedSet);
//writeln("proposedMap = ", proposedMap);
// 2nd part of round, move elf if proposed direction is still in proposedSet
for (r,c) in current {
if proposedMap.contains((r,c)) && proposedSet.contains(proposedMap[(r,c)]) {
nextSet.add( proposedMap[(r,c)] );
} else {
nextSet.add( (r,c) );
}
}
if current == nextSet then return true;
current = nextSet;
//writeln("current = ", current);
// shift the directions
var tempSet = setList.pop(0);
setList.append(tempSet);
var tempDir = dirList.pop(0);
dirList.append(tempDir);
return false;
}
// compute the bounding box and then subtract the number of elves
var minR, maxR, minC, maxC : int;
var rSet : set(int);
var cSet : set(int);
for (r,c) in current {
rSet.add(r);
cSet.add(c);
}
minR = min reduce rSet;
maxR = max reduce rSet;
minC = min reduce cSet;
maxC = max reduce cSet;
writeln("answer = ", (maxR-minR+1)*(maxC-minC+1) - current.size);
// part 2
var count = numRounds;
while ! doRound() do count+= 1;
writeln("part 2 answer = ", count);