Day 1 Complete

This commit is contained in:
Brian Buller 2016-12-01 07:46:32 -06:00
parent c80bc2991c
commit eecc3eb368
4 changed files with 187 additions and 0 deletions

BIN
day01/day01 Executable file

Binary file not shown.

1
day01/input Normal file
View File

@ -0,0 +1 @@
R3, L5, R2, L1, L2, R5, L2, R2, L2, L2, L1, R2, L2, R4, R4, R1, L2, L3, R3, L1, R2, L2, L4, R4, R5, L3, R3, L3, L3, R4, R5, L3, R3, L5, L1, L2, R2, L1, R3, R1, L1, R187, L1, R2, R47, L5, L1, L2, R4, R3, L3, R3, R4, R1, R3, L1, L4, L1, R2, L1, R4, R5, L1, R77, L5, L4, R3, L2, R4, R5, R5, L2, L2, R2, R5, L2, R194, R5, L2, R4, L5, L4, L2, R5, L3, L2, L5, R5, R2, L3, R3, R1, L4, R2, L1, R5, L1, R5, L1, L1, R3, L1, R5, R2, R5, R5, L4, L5, L5, L5, R3, L2, L5, L4, R3, R1, R1, R4, L2, L4, R5, R5, R4, L2, L2, R5, R5, L5, L2, R4, R4, L4, R1, L3, R1, L1, L1, L1, L4, R5, R4, L4, L4, R5, R3, L2, L2, R3, R1, R4, L3, R1, L4, R3, L3, L2, R2, R2, R2, L1, L4, R3, R2, R2, L3, R2, L3, L2, R4, L2, R3, L4, R5, R4, R1, R5, R3

109
day01/main.go Normal file
View File

@ -0,0 +1,109 @@
package main
import (
"fmt"
"io"
"log"
"math"
"strconv"
"strings"
)
const (
north = iota
east
south
west
)
type pos struct {
dir int
x int
y int
}
func (s *pos) TurnLeft() {
s.dir++
if s.dir > west {
s.dir = north
}
}
func (s *pos) TurnRight() {
s.dir--
if s.dir < north {
s.dir = west
}
}
func (s *pos) CoordsToString() string {
return strconv.Itoa(s.x) + "-" + strconv.Itoa(s.y)
}
func (s *pos) DistanceTravelled() int {
return int(math.Abs(float64(s.y)) + math.Abs(float64(s.x)))
}
func (s *pos) Walk(dist int) {
switch s.dir {
case north:
s.y += dist
case east:
s.x += dist
case south:
s.y -= dist
case west:
s.x -= dist
}
}
func main() {
var input string
var readInp string
travelLog := make(map[string]int)
firstRepeat := false
currPos := pos{dir: north, x: 0, y: 0}
travelLog[currPos.CoordsToString()]++
for {
_, err := fmt.Scan(&readInp)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
input += readInp
}
for _, k := range strings.Split(input, ",") {
k = strings.Trim(k, " ")
if k[0] == 'R' {
currPos.TurnRight()
} else {
currPos.TurnLeft()
}
tDist := atoi(k[1:])
for mvI := 0; mvI < tDist; mvI++ {
currPos.Walk(1)
if !firstRepeat && travelLog[currPos.CoordsToString()] > 0 {
fmt.Println(" First Repeated Position: " + currPos.CoordsToString())
fmt.Println(" Distance: ", currPos.DistanceTravelled())
firstRepeat = true
}
travelLog[currPos.CoordsToString()]++
}
}
fmt.Println("End Position: ", currPos.x, ",", currPos.y)
fmt.Println(" Distance: ", currPos.DistanceTravelled())
}
func atoi(inp string) int {
var err error
var i int
if i, err = strconv.Atoi(inp); err != nil {
log.Fatal(err.Error())
}
return i
}

77
day01/problem Normal file
View File

@ -0,0 +1,77 @@
Advent of Code
• [About]
• [AoC++]
• [Events]
• [Settings]
• [Log Out]
br0xen (AoC++)
          2016
• [Calendar]
• [Leaderboard]
• [Stats]
• [Sponsors]
Our sponsors help make AoC possible:
Novetta - Unleash your imagination. Innovate at Novetta.
--- Day 1: No Time for a Taxicab ---
Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated
by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you
to retrieve all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the
second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you
can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody
had time to work them out further.
The Document indicates that you should start at the given coordinates (where you just landed) and face North.
Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given
number of blocks, ending at a new intersection.
There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the
destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the
destination?
For example:
 Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away.
 R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away.
 R5, L5, R5, R3 leaves you 12 blocks away.
How many blocks away is Easter Bunny HQ?
To begin, get your puzzle input.
Answer: _____________________ [ [Submit] ]
You can also [Shareon Twitter Google+ Reddit] this puzzle.
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2016/about
. http://adventofcode.com/2016/support
. http://adventofcode.com/2016/events
. http://adventofcode.com/2016/settings
. http://adventofcode.com/2016/auth/logout
. http://adventofcode.com/2016
. http://adventofcode.com/2016
. http://adventofcode.com/2016/leaderboard
. http://adventofcode.com/2016/stats
. http://adventofcode.com/2016/sponsors
. http://adventofcode.com/2016/sponsors
. http://www.novetta.com/careers/#opportunities
. https://en.wikipedia.org/wiki/Taxicab_geometry
. http://adventofcode.com/2016/day/1/input
. https://twitter.com/intent/tweet?text=%22No+Time+for+a+Taxicab%22+%2D+Day+1+%2D+Advent+of+Code+2016&url=http%3A%2F%2Fadventofcode%2Ecom%2F2016%2Fday%2F1&related=ericwastl&hashtags=AdventOfCode
. https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2F2016%2Fday%2F1
. http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2F2016%2Fday%2F1&title=%22No+Time+for+a+Taxicab%22+%2D+Day+1+%2D+Advent+of+Code+2016