Skip to content

Instantly share code, notes, and snippets.

@aaiezza
Created July 20, 2024 18:17
Show Gist options
  • Save aaiezza/3218108be528eebc614c35d19bb7fe20 to your computer and use it in GitHub Desktop.
Save aaiezza/3218108be528eebc614c35d19bb7fe20 to your computer and use it in GitHub Desktop.
Coding Exercise: Connected Sinks in a Pipe System

Part 3 - Coding Exercise: Connected Sinks in a Pipe System

In this exercise, you will write code to solve a problem. Your code must be in either Python (preferred) or JavaScript—solutions in other languages will not be accepted! You can write your code using any IDE you want.

Problem We have a pipe system represented by a 2D rectangular grid of cells. There are three different types of objects located in cells in the grid, with each cell having either 0 objects or 1 object:

  • Source: There is 1 source in the system. It is represented by the asterisk character '*'.

  • Sinks: There are an arbitrary number of sinks in the system. They are each represented by a different uppercase letter ('A', 'B', 'C', etc.).

  • Pipes: There are 10 different shapes of pipes, represented by the following characters: '═', '║', '╔', '╗', '╚', '╝', '╠', '╣', '╦', '╩'.

    • Note that each pipe has openings on 2 or 3 sides of its cell.

    • Two adjacent cells are connected if both have a pipe opening at their shared edge.

    • For example, the two cells '╩ ╦' are connected to each other through their shared edge. The two cells '╩ ╔' are not connected to each other through their shared edge, but they could possibly still be connected via a path through other cells around them.

    • Treat the source and sinks as having pipe openings at all of their edges. For example, the two cells 'A ╦' are connected through their shared edge, but the two cells 'B ╔' are not directly connected through their shared edge.

    • A sink may be connected to the source through another sink. For example, in the simple pipe system '* ╦ X Y ═ Z', all three sinks are connected to the source.

Your objective is to write a function that determines which sinks are connected to the source in a given pipe system.

As an example, consider the following illustration of a pipe system:

* ╣   ╔ ═ A
  ╠ ═ ╝    
  C   ╚ ═ B

In this system, the source is connected to sinks A and C, but it is not connected to sink B.

A system is specified by an input text file that contains rows of data indicating the location of the objects in the grid. Each row has three pieces of information, separated by a space character:

  • The character representing the object (asterisk, uppercase letter, or pipe).
  • The x-coordinate of the object in the grid. This has a minimum value of 0. The y-coordinate of the object in the grid. This has a minimum value of 0.

Below are the contents of an input file that specifies the example pipe system illustrated above. The order of the rows within the file is arbitrary, so the rows could be given in any order. The coordinates (0, 0) will always correspond to the same corner of the grid as in this example, so make sure to understand in which directions the x- and y-coordinates increase.

* 0 2
C 1 0
╠ 1 1
╣ 1 2
═ 2 1
╚ 3 0
╝ 3 1
╔ 3 2
═ 4 0
═ 4 2
B 5 0
A 5 2

Specifications

  • Your function must be written in Python (preferred) or JavaScript.
  • The function should take in a single argument, which is a string containing the file path for the input text file.
  • The function should return (not print) your answer as a string of uppercase letters, in alphabetical order with no other characters. For example, if the code determines that sinks 'P', 'B', 'J', and 'T' are the only sinks connected to the source, your code should return the string 'BJPT'.

Explanation of solution

The relevant grid spaces are stored in a dictionary, keyed by its coordinate with a value of what is in the cell (either the power source, the sink, or the pipe shape). After the data object is built and the source is added to the queue and also considered "visited", we begin a breadth-first search, which essentially traverses the graph one cell "outward" at a time in each possible direction. The directions are stored in a constant, containing the delta x and y, and also the possible pipe shapes for both going INTO another pipe in the respective direction, and the pipes that would accept flow FROM another direction. For example, when going UP (dx = 0, dy = 1), all fromPipes must have an opening in the top, and all of the toPipes must have an opening below in order to be considered connected. As the queue is traversed starting with the power source and moving "outward", helpful variables are set including the fromCell, the cell to which we are testing connection, and the direction in which we are testing for an iteration. If the "new" cell has already been visited (tested by coming from another direction), we continue with the next iteration of cells to be traversed. If not yet visited, we test if the cell we have "created" in some direction exists in the grid at the assumed coordinates. If not yet visited, and exists in the grid, we proceed with the iteration and see if the "from" cell connects to the "to" cell, which we decide is possible if there is an opening on the "to" cell's pipe in the direction we are going, or it is a sink. If it does connect, we check if it is a sink, and capture a connection, if it is another pipe, the new cell is added to the queue, and also noted as visited so it is not traversed outward from its coordinate a second time. Once the queue has been exhausted of all connections stemming from the power source, the loop ends, and any "captured" sinks calculated to be connected through pipes to the power source is sorted alphanumerically, and returned.

