My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Update utils.py

+24 -4
+24 -4
2019/utils.py
··· 139 139 return '%032x' % hash_val 140 140 141 141 142 + HEX_DIRS = { 143 + 'N': (1, -1, 0), 144 + 'NE': (1, 0, -1), 145 + 'SE': (0, 1, -1), 146 + 'S': (-1, 1, 0), 147 + 'SW': (-1, 0, 1), 148 + 'NW': (0, -1, 1), 149 + } 150 + 151 + 152 + def hex_distance(x, y, z): 153 + """Returns a given hex point's distance from the origin.""" 154 + return (abs(x) + abs(y) + abs(z)) // 2 155 + 156 + 142 157 @total_ordering 143 158 class Point: 144 159 """Simple 2-dimensional point.""" ··· 168 183 return not self == other 169 184 170 185 def __lt__(self, other): 171 - return self.manhattan < other.manhattan 186 + return self.length < other.length 172 187 173 188 def __str__(self): 174 189 return "({}, {})".format(self.x, self.y) ··· 179 194 def __hash__(self): 180 195 return hash(tuple((self.x, self.y))) 181 196 182 - def to(self, other): 197 + def dist(self, other): 183 198 return math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2) 184 199 185 - def to_manhattan(self, other): 200 + def dist_manhattan(self, other): 186 201 return abs(self.x - other.x) + abs(self.y - other.y) 187 202 203 + def angle(self, to=None): 204 + if to is None: 205 + return math.atan2(self.y, self.x) 206 + return math.atan2(self.y - to.y, self.x - to.x) 207 + 188 208 @property 189 209 def manhattan(self): 190 210 return abs(self.x) + abs(self.y) 191 211 192 212 @property 193 - def distance(self): 213 + def length(self): 194 214 return math.sqrt(self.x ** 2 + self.y ** 2) 195 215 196 216 def neighbours_4(self):