SOLVED! Advent of Code Day 19 part 1

In this example below you will see how to do a SOLVED! Advent of Code Day 19 part 1 with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 01abhishekjain, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 01abhishekjain ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>SOLVED! Advent of Code Day 19 part 1</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div>SOLVED!</div>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/01abhishekjain/solved-advent-of-code-day-19-part-1-WdpBNL */
body {
  display: flex;
  align-items: center;
  justify-content: center;
  background: lightgreen;
  font-size: 50px;
  color: darkgreen;
  font-family: monospace;
  height: -webkit-fill-available;
}

/*Downloaded from https://www.codeseek.co/01abhishekjain/solved-advent-of-code-day-19-part-1-WdpBNL */
// http://adventofcode.com/2017/day/19/answer

// --- Day 19: A Series of Tubes ---
// Somehow, a network packet got lost and ended up here. It's trying to follow a routing diagram (your puzzle input), but it's confused about where to go.

// Its starting point is just off the top of the diagram. Lines (drawn with |, -, and +) show the path it needs to take, starting by going down onto the only line connected to the top of the diagram. It needs to follow this path until it reaches the end (located somewhere within the diagram) and stop there.

// Sometimes, the lines cross over each other; in these cases, it needs to continue going the same direction, and only turn left or right when there's no other option. In addition, someone has left letters on the line; these also don't change its direction, but it can use them to keep track of where it's been. For example:

//      |          
//      |  +--+    
//      A  |  C    
//  F---|----E|--+ 
//      |  |  |  D 
//      +B-+  +--+ 

// Given this diagram, the packet needs to take the following path:

// Starting at the only line touching the top of the diagram, it must go down, pass through A, and continue onward to the first +.
// Travel right, up, and right, passing through B in the process.
// Continue down (collecting C), right, and up (collecting D).
// Finally, go all the way left through E and stopping at F.
// Following the path to the end, the letters it sees on its path are ABCDEF.

// The little packet looks up at you, hoping you can help it find the way. What letters will it see (in the order it would see them) if it follows the path? (The routing diagram is very wide; make sure you view it without line wrapping.)

// Your puzzle answer was AYRPVMEGQ.


