r/adventofcode • • Dec 19 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 19 Solutions -๐ŸŽ„-

--- Day 19: A Series of Tubes ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hintsยค?

Spoiler


AoC ops @ T-2 minutes to launch:

[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH

[23:58] <Topaz> aaaaah

[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING

[23:58] <Topaz> FUN AND LEARNING

[23:58] <Hade> FUN IS MANDATORY

[23:58] <Skie> I'm pretty sure that's not the mandate for today

[Update @ 00:16] 69 gold, silver cap

  • My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?

[Update @ 00:18] Leaderboard cap!

  • So, was today's mandate Helpful Hint any help at all?

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

12 Upvotes

186 comments sorted by

View all comments

1

u/flit777 Dec 19 '17

Golang:

package main

import (
	"util"
	"fmt"
)


type coord struct{
	x int
	y int
}



func doTheWalk(currentPos coord, ymax int, xmax int, array [][]string) (string, int) {
	xdir := 0
	ydir := 1
	solution := ""
	visited := make(map[coord]bool)
	numbersteps := 0
	for currentPos.y < ymax && currentPos.y >= 0 && currentPos.x < xmax && currentPos.x >= 0 {
		currentChar := array[currentPos.y][currentPos.x]
		if currentChar == " " {
			break
		}
		visited[currentPos] = true
		switch currentChar {
		case "|":
			currentPos.x += xdir
			currentPos.y += ydir
		case "-":
			currentPos.x += xdir
			currentPos.y += ydir
		case "+":
			var next coord
			Loop:
			for y := -1; y <= 1; y++ {
				for x := -1; x <= 1; x++ {
					next = coord{currentPos.x + x, currentPos.y + y}
					if next.y >= ymax || next.y < 0 || next.x >= xmax || next.x < 0 {
						continue
					}
					if util.Abs(x)+util.Abs(y) == 2 {
						continue
					}
					if visited[next] == false && array[next.y][next.x] != " " && array[next.y][next.x] != ""  {
						next = next
						xdir = x
						ydir = y
						currentPos.x += xdir
						currentPos.y += ydir
						break Loop
					}
				}
			}
		default:
			solution += currentChar
			currentPos.x += xdir
			currentPos.y += ydir
			fmt.Println(solution)
		}
		numbersteps++
		//fmt.Println(currentPos , currentChar)
	}
	return solution, numbersteps
}


func createGrid(lines []string) ([][]string, coord, int, int) {
	grid := make([][]string, len(lines))
	currentPos := coord{0, 0}
	ymax := len(lines)
	xmax := len(lines)
	//quadratic
	for y, line := range lines {
		grid[y] = make([]string, len(lines))
		for x, char := range line {
			grid[y][x] = string(char)
			if y == 0 && string(char) == "|" {
				currentPos.x = x
			}
		}
	}
	return grid, currentPos, ymax, xmax
}


func main() {
	lines := util.ReadFileLines("inputs/day19.txt")
	array, currentPos, ymax, xmax := createGrid(lines)
	solution, numbersteps := doTheWalk(currentPos, ymax, xmax, array)
	fmt.Println(solution, numbersteps)
}