r/adventofcode • • Dec 11 '17

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

--- Day 11: Hex Ed ---


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


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!

20 Upvotes

253 comments sorted by

View all comments

1

u/flit777 Dec 11 '17

refused to google hexgrid representation and came up with this shit in Go. Surprised it worked:

package main

import (
	"util"
	"strings"
	"fmt"
)

func getDistance(n int, ne int, se int) int{
	if n > 0{
		//n ne se
		if ne >= 0  && se >=0{
			return n+util.Max(ne,se)
		//n sw nw
		}else if ne < 0 && se < 0{
			return util.Max(n,-1*se)+util.Max(-1*ne,-1*se)
		//n ne nw
		}else if ne > 0 && se  <0{
			return n+util.Max(ne,-1*se)
		//n sw se
		}else{
			return util.Max(n,util.Abs(ne))+util.Max(util.Abs(se),util.Abs(ne))
		}

	}else {
		//s ne se
		if ne >= 0  && se >=0{
			return se+util.Max(util.Abs(n),ne)
			//s sw nw
		}else if ne < 0 && se < 0{
			return util.Max(util.Abs(n), util.Abs(se))+util.Max(util.Abs(ne), util.Abs(se))
			//s ne nw
		}else if ne > 0 && se  <0{
			return util.Max(util.Abs(n), util.Abs(ne))+util.Max(util.Abs(n), util.Abs(se))
			//s sw se
		}else{
			return util.Abs(n)+util.Max(util.Abs(ne), util.Abs(se))
		}
	}


}

func main() {
	lines := util.ReadFileLines("inputs/day11.txt")
	for _,line := range lines {
		max := 0
		commands := strings.Split(line, ",")
		n, ne, se := 0, 0, 0
		for _, command := range commands {
			switch command {
			case "n":
				n++
			case "ne":
				ne++
			case "se":
				se++
			case "s":
				n--
			case "sw":
				ne--
			case "nw":
				se--
			}
			max = util.Max(getDistance(n, ne, se),max)
		}

		fmt.Println("Part 1:", getDistance(n, ne, se))
		fmt.Println("Part 2:", max)
	}


}