|
27 | 27 | These wires cross at two locations (marked X), but the lower-left one is closer to the central port: its distance is 3 + 3 = 6.
|
28 | 28 | '''
|
29 | 29 |
|
30 |
| -from typing import List |
| 30 | +from typing import List, Tuple |
31 | 31 |
|
32 | 32 |
|
33 |
| -def manhattendistance(A: List[str], B: List[str]) -> int: |
34 |
| - return None |
| 33 | +def crossedWires(A: List[str], B: List[str]) -> int: |
| 34 | + # transform Lists, |
| 35 | + aP = transform2Points(A) |
| 36 | + bP = transform2Points(B) |
| 37 | + # find matching Points |
| 38 | + poi = matchingPoints(aP, bP) |
| 39 | + print(poi) |
| 40 | + # get shortest distance |
| 41 | + pass |
35 | 42 |
|
36 | 43 |
|
37 |
| -A1 = ['R8', 'U5', 'L5', 'D3'] |
38 |
| -B1 = ['U7', 'R6', '4', 'L4'] |
39 |
| -assert manhattendistance(A1, B1) == None |
| 44 | +def transform2Points(A: List[str]) -> List[Tuple[int]]: |
| 45 | + res = [] |
| 46 | + currentPoint = (0, 0) |
| 47 | + point = List |
| 48 | + for instr in A: |
| 49 | + direction = instr[:1] |
| 50 | + distance = int(instr[1:]) |
| 51 | + if direction == 'R': |
| 52 | + point = (currentPoint[0]+distance, currentPoint[1]) |
| 53 | + elif direction == 'L': |
| 54 | + point = (currentPoint[0]-distance, currentPoint[1]) |
| 55 | + elif direction == 'U': |
| 56 | + point = (currentPoint[0], currentPoint[1]+distance) |
| 57 | + elif direction == 'D': |
| 58 | + point = (currentPoint[0], currentPoint[1]-distance) |
| 59 | + else: |
| 60 | + raise Exception('no valid direction') |
| 61 | + currentPoint = point |
| 62 | + res.append(point) |
| 63 | + print(res) |
| 64 | + return res |
| 65 | + |
| 66 | + |
| 67 | +def matchingPoints(A: List[Tuple[int]], B: List[Tuple[int]]) -> List[Tuple[int]]: |
| 68 | + res = [] |
| 69 | + for p1 in A: |
| 70 | + if B.count(p1) > 0: |
| 71 | + res.append(p1) |
| 72 | + return res |
| 73 | + |
| 74 | + |
| 75 | +def mannhattenDistance(A: Tuple[int]) -> int: |
| 76 | + return A[0] + A[1] |
| 77 | + |
| 78 | + |
| 79 | +one = { |
| 80 | + 'A': ['R8', 'U5', 'L5', 'D3'], |
| 81 | + 'B': ['U7', 'R6', 'D4', 'L4'], |
| 82 | + 'MD': 6 |
| 83 | +} |
| 84 | +# assert crossedWires(one['A'], one['B']) == one['MD'] |
| 85 | +two = { |
| 86 | + 'A': ['R75', 'D30', 'R83', 'U83', 'L12', 'D49', 'R71', 'U7', 'L72'], |
| 87 | + 'B': ['U62', 'R66', 'U55', 'R34', 'D71', 'R55', 'D58', 'R83'], |
| 88 | + 'MD': 159 |
| 89 | +} |
| 90 | +# assert crossedWires(two['A'], two['B']) == two['MD'] |
| 91 | +three = { |
| 92 | + 'A': ['R98', 'U47', 'R26', 'D63', 'R33', 'U87', 'L62', 'D20', 'R33', 'U53', 'R51'], |
| 93 | + 'B': ['U98', 'R91', 'D20', 'R16', 'D67', 'R40', 'U7', 'R15', 'U6', 'R7'], |
| 94 | + 'MD': 135 |
| 95 | +} |
| 96 | +# assert crossedWires(three['A'], three['B']) == three['MD'] |
40 | 97 |
|
41 | 98 | # with open('day01-Input.txt') as f:
|
42 | 99 | # masses = [int(line.strip()) for line in f]
|
43 | 100 | # res = sum(fuel(mass) for mass in masses)
|
44 | 101 |
|
45 | 102 | # print(res)
|
| 103 | + |
| 104 | +print(crossedWires(one['A'], one['B'])) |
0 commit comments