console.log(calc(`                                       |                                                                                                                                                                 
 +-----+         +-----+ +-----+ +-----|-----+   +-+           +-----------------------------------------------------------------------------------------+ +---------+       +-+               +-+       
 |     |         |     | |     | |     |     |   | |           |                                                                                         | |         |       | |               | |       
 |     | +-------+ +---+ |     +-------------|---+ +---------------------+ +-----------------------------------------------------------------------------|-----------|---------------+         +-|---+   
 |     | |         |     |       |     |     |                 |         | |                                                                             | |         |       | |     |           |   |   
 |   +---|-------+ +-----|---+ +-------------|---+             |         | |                                           +-----Q                   +-------|-|-----+   |     +-|-|-------+         |   |   
 |   | | |       |       |   | | |     |     |   |             |         | |                                           |                         |       | |     |   |     | | |     | |         |   |   
 |   | | |       |   +---------|-|-----------|-------+         |         +-----------------------------+               +-----+                   |       | |     +-----+   | | |     | |         |   |   
 |   | | |       |   |   |   | | |     |     |   |   |         |           |                           |                     |                   |       | |         | |   | | |     | |         |   |   
 |   | | |       |   |   |   | +-+ +---------|---+   |       +-+ +-----------------------------------------------------------|-------------------|-------|-----------|-----+ | |     | | +-------|-----+ 
 |   | | |       |   |   |   |     |   |     |       |       |   |         |                           |                     |                   |       | |         | |     | |     | | |       |   | | 
 |   | | |       |   +---|---|-+   |   |     |       |       |   |         |                 +---------------------------------+                 |     +-------------|-|-------------+ | |       | +-+ | 
 |   | | |       |       |   | |   |   |     |       |       |   |         |                 |         |                     | |                 |     | | |         | |     | |       | |       | |   | 
 |   +---+       |       |   | +---|-------------+   |       |   |         |           +-----|---------|---------------------|-|-----------------|-----|-|---+   +-------------|-----------------|-|-+ | 
 |     |         |       |   |     |   |     |   |   |       |   |         |           |     |         |                     | |                 |     | | | |   |   | |     | |       | |       | | | | 
 |     |         |       |   +---------|-------+ |   |       | +-----------------------------|-------------------------------|-+                 |     | +---+   |   | +---+ | | +---------------|-|-+ | 
 |     |         |       |         |   |     | | |   |       | | |         |           |     |         |                     |                   |     |   |     |   |     | | | |     | |       | |   | 
 |     |         |       |         |   |     | | |   |       | +-----------------------|-+   |     +-------------------------|-------------------------|---|---------|-----+ | | |     | |       | |   | 
 |     |         |       |         |   |     | | |   |       |   |         |           | |   |     |   |                     |                   |     |   |     |   |       | | |     | |       | |   | 
 |     |     +-----------|-------------|-----|-|-------------|---+         |           | +---|-----------------------+       +-------------+     |     |   |     |   |       | | |     | |       | | +-+ 
 |     |     |   |       |         |   |     | | |   |       |             |           |     |     |   |             |                     |     |     |   |     |   |       | | |     | |       | | |   
 |     |     |   |       |   +---------|-----|-|-+   |       +-------------------------------|---------------------------------------------------|-------------------|---------|-|-------+       | | |   
 |     |     |   |       |   |     |   |     | |     |                     |           |     |     |   |             |                     |     |     |   |     |   |       | | |     |         | | |   
 |     |     +---+   +-------|---------------+ | +---|---------------------------------|-----|-+   |   |             |                     | +---|-----+   |     |   |       | | |     |         | | |   
 |     |             |   |   |     |   |       | |   |                     |           |     | |   |   |             |                     | |   |         |     |   |       | | |     |         | | |   
 |     |       +-----+   |   | +-------|-------|---+ | +-------------------|-----------|-----|-|---|---------------------------------------|-|-------------------|---|-------|-|-----------------+ | |   
 |     |       |         |   | |   |   |       | | | | |                   |           |     | |   |   |             |                     | |   |         |     |   |       | | |     |           | |   
 | +-------------------------|---------|-----------|-----------------------------------|-----|-|---|---|-------------------------------------+   |         |     |   |       | | |     |           | |   
 | |   |       |         |   | |   |   |       | | | | |                   |           |     | |   |   |             |                     |     |         |     |   |       | | |     |           | |   
 | |   |       +-+   +-------|---------------+ | | | | |                   |           |     | |   +-+ |             |             +-----------------------|-----------------|---------|-+         | |   
 | |   |         |   |   |   | |   |   |     | | | | | |                   |           |     | |     | |             |             |       |     |         |     |   |       | | |     | |         | |   
 | +---|-+     +-+   |   |   | | +-|-----+   | | +-|-+ |                   |           |     | |     | |             |             |       |     |         |     |   |       | | |     | |         | |   
 |     | |     |     |   |   | | | |   | |   | |   |   |                   |           |     | |     | |             |             |       |     |         |     |   |       | | |     | |         | |   
 |     | |     +-----|-------|-----|---|-|---|-|---------------------------------------------|-|-----------------------------------|-------|-------------------------+       | | |     +-+         | |   
 |     | |           |   |   | | | |   | |   | |   |   |                   |           |     | |     | |             |             |       |     G         |     |           | | |                 | |   
 |     | |     +-----|-+ | +-|-|---------|---|-----|---|-------------------------------|-------------|-|-----+       |     +---------------------|-----------------------------|-|-----------------+ |   
 |     | |     |     | | | | | | | |   | |   | |   |   |                   |           |     | |     | |     |       |     |       |       |     |         |     |           | | |                   |   
 |     | |     | +-----|-|-|-----|-|---------------|-----------------------------------------|-------|---------------------|---------------------|---------|---------------+ | | |                   |   
 |     | |     | |   | | | | | | | |   | |   | |   |   |                   |           |     | |     | |     |       |     |       |       |     |         |     |         | | | |                   |   
 | +-------------------|-----|-|---------+   | |   |   | +-----------------------------|-----|-|-----|---------------------|---------------|---------------|-----|---------|-|-|-|-----+             |   
 | |   | |     | |   | | | | | | | |   |     | |   |   | |                 |           |     | |     | |     |       |     |       |       |     |         |     |         | | | |     |             |   
 | |   | |     | |   | | +---+ | +---------------------|-|---------------+ |           |     | |     | |     |       +-----|---------+     |     |       +-|-----|-----+   | | | |     |             |   
 | |   | |     | |   | |   |   |   |   |     | |   |   | |               | |           |     | |     | |     |             |       | |     |     |       | |     |     |   | | | |     |             |   
 | |   | |     | |   | |   |   |   |   |     | |   |   | |               | |           | +---|-|-------|-----|-------------|-------|-------------+   +-+ | +-----|---------+ | | |     |             |   
 | |   | |     | |   | |   |   |   |   |     | |   |   | |               | |           | |   | |     | |     |             |       | |     |         | | |       |     |     | | |     |             |   
 | |   | |     | |   | |   |   |   | +-------|---------|-|---------------|-------------+ |   | |     | |     |     +-------|-------------+ +---------+ | |       |     |     +-------+ |             |   
 | |   | |     | |   | |   |   |   | | |     | |   |   | |               | |             |   | |     | |     |     |       |       | |   |             | |       |     |       | |   | |             |   
 | |   | |     | |   | |   |   |   | | |     | |   |   | |               | |             |   | |     | |     |     |       |     +-----------------------|-------|-----+       | |   | |             |   
 | |   | |     | |   | |   |   |   | | |     | |   |   | |               | |             |   | |     | |     |     |       |     | | |   |             | |       |             | |   | |             |   
 | |   | |     | |   | |   |   | +---+ |     | |   |   | |               | | +-----------|-----------|-----+ |     |       |     +---|---|-------------|-|------------------P----|-+ | |             |   
 | |   | |     | |   | |   |   | | |   |     | |   |   | |               | | |           |   | |     | |   | |     |       |       | |   |             | |       |             | | | | |             |   
 | |   | |     | |   | |   |   | | |   |     | |   |   | |               | | |           |   | |     | |   | |     |     +-+       | |   | +---+       | |       |             | | | | |             |   
 | |   | |     | |   | |   |   | | |   |     | |   |   | |               | | |           |   | |     | |   | |     |     |         | |   | |   |       | |       |             | | | | |             |   
 | |   | |     | |   | | +-------------------|-|-----------------------+ | | |           |   | |     | |   | |     |     |         | |   | |   |       | |       |             | | | | |             |   
 | |   | |     | |   | | | |   | | |   |     | |   |   | |             | | | |           |   | |     | |   | |     |     |         | |   | |   |       | |       |             | | | | |             |   
 | | +---|-----|-------|-|-|-----|-+   |     | |   | +---|-------------------------------|---+ |     | |   | |     |     |         | |   | |   |       | | +---------------------|-----|---+         |   
 | | | | |     | |   | | | |   | |     |     | |   | | | |             | | | |           |     |     | |   | |     |     |         | |   | |   |       | | |     |             | | | | |   |         |   
 | | | | |     | +-------------|-|-----------|-|-----|-----------------|---|-|-----------|-----------|-+   | |     |     |         | +---|-----|-----------------------+       | | | | |   |         |   
 | | | | |     |     | | | |   | |     |     | |   | | | |             | | | |           |     |     |     | |     |     |         |     | |   |       | | |     |     |       | | | | |   |         |   
 | | | | |     |     | | | |   | |     |     | |   | | | |             | | | |           |     |     |     | |     |     |         |     | |   |       | | |     |     |       | | | | |   |         |   
 | | | | |     |     | | | |   | |     |     | |   | | | |             | | | |           |     |     |     | |     |     |         |     | |   |       | | |     |     |       | | | | |   |         |   
 | | | | |     |     | | | |   | |     |     | +---+ | | |             | | | |           |     |     |     | |     |     |         |     | | +-|-------|-|-|-----|-------------|-----+ |   | +---+   |   
 | | | | |     |     | | | |   | |     |     |       | | |             | | | |           |     |     |     | |     |     |         |     | | | |       | | |     |     |       | | |   |   | |   |   |   
 | | | | |     |     | | | |   | |     |     |       | | |       +-----|-|---|-----------|-----|-------+   | |     |     |         |     | | | |       | | |     |     |       | | |   |   | |   |   |   
 | | | | |     |     | | | |   | |     |     |       | | |       |     | | | |           |     |     | |   | |     |     |         |     | | | |       | | |     |     |       | | |   |   | |   |   |   
 | | | | |     |     | | | |   | |     |     |       | | |       |     | | | |       +-+ |     |   +-|-+   | |     |     |         |     | | | |       | | |     |     |   +---|---|---|---|-|---|-+ |   
 | | | | |     |     | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |       | | |     |     |   |   | | |   |   | |   | | |   
 | | | | |     |     | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |       | | |     |     |   |   | | |   |   | |   | | |   
 | | | | |     |     | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |       | | |     |     |   |   | | |   |   | |   | | R   
 | | | +-------|-----|-|-|-|-----|-----|---------------|-----------------|-|-------------------|---|---------|-----------|-------------------|-----+   | | |     |     |   |   | | |   |   | |   | | |   
 | | |   |     |     | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |   |   | | |     |     |   |   | | |   |   | |   | | |   
 | | |   |     |     | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |   |   | | |     |     |   |   | | |   | +---|---|---|-+ 
 | | |   |     |     | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |   |   | | |     |     |   |   | | |   | | | |   | | | | 
 | | |   |     |     | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |   |   | | |     |     |   |   | | |   | | | |   | | | | 
 | | |   |     |     | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |   |   | | |     |     |   |   | | |   | | | |   | | | | 
 | | +-----------+   | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |   | +-----------|---------+   | | |   | | | |   | | | | 
 | |     |     | |   | | | |   | |     |     |       | | |       |     | | | |       | | |     |   | |     | |     |     |         |     | | | |   | | | | |     |     |       | | |   | | | |   | | | | 
 | |     |     | |   | | | |   | |     |   +-|---------|-|---------------|-----------|---+     |   | |     | |     |     |         |     | | | |   | | | | |     |     |       | | |   | | | |   | | | | 
 | |     |     | |   | | | |   | |     |   | |       | | |       |     | | | |       | |       |   | |     | |     |     |         |     | | | |   | | | | |     |     |       | | |   | | | |   | | | | 
 | |     |     | | +-|-----|-----|---------|-----------|---------------|-|-----------|---------------+ +---|-----------------------|-----|-|-|-|-----|-----|---+ |     |       | | |   | | | |   | | | | 
 | |     |     | | | | | | |   | |     |   | |       | | |       |     | | | |       | |       |   |   |   | |     |     |         |     | | | |   | | | | |   | |     |       | | |   | | | |   | | | | 
 | |     |     | | | | | | | +-|-----+ |   | |       | | |       |     | | | |       | |       |   |   |   | |     |     |         |     | | | |   | | | | |   | |     |       | | |   | | | |   | | | | 
 | |     |     | | | | | | | | | |   | |   | |       | | |       |     | | | |       | |       |   |   |   | |     |     |         |     | | | |   | | | | |   | |     |       | | |   | | | |   | | | | 
 | |     |     | | | | | | | | | |   | |   | |       | | |       |     | | | |       | |       |   |   |   | |     |     |         |     | | | |   | +---|-|-----+     |       | | |   | | | |   | | | | 
 | |     |     | | | | | | | | | |   | |   | |       | | |       |     | | | |       | |       |   |   |   | |     |     |         |     | | | |   |   | | |   |       |       | | |   | | | |   | | | | 
 | |     |     | | | | | | | | | |   | |   | |       | | |       |     | | | |       | |       |   |   |   | |     |     |         |     | | | |   |   | | |   |       |       | | |   | | | |   | | | | 
 | |     |     | | | | | | | | | |   | |   | |       | | |       |     | | | |       | |       |   |   |   | |     |     |         |     | | | |   |   | | |   |       |       | | |   | | | |   | | | | 
 | |     |     | | | | | | | | +-------|---|---------|---|-------|-----|-|-|-------------------|---|-------+ |     |     |         |     | | | |   |   | | |   |       |     +-|-------|-|---|-+ | | | | 
 | |     |     | | | | | | | |   |   | |   | |       | | |       |     | | | |       | |       |   |   |     |     |     |         |     | | | |   |   | | |   |       |     | | | |   | | | | | | | | | 
 | |     |     | | | | | | | |   | +-|-|---+ |       | | |       |     | | | |       | |       |   |   |     |     |     |         |     | | | |   |   | | |   |       |     +-|-|---+ | | | | | | | | | 
 | |     |     | | | | | | | |   | | | |     |       | | |       |     | | | |       | |       |   |   |     |     |     |         |     | | | |   |   | | |   |       |       | | | | | | | | | | | | | 
 | | +---|-----|-|-----|-|-----------|---------------+ | |       |     | | | |       | |       |   |   |     +-----------|-----------------|-|-|---|---|-|-----------+ |       | | | | | | | | | | | | | 
 | | |   |     | | | | | | | |   | | | |     |         | |       |     | | | |       | |       |   E   |           |     |         |     | | | |   |   | | |   |     | |       | | | | | | | | | | | | | 
 | | |   |     | | | | | | | |   | | +-|-----|---------|-|-------------|-|-------------|-------|---|-------+       |     | +-------|-+   | | | |   |   | | |   | +---|-|-----+ | | | | | | | | | | | | | 
 | | |   |     | | | | | | | |   | |   |     |         | |       |     | | | |       | |       |   |   |   |       |     | |       | |   | | | |   |   | | |   | |   | |     | | | | | | | | | | | | | | 
 | | |   |     | | | | | | | |   | |   |     |         | |       |     | | | |       | |       |   |   |   |       |     | |       | |   | | | |   |   | | |   | |   | |     | | | | | | | | | | | | | | 
 | | |   |     | | | | | | | |   | |   |     |         | |       |     | | | |       | |       |   |   |   |       |     | |       | |   | | | |   |   | | |   | |   | |     | | | | | | | | | | | | | | 
 | | |   |     | | | | | | | |   | |   +---------------|-|-------|---+ | | | |       | |       |   |   |   |       |     | |       | | +---|-|-|---|-----|-|-----|---|-----------------|-|-|-|-|-|-+ | | 
 | | |   |     | | | | | | | |   | |         |         | |       |   | | | | |       | |       |   |   |   |       |     | |       | | | | | | |   |   | | |   | |   | |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |   +-|---------|---------|-|-------|---|-|---------------|-------|---|---------------|-----+ |       | | | | | | |   |   | | |   +-|-+ | |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |     |         |         | |       |   | | | | |       | |       |   |   |   |       |       |       | | | | | | |   |   | | |     | | | |     | | | | | | | | | | |   | | 
 | | |   | +---|-|-|---------------+ +-------|-+       | |       |   | | | | |       | | +-----|---|---------------|-----+ |       | | | | | | |   +---------+   | | | |     | | | | | | | | | | |   | | 
 | | |   | |   | | | | | | | |       |       | |       | |       |   | | | | |       | | |     |   |   |   |       |     | |       | | | | | | |       | | | |   | | | |     | | | | | | | | | | |   | | 
 | | |   | |   | | | | | | | |       |       | |       | |       |   | | | | |       | | |     |   |   |   |       |     | |       | | | | | | |     +---|---|-+ | | | |     | | | | | | | | | | |   | | 
 | | |   | |   | | | | | | | |       |       | |       | |       |   | | | | |       | | |     |   |   |   |       |     | |       | | | | | | |     | | | | | | | | | |     | | | | | | | | | | |   | | 
 | | |   | |   | | | | | | | |       |       | |       | |       | +---|---------------|-----------|---|-------------------|-------|-|-|-------------|---|-+ | | | | | |     | | | | | | | | | | |   | | 
 | | |   | |   | | | | | | | |       |       | |       | |       | | | | | | |       | | |     |   |   |   |       |     | |       | | | | | | |     | | |   | | | | | |     | | | | | | | | | | |   | | 
 | | | +-|-|---|-|-|-|-|-----|-----------------|---------|-------|---|-|---|---------|---------|---|---+   |       |     | |       | | | | | | |     | | |   | | | | | |     | | | | | | | | | | |   | | 
 | | | | | |   | | | | | | | |       |       | |       | |       | | | | | | |       | | |     |   |       |       |     | |       | | | | | | |     | | |   | | | | | |     | | | | | | | | | | |   | | 
 | | | +---+   | | | | | | | |       |       | |       | +-------|---|-|-------------|---|-------------------------|-----|-|-------|-|---|-|---|-------|---------|---+ |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |       |       | |       |         | | | | | | |       | | |     |   |       |       |     | |       | | | | | | |     | | |   | | | |   |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |       |       | |       |         | | | | | | |       | | |     |   |       |       |     | |       +-|-|-|-----|----V|-|---+ | | | |   |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |       |       | |       |         | | | | | | |       | | |     |   |       |       |     | |         | | | | | |     | | | | | | | |   |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |       |       | |       |         | | | | | | |       | | |     |   |       |       |     | |         | | | | | |     | | | | | | | |   |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |       |       | |       |         | | | | | | |       | | |     |   |       |       |     | |         | | | | | |     | | | | | | | |   |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |       |       | |       |         | | +-|---|---------|-----+   |   |       |       |     | |         | | | | | |     | | | | | | | |   |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |       |       | |       |         | |   | | | |       | | | |   |   |       |       |     | |         | | | | | |     | | | | | | | |   |     | | | | | | | | | | |   | | 
 | | |   |     | | | | | | | |       |       | |       |         | |   | +-|-|-------------|---|-----------|-----------------------------|-|-------+ | +-|---|-+ | |   |     | | +---|-|-------|---+ | | 
 | | |   |     | | | | | | | |       |       | |       |         | |   |   | |       | | | |   |   |       |       |     | |         | | | | | |   | |   | | |   | |   |     | |   | | | | | | | | | | | 
 | | |   |     | | | | | | | |       |       | |       |         | |   |   | |       | | | |   |   |       |       |     | |         | | | | | |   | |   | | |   | |   |     | |   | | | | | | | | | | | 
 | | |   |     | | | | | | | |       |       | |       |         | |   |   | |       | | | |   |   |       |       |     | |         | | | | | |   | |   | | |   | |   |     | |   | | | | | | | | | | | 
 | | |   |     | | | | | | | |       |       | |       | +-------|-|---|---|-|-------|---|---------|-------|-------|-----------------|---------|---|-+   | | |   | |   |     | |   | | | | | | | | | | | 
 | | |   |     | | | | | | | |       |       | |       | |       | |   |   | |       | | | |   |   |       |       |     | |         | | | | | |   |     | | |   | |   |     | |   | | | | | | | | | | | 
 | | |   |     | | | | | | | |       |       | |       | |       | |   | +---|-----------|---------+       |       |     | |         | | | | | |   |     | | |   | |   |     | |   | | | | | | | | | | | 
 | | |   |     | | | | | | | |       |       | |       | |       | |   | | | |       | | | |   |           |       |     | |         | | | | | |   |     | | |   | |   |     | |   | | | | | | | | | | | 
 | | |   |     | | | | | | | |       |       | |       | |       | |   | | | |       | | | |   |           |       |     | |         | | | | | |   |     | | |   | |   |     | |   | | | | | | | | | | | 
 | | |   |     | | | | | | | |       |       | |       | |       | |   | | | |       | | | |   |           |       |     | |         | | | | | |   |     | | |   | |   |     | |   | | | | | | | | | | | 
 | +-------------------|-|-|-----------------|-|-------|-|-------|-|---|-|-|---------|---------|-----------|-------|-----------------|-|-|-----------------|-|---|-----------|-|-----|---|---|-|-+ | | | 
 |   |   |     | | | | | | | |       |       | |       | |       | |   | | | |       | | | |   |           |       |     | |         | | | | | |   |     | | |   | |   |     | |   | | | | | | |   | | | 
 |   |   |     | | | | | | | |   +-----------|---------+ |       | |   | | | +-----------|-|---------------|-------|-----+ |         | | | | | |   |     | | |   | |   |     | |   | | | | | | |   | | | 
 |   |   |     | | | | | | | |   |   |       | |         |       | |   | | |         | | | |   |           |       |       |         | | | | | |   |     | | |   | |   |     | |   | | | | | | |   | | | 
 |   |   |   +-------|-|-+ | |   | +-+       | |         |       | |   | | |         | | | |   |           |       |       |         | | | | | |   |     | | |   | |   |     | |   | | | | | | |   | | | 
 |   |   |   | | | | | |   | |   | |         | |         |       | |   | | |         | | | |   |           |       |       |         | | | | | |   |     | | |   | |   |     | |   | | | | | | |   | | | 
 |   |   |   | | | | | |   | |   | |         | |         |       | |   | | |         | | | |   |           |       |       |         | | | | | |   |     | | |   | |   |     | |   | | | | | | |   | | | 
 |   |   |   | | | | | |   | |   | |         | |         |       | |   | | |         | | | |   |           |       |       |         | | | | | |   |     | | |   | |   |     | |   | | | | | | |   | | | 
 |   |   |   | | | | | |   | |   | |         | |         |       | |   | | |         | | | |   |           |   +---|-+     |         | +-|-|---|---|---------------|---------------|-----+ | | |   | | | 
 |   |   |   | | | | | |   | |   | |         | |         |       | |   | | |         | | | |   |           |   |   | |     |         |   | | | |   |     | | |   | |   |     | |   | | |   | | |   | | | 
 |   |   |   | | | | | |   | |   | |         | |       +---------|-|---|---|---------------|---|---------------|---|-+     |         |   | | | |   |     | +-|---|-|-----------|---|-|-----------+ | | | 
 |   |   |   | | | | | |   | |   | |         | |       | |       | |   | | |         | | | |   |           |   |   |       |         |   | | | |   |     |   |   | |   |     | |   | | |   | | | | | | | 
 +---------------------|-----|-----------------|-------|-|---------|-------|-----------|-|-|---|-----------|A--|-------------------------|-|-|-|---------|-------|-----|-----|-----|---+   | | +-|-----+ 
     |   |   | | | | | |   | |   | |         | |       | |       | |   | | |         | | | |   |           |   |   |       |         |   | | | |   |     |   |   | |   |     | |   | |     | |   | | |   
 +---|----M--|-|---|---|---|-------|---------|-----------|-------|-----+ | |         | | | |   |           |   |   |       |         |   | | | +---|---------------|---------|-|---|-|-----|-----+ | |   
 |   |   |   | | | | | |   | |   | |         | |       | |       | |     | |         | | | |   |           |   |   |       |         |   | | |     |     |   |   | |   |     | |   | |     | |     | |   
 +---+   | +-|-|-|-|-|-|-----------|-----------|-------|-|---------------|-----------|-----|---|-----------|---|---|-------|---------|---|---------------|---|---|-----|-----|-----|-|-----|-|-------|-+ 
         | | | | | | | |   | |   | |         | |       | |       | |     | |         | | | |   |           |   |   |       |         |   | | |     |     |   |   | |   |     | |   | |     | |     | | | 
         | | | | | | | |   | |   | |         | |       | |       | |     | |         | | | |   |           |   |   |       |         |   | | |     |     |   |   | |   |     | |   | |     | |   +-+ | | 
         | | | | | | | |   | |   | |         | |       | |       | |     | |         | | | |   |           |   |   |       |         |   | | |     |     |   |   | |   |     | |   | |     | |   |   | | 
         | | | | | | | |   | |   | |         | |       | |       | |     | |         | | | |   |           |   |   |       |         |   | | |     |     |   |   | |   |     | |   | |     | |   |   | | 
         | | | | | | | |   | |   | |         | |       | |       | |     | |         | | | |   |           |   |   |       |         |   | | |     |     |   |   | |   |     | |   | |     | |   |   | | 
         | | | | | | | +-----|-----------------|-----------------|-------------------|-|-|-|---------------|---------------------------------------|---------|-----|-----------|---+ |     | |   |   | | 
         | | | | | | |     | |   | |         | |       | |       | |     | |         | | | |   |           |   |   |       |         |   | | |     |     |   |   | |   |     | |     |     | |   |   | | 
         | | | | | | |     | |   +-|---------|---------|---------------------------------|-----|-------------------------------------+ +---|-|-----+   +-|---|---------|-----|---------------|---+   | | 
         | | | | | | |     | |     |         | |       | |       | |     | |         | | | |   |           |   |   |       |           | | | |         | |   |   | |   |     | |     |     | |       | | 
         | | | | | | |     | |     |         | |       | |       | |     | |         | | | |   |           |   |   |       |           | | | |         | |   |   | |   |     | |     |     | |       | | 
         | | | | | | |     | |     |         | |       | |       | |     | |         | | | |   |           |   |   |       |           | | | |         | |   |   | |   |     | |     |     | |       | | 
         | | | | | +-|-----|-------+         | |       | +-------|---------|-----------|-----------------------|-----------------------------|---------|-----|---|-|---------|-|-----|---------------|-+ 
         | | | | |   |     | |               | |       |         | |     | |         | | | |   |           |   |   |       |           | | | |         | |   |   | |   |     | |     |     | |       |   
         | | | | |   |     | |               | |       |         | |   +-+ |         | | | |   |           |   |   |       |           | | | |         | |   |   | |   |     | |     |     | |       |   
         | | | | |   |     | |               | |       |         | |   |   |         | | | |   |           |   |   |       |           | | | |         | |   |   | |   |     | |     |     | |       |   
         | | | | |   +-----|----Y------------------+   |         | |   |   |         | | | |   |           |   +-------------+         | | | |         | |   | +-|-+   |     | |     |     | |       |   
         | | | | |         | |               | |   |   |         | |   |   |         | | | |   |           |       |       | |         | | | |         | |   | | |     |     | |     |     | |       |   
         | | | | |         | |               +-|---|-------------|---+ |   |         | | | |   |           |       |       | +---------|-----------------|-----+ |     |     | |     |     | |       |   
         | | | | |         | |                 |   |   |         | | | |   |         | | | |   |           |       |       |           | | | |         | |   |   |     |     | |     |     | |       |   
         | | | | |         | |     +-----------|---|-----------------|-|-------------|-|-|-|---|-----------|-------|-------+           | | | |         | |   |   |     |     | |     |     | |       |   
         | | | | |         | |     |           |   |   |         | | | |   |         | | | |   |           |       |                   | | | |         | |   |   |     |     | |     |     | |       |   
         | | | | |         | |     |           |   |   |         | +---|---|-------------------|-----------|-------|-------------------|-|---|-----------|-------|-------------------|-------|-------|-+ 
         | | | | |         | |     |           |   |   |         |   | |   |         | | | |   |           |       |                   | | | |         | |   |   |     |     | |     |     | |       | | 
         | | | +-------------------+           |   |   |         |   | |   |         | | | |   |           |       |                   | | | |         | |   |   |     |     | |     |     | |       | | 
         | | |   |         | |                 |   |   |         |   | |   |         | | | |   |           |       |                   | | | |         | |   |   |     |     | |     |     | |       | | 
         | | |   |   +-------|-----+           |   |   |         +---|-----------------|-|-----+           |       +-+                 | | | |         | |   |   |     |     | |     +---+ | |       | | 
         | | |   |   |     | |     |           |   |   |             | |   |         | | | |               |         |                 | | | |         | |   |   |     |     | |         | | |       | | 
         | +-|---|-------------------------------------+             | |   |         | | | |               |         +-------------------|---|-----------|---|---|-----|-------|-----------|-|-------+ | 
         |   |   |   |     | |     |           |   |                 | |   |         | | | |               |                           | | | |         | |   |   |     |     | |         | | |         | 
     +---|-----+ |   |     | |     |           |   |                 | |   |         | | | |               |                           +-|-------------|-|---|---+     +-----+ |         | | |         | 
     |   |   | | |   |     | |     |           |   |                 | |   |         | | | |               |                             | | |         | |   |                 |         | | |         | 
     |   |   | | |   |     | |     |           |   |                 | |   |         | | | | +-------------|---------+                   | | |         | |   |                 |         | | |         | 
     |   |   | | |   |     | |     |           |   |                 | |   |         | | | | |             |         |                   | | |         | |   |                 |         | | |         | 
     |   |   | | |   |     | |   +-----------------|-------------------+   |         | | | | |             |         +-------------------|-------------|-|---------------------|---------+ | |         | 
     |   |   | | |   |     | |   | |           |   |                 |     |         | | | | |             |                             | | |         | |   |                 |           | |         | 
     |   |   | | |   |     | |   | |           +---|-----------------|-----|-----------|-|-|-+             |                             | | |         | |   |                 |           | |         | 
     |   |   | | |   |     | |   | |               |                 |     |         | | | |               |                             | | |         | |   |                 |           | |         | 
     |   |   | | |   |     | |   | |               |                 |     |         +---------------------------------------------------+ | |         | | +-|-----------------|-----------|-|---------+ 
     |   |   | | |   |     | |   | |               |                 |     |           | | |               |                               | |         | | | |                 |           | |           
     |   |   | | |   |     | |   | |               |                 |     |           | | |           +---|-------------------------------|-|---------|-|---------------------+           | |   +-+     
     |   |   | | |   |     | |   | |               |                 |     |           | | |           |   |                               | |         | | | |                             | |   | |     
 +---|---|-------+   |     | |   | +---------------------------------|-----|-----------+ | |           |   |                               +-----------|-|---------------------------------|-+   | |     
 |   |   |   | |     |     | |   |                 |                 |     |             | |           |   |                                 |         | | | |                             |     | |     
 |   |   |   +-+     |     +-|---|-----------------|-----------------------------------------------------------------------------------------------------|---|-----------------------------+     | |     
 |   |   |           |       |   |                 |                 |     |             | |           |   |                                 |         | | | |                                   | |     
 +---+   |           |       |   |                 |                 +-------------------|-----------------|-------------------------------------------|---+ +-------------------------------------+     
         |           |       |   |                 |                       |             | |           |   |                                 |         | |                                       |       
 +---+   |   +-------|-------|---|-+               |                       |             | +---------------|---------------------------------|---------|-|-----------------------------------+   |       
 |   |   |   |       |       |   | |               |                       |             |             |   |                                 |         | |                                   |   |       
 +---|---------------|-------|---|-----------------|-----------------------|-------------|-------------|---|---------------------------------|---------+ |                                   |   |       
     |   |   |       |       |   | |               |                       |             |             |   |                                 |           |                                   |   |       
     |   |   |       |       |   +-----+           |                       |             |             +---|-+                               +-----------+                                   |   |       
     |   |   |       |       |     |   |           |                       |             |                 | |                                                                               |   |       
     +---|---+       +-------+     +---+           |                       +-------------|-----------------+ +---------------------------------------------------+                           +---+       
         |                                         |                                     |                                                                       |                                       
         +-----------------------------------------+                                     +-----------------------------------------------------------------------+                                       
                                                                                                                                                                                                         
`));

