aoc/aoc2015/day03.go

72 lines
990 B
Go
Raw Normal View History

2024-07-24 14:29:59 +00:00
package aoc2015
2024-07-24 18:03:39 +00:00
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))
}
2024-07-24 14:29:59 +00:00
func day03() {
2024-07-24 18:03:39 +00:00
in := utility.GetInput("input/2015/03")
day03part1(in)
day03part2(in)
2024-07-24 14:29:59 +00:00
}