╠ 18 29
╝ 36 17
╠ 18 25
═ 28 26
╣ 49 0
╠ 8 26
╣ 40 23
╠ 24 6
╠ 31 27
╩ 27 24
╣ 49 24
╠ 23 2
╠ 49 14
╣ 44 1
╗ 36 15
╠ 42 29
╦ 40 0
╚ 25 23
╣ 2 6
╣ 17 7
╦ 46 21
╦ 15 27
╣ 35 4
╩ 31 25
╩ 20 5
╠ 22 20
╦ 37 19
╠ 44 0
╦ 15 10
╣ 33 15
╝ 12 0
╠ 16 3
╣ 46 26
╦ 5 19
╣ 11 23
╝ 42 0
╩ 15 13
╠ 3 7
╠ 14 28
╩ 41 14
║ 31 15
╣ 43 14
╩ 5 17
╣ 23 13
╣ 25 25
╠ 37 2
╩ 19 16
╦ 4 4
╣ 31 23
╣ 17 16
╦ 27 9
╠ 10 18
╦ 42 23
╠ 7 11
╠ 24 17
╩ 1 8
╦ 20 0
║ 30 25
╠ 21 4
╠ 24 8
╣ 5 2
╠ 6 7
╔ 38 24
╦ 35 18
╣ 35 26
╠ 2 23
═ 42 3
╠ 47 21
╣ 15 5
╠ 3 12
╩ 23 1
╩ 42 10
╠ 41 17
║ 3 1
╩ 5 0
╣ 36 2
╦ 45 4
╩ 8 0
╣ 1 15
╩ 15 2
╦ 17 21
╣ 16 2
╝ 24 23
╦ 43 2
║ 6 11
╠ 16 6
╠ 3 0
║ 3 3
╚ 38 1
╣ 23 16
╩ 44 22
╣ 24 27
╣ 12 19
╦ 39 3
╠ 19 1
═ 25 5
╝ 41 1
╝ 0 13
╣ 7 23
╠ 13 29
═ 6 0
╠ 12 25
╦ 28 25
╦ 15 29
╦ 27 12
╩ 10 11
╠ 29 26
╩ 40 20
╣ 10 8
╣ 33 1
╦ 25 15
╣ 21 9
╩ 17 4
╣ 47 24
╣ 17 12
╚ 1 23
═ 33 28
╣ 20 22
╦ 2 17
╦ 43 6
╣ 29 3
╣ 26 6
╣ 30 9
╠ 25 9
╣ 36 18
╦ 23 10
╦ 38 23
╣ 6 3
╠ 43 28
═ 1 14
╣ 23 27
╚ 25 0
╦ 48 24
╦ 36 21
╩ 5 7
╩ 32 18
╩ 41 22
╦ 31 29
╩ 39 24
║ 44 13
╠ 10 27
╩ 21 10
╦ 14 8
╠ 6 15
╠ 0 2
╣ 27 17
╦ 7 21
╩ 48 20
╩ 22 13
═ 23 0
╩ 38 15
╠ 13 11
═ 22 7
╦ 6 8
╦ 27 18
╠ 24 21
╩ 36 22
╩ 18 6
╦ 24 4
╣ 27 10
╦ 26 21
═ 28 22
╠ 30 27
╣ 4 22
╣ 39 12
╠ 33 18
╠ 25 17
╩ 2 11
╦ 16 29
╩ 27 0
╦ 23 18
╩ 23 6
╩ 3 2
╦ 17 19
╩ 4 14
╩ 21 21
╠ 45 16
╝ 48 12
╠ 8 4
╩ 38 2
╩ 46 0
╠ 31 11
╦ 28 20
╠ 15 3
╩ 45 26
╦ 3 28
╠ 20 19
╩ 23 3
╦ 43 12
╩ 2 13
╩ 43 8
╩ 9 4
╦ 49 28
╠ 36 4
╔ 20 21
╠ 49 11
╩ 13 8
╩ 49 10
╩ 16 16
╩ 45 10
═ 12 24
╦ 46 9
╦ 35 17
╠ 40 16
╦ 26 2
╠ 45 23
╚ 39 11
╝ 0 6
╩ 8 19
╠ 17 15
╩ 28 9
╣ 29 5
╔ 48 27
╦ 40 17
╠ 32 14
╣ 10 28
╦ 13 9
╠ 27 27
╠ 34 10
╣ 2 15
╠ 12 14
╠ 14 6
╦ 23 7
╔ 26 12
╩ 12 23
╝ 31 28
╔ 19 9
╩ 29 22
╠ 40 9
╣ 23 26
╝ 8 14
╠ 18 18
╩ 31 26
╠ 12 29
╠ 39 1
╔ 14 13
╣ 36 23
╠ 18 14
╩ 10 10
╣ 11 28
╠ 4 5
╣ 33 19
╣ 31 10
╣ 20 13
╩ 37 5
╦ 49 29
╚ 46 16
╩ 6 19
╩ 19 3
╦ 46 10
╠ 44 9
╣ 24 20
╣ 49 4
╣ 44 7
╦ 22 10
╩ 13 17
╦ 6 13
║ 17 5
╩ 5 5
P 35 3
Q 7 25
╠ 25 26
╩ 9 6
╣ 43 0
╩ 15 22
╠ 16 12
╦ 18 17
╣ 49 9
╠ 26 26
╣ 35 28
╠ 1 2
╦ 21 20
╦ 19 13
╩ 22 29
╣ 44 20
╣ 3 10
╣ 11 9
╣ 16 27
╦ 10 29
╩ 32 6
╩ 4 1
╗ 30 3
╩ 14 11
╩ 21 1
M 13 12
╩ 42 20
╦ 34 11
╩ 26 0
╠ 4 18
╚ 28 11
╦ 13 23
╩ 34 19
╦ 0 4
╦ 26 27
╠ 10 14
╩ 48 13
╠ 35 15
╣ 48 4
╦ 38 29
╠ 43 15
╦ 24 0
╩ 33 26
╩ 30 22
╠ 40 28
╦ 19 29
╦ 20 1
╦ 27 29
╦ 2 9
╦ 14 17
╠ 46 7
╦ 18 9
╠ 0 5
╣ 21 24
╣ 0 3
╠ 27 23
╩ 48 7
╠ 35 24
╠ 32 29
╦ 42 6
╠ 39 13
╩ 7 9
╩ 33 5
╗ 29 27
╝ 3 9
╩ 45 6
╦ 31 6
╩ 47 29
╠ 0 27
╩ 41 19
╗ 24 24
╩ 29 19
╔ 17 17
╦ 28 29
╠ 11 12
╣ 40 29
╩ 15 26
W 22 1
╠ 42 16
╦ 36 26
╣ 8 7
╩ 30 1
╠ 37 16
╠ 32 5
╣ 46 29
╩ 38 14
╩ 40 21
╝ 38 18
╦ 28 0
╣ 34 6
╣ 26 7
I 21 23
╦ 42 5
╚ 16 9
╣ 4 3
╦ 20 10
╔ 1 25
╠ 4 24
╩ 24 29
╩ 0 28
╦ 34 23
╦ 25 29
╔ 7 6
╩ 26 29
╠ 21 12
╠ 29 21
╠ 25 27
╣ 14 14
╩ 10 7
╠ 14 22
╦ 40 1
╦ 12 27
╠ 27 15
╦ 30 0
╗ 49 12
╝ 6 21
╠ 41 5
╦ 17 18
╦ 35 2
╠ 34 28
╠ 32 9
╣ 33 23
╠ 21 6
╩ 41 26
╠ 10 16
╩ 9 14
╩ 44 19
╠ 5 24
═ 28 19
╦ 19 18
╦ 20 9
╠ 9 5
╩ 38 13
╗ 13 10
╩ 44 16
╣ 49 27
╦ 30 24
╠ 13 13
═ 0 23
╣ 13 27
╩ 27 13
╩ 36 12
╦ 19 7
╠ 12 11
═ 36 29
╠ 24 7
║ 10 21
║ 19 11
╠ 42 17
╦ 11 22
╩ 49 13
╠ 44 29
╠ 13 20
╦ 10 13
╩ 41 9
║ 48 1
╚ 32 2
╣ 11 21
═ 7 0
╩ 10 19
╣ 48 5
║ 41 28
╩ 4 8
╩ 15 6
╣ 20 4
╦ 11 10
╩ 37 7
╩ 21 5
╔ 23 11
║ 17 6
╦ 47 20
╦ 5 11
╠ 30 16
╣ 43 3
╗ 5 6
╣ 36 1
╚ 45 25
╦ 24 13
╩ 14 27
╩ 2 7
╦ 39 22
╣ 1 1
╩ 22 4
╝ 0 17
* 34 17
╣ 16 26
╠ 46 2
╦ 5 20
╩ 36 19
╠ 43 19
╠ 46 12
╩ 35 14
╠ 39 23
╣ 16 0
╦ 46 20
╠ 3 20
╦ 29 6
╠ 46 14
╗ 36 11
╚ 14 7
╠ 31 1
╦ 3 17
╩ 40 4
╝ 35 12
╠ 12 2
╩ 26 24
║ 43 5
╠ 45 18
╠ 36 28
╝ 27 25
╦ 36 3
╠ 42 24
╔ 22 5
╠ 35 27
║ 4 25
╦ 44 26
╦ 7 26
╦ 29 1
╣ 19 24
╗ 14 15
╣ 26 3
╠ 29 4
╠ 47 0
╝ 0 8
╦ 2 20
╝ 39 14
╩ 23 17
═ 34 22
╠ 12 3
╠ 42 1
╠ 30 8
╦ 3 29
╣ 11 13
╣ 25 28
╩ 4 29
╦ 7 13
╠ 3 26
╩ 35 22
╠ 41 0
╣ 48 17
╩ 10 17
╦ 44 17
╦ 38 3
╩ 18 22
╣ 1 10
╦ 37 21
╣ 1 22
╠ 5 15
╠ 22 24
╔ 5 28
╠ 24 28
╩ 23 20
G 47 23
╦ 27 26
═ 22 17
╩ 0 0
╔ 41 15
╩ 18 0
╩ 40 25
╣ 25 1
╩ 43 27
╩ 2 16
╦ 20 7
╦ 9 20
╠ 48 25
╣ 4 15
╠ 34 3
╦ 10 24
╠ 1 17
╣ 26 8
C 30 18
╣ 44 14
╩ 17 2
╣ 30 6
╩ 20 20
╩ 30 19
╣ 0 22
╣ 43 7
╣ 42 12
╠ 23 8
╠ 32 10
╩ 8 25
╠ 7 10
╦ 11 26
╩ 36 14
╣ 42 22
╣ 5 13
╦ 46 8
╣ 46 13
╦ 14 25
╔ 35 11
╦ 22 19
╠ 19 14
╠ 5 16
╩ 36 7
╣ 26 11
╦ 4 16
╣ 45 13
╠ 3 21
╣ 6 22
╠ 11 18
╣ 35 8
╣ 9 24
╣ 25 14
╣ 2 24
╣ 24 25
╩ 23 21
╦ 37 12
╩ 13 16
╣ 14 1
╦ 9 8
╩ 32 15
╦ 46 6
╣ 7 12
╩ 46 22
═ 12 15
╦ 45 14
╩ 39 25
║ 12 9
╣ 41 27
╩ 35 29
╦ 9 25
╠ 10 1
╦ 41 18
╣ 23 12
╦ 33 0
╣ 34 1
╠ 43 24
╠ 45 12
╠ 16 22
╠ 32 11
╣ 26 19
╩ 40 22
╔ 5 3
║ 18 4
╩ 21 18
╩ 43 1
╠ 17 3
╣ 10 0
╣ 9 3
╩ 27 3
╦ 17 10
╦ 7 29
╦ 21 11
╣ 6 10
╣ 4 2
╣ 15 19
╩ 44 6
║ 39 26
╠ 7 3
╩ 13 3
╔ 18 2
╦ 22 16
╠ 49 19
╠ 44 11
╣ 47 8
╠ 28 23
╦ 1 24
║ 45 24
╣ 21 8
╠ 30 28
╩ 5 12
╦ 44 25
╣ 0 11
╠ 18 28
╦ 44 21
╦ 8 3
╦ 33 4
╝ 32 16
╩ 35 7
╣ 37 9
║ 6 12
╠ 15 23
╦ 14 10
╦ 41 13
╣ 12 28
╗ 9 29
╠ 43 25
╣ 34 18
╔ 42 11
╦ 37 3
╦ 4 6
╦ 11 1
╣ 0 19
╩ 34 20
╩ 16 20
╦ 29 16
╣ 23 15
═ 24 16
╩ 29 0
╩ 19 10
║ 46 19
╦ 33 13
╠ 10 12
╣ 9 27
╠ 23 23
╝ 34 16
╣ 9 28
╣ 16 14
╣ 44 3
║ 3 4
╣ 29 9
╠ 16 18
╦ 46 5
╩ 26 14
╣ 18 3
╣ 34 25
╣ 13 4
╦ 42 27
╩ 8 13
╦ 0 24
╦ 41 8
╦ 33 25
╠ 37 23
╠ 3 24
╦ 48 3
╠ 19 15
╩ 2 8
╠ 22 25
╣ 39 17
╣ 45 19
╩ 30 5
╦ 37 26
╩ 19 19
╩ 29 13
╩ 47 18
╣ 2 26
╣ 7 17
╩ 38 25
╠ 26 23
╔ 12 7
╩ 2 29
O 47 12
╩ 33 14
╠ 43 11
╠ 43 4
╣ 16 13
╣ 48 0
╩ 15 4
╣ 5 22
╩ 27 8
╣ 30 20
╣ 7 18
╩ 49 6
╝ 37 1
╩ 41 10
╠ 31 17
╝ 48 11
╣ 37 27
╠ 27 5
╔ 33 12
╚ 29 18
╦ 28 28
╦ 36 6
E 47 15
╦ 41 2
╩ 13 24
╩ 18 20
╣ 20 8
╠ 14 29
╔ 5 14
╦ 17 29
╦ 46 23
S 47 19
╦ 5 8
╦ 18 1
╩ 41 29
╦ 49 2
╠ 20 15
╦ 22 14
╗ 45 21
╦ 31 0
╣ 43 20
╩ 1 3
╣ 37 6
╠ 39 15
╠ 39 28
╦ 39 27
╦ 2 18
╩ 3 23
╠ 37 8
╦ 0 26
═ 4 27
╣ 18 26
╠ 22 26
╠ 20 17
╦ 0 29
╚ 39 2
╩ 32 8
╩ 30 2
╣ 45 8
╣ 3 5
╩ 44 12
╠ 28 15
╩ 39 5
╦ 13 19
╦ 11 5
╠ 21 27
╩ 47 16
═ 28 27
╠ 49 22
╠ 45 7
╦ 7 14
╠ 48 19
╠ 20 28
╦ 9 9
╩ 1 5
╠ 20 25
╠ 20 29
╣ 24 1
╗ 6 25
╦ 41 16
╣ 38 8
╩ 38 20
╣ 1 27
╗ 19 4
╩ 42 4
╣ 6 24
╩ 49 16
╠ 19 23
╦ 38 16
╦ 16 24
╦ 29 14
╦ 4 21
╣ 17 20
╦ 38 4
╠ 27 16
╣ 17 25
╣ 27 14
╣ 1 28
╦ 11 27
╔ 48 8
╣ 4 12
╠ 23 24
╠ 17 1
╦ 13 5
╦ 41 3
╩ 33 17
╠ 0 20
╦ 32 13
╠ 14 12
╣ 32 3
╩ 13 15
╠ 38 10
╠ 0 9
╠ 0 21
╩ 25 16
╔ 0 15
╣ 7 24
╩ 0 18
N 31 12
╣ 48 18
╦ 36 27
╩ 6 6
╣ 17 24
╔ 18 8
╠ 33 8
╦ 20 12
╩ 12 6
╦ 36 16
╗ 17 26
╩ 25 20
╦ 23 14
A 31 16
╩ 39 10
V 12 1
╦ 41 7
╗ 15 15
╩ 33 27
╩ 20 26
╣ 35 23
╩ 6 4
╦ 27 20
╔ 46 11
╠ 41 23
╠ 29 29
╦ 3 15
╠ 19 0
╩ 32 4
╦ 24 18
╩ 23 25
╚ 32 26
╠ 43 17
╩ 0 7
╩ 22 28
╣ 38 7
╦ 30 12
╣ 48 10
╚ 2 3
╠ 1 29
╩ 31 13
╦ 4 11
╠ 44 5
╣ 9 10
╦ 3 18
╣ 21 29
╠ 25 12
╩ 10 2
╣ 13 28
╠ 9 2
╩ 6 14
╩ 25 10
╩ 19 6
╔ 5 10
╦ 30 11
╠ 24 14
╝ 3 8
╠ 11 19
╣ 8 23
╠ 37 13
╠ 3 22
╝ 25 24
╠ 41 21
╦ 26 28
╠ 33 7
╣ 40 15
╩ 47 10
╦ 15 12
╩ 47 11
═ 20 23
╠ 3 11
║ 29 24
╩ 16 23
╠ 35 10
╠ 6 18
╩ 40 26
╠ 19 26
╩ 1 6
╠ 26 9
╩ 21 19
╣ 17 0
╠ 28 17
╦ 46 18
╣ 8 24
╠ 40 2
╔ 1 18
╩ 30 21
╣ 36 20
╠ 15 28
╠ 14 9
╦ 24 22
╩ 34 27
╦ 28 5
╩ 49 7
╠ 21 28
╣ 8 21
╦ 31 4
╩ 6 16
╗ 25 11
╩ 40 8
╦ 30 10
╩ 43 9
╦ 20 16
╩ 21 13
╠ 36 25
╦ 24 9
╩ 16 8
╔ 2 5
╩ 18 16
╩ 24 11
╣ 39 16
╣ 27 22
╦ 18 21
╝ 49 23
║ 42 14
╦ 20 2
╩ 2 1
╦ 33 29
╠ 28 3
╦ 30 26
╔ 47 6
╩ 28 7
╣ 28 12
╠ 8 2
╦ 45 2
╠ 27 1
╩ 45 22
╠ 4 23
╩ 34 4
╩ 4 26
╦ 6 23
╠ 17 27
╩ 18 7
╣ 46 27
╩ 45 27
╠ 33 11
╠ 28 16
╠ 31 14
╠ 24 12
║ 7 20
╩ 13 0
╦ 10 5
╣ 23 28
╠ 34 0
╣ 16 15
╠ 10 15
╣ 2 14
╩ 9 19
╦ 27 4
╠ 15 9
╠ 23 22
╩ 19 22
╦ 38 0
╣ 11 3
╩ 42 18
╠ 10 3
╦ 39 6
H 9 18
╩ 8 15
╩ 11 15
╠ 48 22
╦ 38 6
╦ 39 4
╦ 19 25
╦ 0 10
╚ 42 21
╠ 32 0
║ 40 6
╣ 18 13
╠ 7 15
╦ 15 16
╠ 7 2
╣ 47 2
╦ 42 9
╝ 9 7
╩ 32 21
╣ 43 29
╣ 5 29
╚ 13 22
═ 1 19
╠ 11 4
╦ 9 17
╦ 22 21
╦ 40 13
╠ 8 12
╠ 7 7
╠ 15 11
╩ 16 5
╣ 27 28
╠ 45 1
╩ 14 18
║ 22 6
╩ 35 9
╣ 14 23
╩ 8 9
╣ 3 25
╣ 38 5
╠ 41 20
╠ 2 22
╠ 13 14
╠ 48 14
╦ 35 5
╩ 3 14
╣ 31 3
╣ 40 12
╠ 3 27
╣ 13 18
╩ 21 25
╣ 32 28
╦ 16 21
╦ 10 26
╦ 36 9
╝ 49 26
╣ 27 21
╣ 34 5
╠ 35 20
╣ 48 29
╩ 17 8
╠ 25 2
╦ 43 23
╦ 6 27
═ 37 28
╠ 7 22
╦ 8 8
╩ 12 4
╩ 40 19
╠ 17 23
╩ 35 16
╩ 34 2
╩ 24 26
╩ 11 29
╠ 36 24
╦ 32 24
╦ 6 17
╝ 6 9
╣ 7 16
╩ 29 20
╩ 20 6
╦ 15 25
═ 48 26
║ 15 1
╣ 46 24
╦ 16 11
╣ 28 13
╦ 22 9
╩ 37 10
╠ 16 7
╦ 14 19
╩ 42 19
═ 42 26
╩ 47 9
╦ 49 20
╠ 4 10
╦ 8 20
D 35 13
╔ 4 9
╩ 25 7
╦ 1 26
╦ 21 22
╠ 34 21
╦ 49 17
╠ 40 5
╩ 18 11
╩ 14 26
╠ 44 23
╩ 37 25
═ 24 3
╦ 20 14
╠ 43 18
╦ 8 6
Z 25 8
╦ 12 16
╣ 14 16
╣ 16 1
╩ 15 14
╦ 11 24
╣ 44 4
╩ 13 26
╦ 31 21
╠ 31 20
X 4 20
╣ 26 18
╩ 12 21
╠ 29 10
╣ 24 5
║ 9 23
╩ 26 5
╦ 34 29
╠ 46 3
╩ 48 28
╣ 23 9
╦ 32 22
╦ 30 13
╚ 11 25
╣ 43 21
╣ 37 17
╦ 3 6
╣ 45 11
╣ 35 25
╦ 36 10
║ 31 22
╠ 39 9
╠ 0 25
╗ 45 29
╦ 46 17
╦ 39 7
╚ 31 7
╠ 31 19
╦ 34 13
═ 48 15
╣ 35 1
╦ 47 5
╚ 15 21
╦ 42 8
╩ 2 21
╣ 29 15
╔ 19 28
╣ 4 7
╠ 31 5
╠ 0 16
╦ 34 26
╠ 45 3
╠ 32 25
╠ 5 9
╩ 25 19
╣ 37 0
╠ 24 15
═ 10 20
╣ 8 5
╣ 16 10
╠ 17 28
╦ 33 2
╣ 2 25
╠ 43 10
╝ 11 0
J 47 4
╣ 4 13
╩ 1 4
╣ 22 18
╣ 20 24
╠ 48 16
╦ 45 28
╠ 29 17
╠ 26 22
╔ 16 25
╩ 37 24
╩ 5 23
╠ 19 17
╩ 8 22
╣ 37 29
╣ 37 4
╔ 38 9
╣ 13 1
╠ 5 25
╝ 3 19
╦ 7 4
╩ 8 11
╣ 30 7
╠ 42 13
╣ 5 1
╠ 42 25
╝ 44 15
╦ 33 6
╣ 47 7
╩ 22 22
╔ 41 25
╩ 8 27
╩ 46 28
╦ 26 25
╦ 28 4
╠ 28 2
╠ 32 19
╦ 29 23
╣ 14 3
╦ 37 14
╠ 2 19
╣ 33 16
╣ 11 20
╩ 25 18
╔ 35 0
╦ 12 10
╠ 49 1
╠ 49 15
╣ 22 2
╩ 47 1
╩ 18 15
╣ 32 1
╠ 40 27
═ 33 20
╩ 13 21
╣ 44 28
╩ 12 13
╦ 42 2
╠ 13 7
L 44 18
╩ 39 19
╣ 4 17
╠ 16 19
╠ 2 12
╠ 16 17
╦ 0 1
╦ 26 13
╣ 48 21
╦ 2 2
╣ 38 22
╦ 12 18
╦ 45 20
╩ 45 9
╔ 23 5
╠ 22 15
╠ 30 15
╗ 2 10
║ 1 21
╦ 7 28
╚ 26 20
║ 25 3
╦ 47 22
╣ 15 24
╩ 9 22
╩ 10 6
╝ 5 21
╣ 48 6
═ 1 11
╦ 32 23
╩ 23 29
╗ 1 12
╠ 1 13
╩ 10 25
╦ 47 27
═ 44 27
╠ 32 17
╦ 28 24
╦ 8 1
║ 0 12
T 38 17
╦ 47 14
╠ 29 25
╝ 0 14
╠ 39 0
╠ 26 17
═ 14 5
╦ 19 2
╠ 6 5
╦ 18 12
╠ 4 19
╦ 1 0
╩ 8 17
╩ 32 12
╚ 1 7
╦ 8 10
╣ 12 5
╦ 21 3
║ 8 28
╩ 9 0
═ 11 6
╣ 42 15
╦ 31 8
║ 7 27
╣ 1 16
║ 21 16
╠ 34 14
╣ 37 22
╣ 7 1
╦ 2 28
╣ 15 20
╣ 28 18
╣ 43 16
╣ 28 21
╚ 13 25
╦ 34 12
╩ 11 16
╠ 33 9
╩ 40 3
╦ 19 21
╔ 17 14
╔ 38 19
╣ 49 21
╦ 21 17
╦ 29 8
╩ 33 22
╠ 12 26
╠ 49 8
U 31 9
╦ 12 12
╣ 46 1
═ 17 9
╚ 41 24
╣ 14 21
╠ 21 15
╩ 37 11
╣ 29 11
╩ 40 24
╩ 40 14
╩ 42 7
╩ 10 4
╦ 22 8
╩ 41 12
╩ 49 18
╣ 46 15
╣ 9 12
║ 15 18
╩ 5 27
╦ 36 0
╩ 44 24
╩ 29 12
╦ 28 6
╔ 12 8
╦ 7 19
╦ 36 13
╠ 13 6
╦ 14 24
╩ 7 5
╦ 16 28
╠ 39 21
╣ 33 10
╣ 29 28
╣ 3 13
╣ 23 4
╣ 25 21
╝ 32 27
╦ 9 11
╣ 36 8
╣ 8 29
╣ 9 13
╠ 27 11
╔ 3 16
╣ 39 18
╦ 46 25
Y 48 23
╠ 18 10
╣ 9 1
╩ 26 15
╣ 48 9
╣ 18 19
╠ 8 18
╩ 22 12
╠ 2 27
╩ 41 4
╩ 2 0
╔ 38 28
╠ 11 8
╦ 22 0
╦ 17 22
╩ 26 4
╩ 19 20
╩ 47 13
╦ 6 28
╩ 49 5
╩ 45 0
╠ 1 9
╣ 47 26
╣ 42 28
╦ 5 26
╦ 44 2
╠ 38 21
╠ 17 13
╩ 28 8
╗ 22 23
╠ 24 2
╩ 31 18
╩ 12 20
╦ 11 11
╣ 35 19
╣ 41 6
╦ 40 11
╠ 45 17
╚ 4 28
╩ 46 4
╦ 37 18
╠ 18 5
╩ 27 2
╣ 47 25
╩ 5 4
╠ 47 3
╩ 21 26
╩ 28 1
╦ 43 26
╦ 38 12
╩ 32 20
╩ 41 11
╩ 10 22
╠ 14 2
╠ 12 22
╦ 27 6
╩ 30 29
╩ 31 2
╩ 36 5
╩ 19 27
╣ 28 14
╦ 9 26
╣ 26 16
╣ 19 8
╣ 22 3
╩ 21 0
╣ 33 24
╩ 47 17
╚ 25 4
╦ 5 18
K 22 27
╠ 30 4
╠ 14 4
╣ 18 27
╦ 18 23
╣ 34 24
╣ 9 16
╣ 13 2
╔ 20 27
╦ 32 7
╝ 44 8
╗ 6 2
╚ 26 10
╦ 30 14
╠ 49 25
╩ 38 27
╣ 29 7
╦ 26 1
╦ 45 15
╦ 24 19
╗ 17 11
╣ 38 11
╣ 7 8
╣ 15 7
╩ 15 8
╝ 37 20
╣ 40 18
╠ 48 2
╩ 43 13
╣ 38 26
╣ 34 7
╠ 34 9
╦ 11 2
╣ 21 7
╦ 33 21
╠ 10 23
╠ 4 0
╠ 19 12
╠ 18 24
╩ 29 2
╦ 39 8
╣ 14 0
╣ 8 16
╠ 30 23
╠ 27 7
╔ 11 7
═ 12 17
║ 27 19
╣ 43 22
╦ 20 3
║ 40 7
╩ 44 10
╩ 35 21
╩ 11 17
╔ 49 3
╦ 16 4
╠ 15 17
═ 31 24
╩ 34 15
╚ 35 6
║ 6 29
╦ 25 22
╦ 6 26
╩ 34 8
╠ 10 9
╩ 20 11
╣ 45 5
╗ 40 10
╝ 14 20
from collections import deque
def main(filename):
grid = {}
source = None
with open(filename) as file:
for line in file:
char, x, y = line.strip().split()
x, y = int(x), int(y)
if char == '*':
source = (x, y)
grid[(x, y)] = char
# BFS setup
queue = deque([source])
visited = set([source])
connected = set()
# Directions for adjacent cells
directions = [(0, 1, '║╝╚╩╣╠╬*', '║╗╔╦╣╠╬', 'up'), # Going up
(1, 0, '═╔╚╦╩╠╬*', '═╗╝╣╩╦╬', 'right'), # Going right
(0, -1, '║╗╔╦╣╠╬*', '║╝╚╩╣╠╬', 'down'), # Going down
(-1, 0, '═╗╝╣╩╦╬*', '═╔╚╦╩╠╬','left')] # Going left
# BFS traversal
while queue:
x, y = queue.popleft()
for dx, dy, cFrom, cTo, direction in directions:
nx, ny = x + dx, y + dy
if (nx, ny) in visited:
continue
if (nx, ny) in grid:
fromCell = grid[(x,y)]
cell = grid[(nx, ny)]
doesConnect = fromCell in cFrom and (cell.isalpha() or cell in cTo)
# print(f'{fromCell} → {cell} going {direction}. connected: {doesConnect}')
if cell == '*' or doesConnect:
if cell.isalpha():
connected.add(cell)
queue.append((nx, ny))
visited.add((nx, ny))
return ''.join(sorted(connected))
print(main('coding_qual_input.txt'))
* 0 2
C 1 0
╠ 1 1
╣ 1 2
═ 2 1
╚ 3 0
╝ 3 1
╔ 3 2
═ 4 0
═ 4 2
B 5 0
A 5 2
D 0 1
═ 1 1
╣ 2 1
B 2 2
╚ 2 0
═ 3 0
* 3 1
╝ 4 0
E 7 0
╬ 4 1
A 4 2
═ 5 1
═ 6 1
╗ 7 1
C 7 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment