adventofcode/2023/day18/main.go

64 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-12-18 12:54:10 +00:00
package main
import (
"fmt"
"strconv"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
part1(inp)
fmt.Println()
part2(inp)
}
func part1(input []string) {
dirMap := map[byte]h.Coordinate{
'U': h.Coordinate{X: 0, Y: -1},
'R': h.Coordinate{X: 1, Y: 0},
'D': h.Coordinate{X: 0, Y: 1},
'L': h.Coordinate{X: -1, Y: 0},
}
curr, area := h.Coordinate{X: 0, Y: 0}, 0
for i := range input {
pts := strings.Fields(input[i])
dir, length, _ := pts[0][0], h.Atoi(pts[1]), strings.Trim(pts[1], "()")
n := curr.Add(dirMap[dir].Mul(length))
area += curr.X*n.Y - curr.Y*n.X + length
curr = n
}
fmt.Println("# Part 1")
fmt.Println(area/2 + 1)
}
func part2(input []string) {
dirMap := map[byte]h.Coordinate{
'3': h.Coordinate{X: 0, Y: -1},
'0': h.Coordinate{X: 1, Y: 0},
'1': h.Coordinate{X: 0, Y: 1},
'2': h.Coordinate{X: -1, Y: 0},
}
hexToDec := func(hex string) int {
v, _ := strconv.ParseInt(hex, 16, strconv.IntSize)
return int(v)
}
curr, area := h.Coordinate{X: 0, Y: 0}, 0
for i := range input {
pts := strings.Fields(input[i])
pts[2] = strings.Trim(pts[2], "#()")
length := hexToDec(pts[2][:len(pts[2])-1])
dir := pts[2][len(pts[2])-1:][0]
n := curr.Add(dirMap[dir].Mul(length))
area += curr.X*n.Y - curr.Y*n.X + length
curr = n
}
fmt.Println("# Part 2")
fmt.Println(area/2 + 1)
}