aoc/aoc2015/day03.go

72 lines
990 B
Go

package aoc2015
import (
"aoc/internal/utility"
"errors"
"fmt"
)
func day03part1(in string) {
var x, y int
houses := make(map[utility.Point]int)
houses[utility.NewPoint(0, 0)]++
for _, c := range in {
switch c {
case '<':
x--
case '>':
x++
case 'v':
y--
case '^':
y++
default:
utility.E(errors.New("unknown input"))
}
houses[utility.NewPoint(x, y)]++
}
fmt.Println(len(houses))
}
func day03part2(in string) {
var (
sx, sy, rx, ry int
x, y *int
)
houses := make(map[utility.Point]int)
houses[utility.NewPoint(0, 0)]++
for i, c := range in {
if i%2 == 0 {
x = &sx
y = &sy
} else {
x = &rx
y = &ry
}
switch c {
case '<':
*x--
case '>':
*x++
case 'v':
*y--
case '^':
*y++
default:
utility.E(errors.New("unknown input"))
}
houses[utility.NewPoint(*x, *y)]++
}
fmt.Println(len(houses))
}
func day03() {
in := utility.GetInput("input/2015/03")
day03part1(in)
day03part2(in)
}