function calc(input){
    const map = processInput(input);
    const currPos = getStartingPoint(map);
    let dir = 'DOWN';
    const letters = [];

    while(true) {
        if( hasStraightPath(map, currPos, dir) ) advance(map, currPos, dir, letters);
        else if( hasValidTurn(map, currPos, dir) ) {
            dir = turn(map, currPos, dir);
            advance(map, currPos, dir, letters);
        }
        else {console.log(`Ended at (x,y) (${currPos}) going ${dir}`); return letters.join('');}
    }
}

function processInput(input){
    return input.split('\n').map( line=> line.split('') );
}

function getStartingPoint(map){
    return [0, map[0].findIndex(_=>_==='|')];
}

function advance(map, currPos, dir, letters){
    switch(dir){
        case 'DOWN': currPos[0]++; break;
        case 'UP': currPos[0]--; break;
        case 'RIGHT': currPos[1]++; break;
        case 'LEFT': currPos[1]--; break;
    }
    if( map[currPos[0]][currPos[1]] >= 'A' && map[currPos[0]][currPos[1]] <= 'Z' ) letters.push(map[currPos[0]][currPos[1]]);
}

function hasStraightPath(map, currPos, dir){
    const i= currPos[0];
    const j= currPos[1];
    switch(dir){
        case 'DOWN': if( map[i+1][j] && map[i+1][j] !== ' ') return true; else break;
        case 'UP': if( map[i-1][j] && map[i-1][j] !== ' ') return true; else break;
        case 'RIGHT': if( map[i][j+1] && map[i][j+1] !== ' ') return true; else break;
        case 'LEFT': if( map[i][j-1] && map[i][j-1] !== ' ') return true; else break;
    }
    return false;
}

function hasValidTurn(map, currPos, dir){
    const i= currPos[0];
    const j= currPos[1];
    switch(dir){
        case 'DOWN':
        case 'UP': if( (map[i][j+1] && map[i][j+1] !== ' ') || (map[i][j-1] && map[i][j-1] !== ' ') ) return true; else break;
        case 'RIGHT':
        case 'LEFT': if( (map[i+1][j] && map[i+1][j] !== ' ') || (map[i-1][j] && map[i-1][j] !== ' ') ) return true; else break;
    }
    return false
}

function turn(map, currPos, dir){
    const i= currPos[0];
    const j= currPos[1];
    switch(dir){
        case 'DOWN':
        case 'UP': if(map[i][j+1] && map[i][j+1]!==' ') return 'RIGHT'; else if(map[i][j-1] && map[i][j-1]!==' ') return 'LEFT'; else break;
        case 'RIGHT':
        case 'LEFT': if(map[i+1][j] && map[i+1][j]!==' ') return 'DOWN'; else if(map[i-1][j] && map[i-1][j]!==' ') return 'UP'; else break;
    }
}

Comments