mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
2015-day3: moving santa & a robot around a square grid
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var part int
|
||||
flag.IntVar(&part, "part", 1, "part 1 or 2")
|
||||
flag.Parse()
|
||||
fmt.Println("Running part", part)
|
||||
|
||||
var ans int
|
||||
if part == 1 {
|
||||
ans = part1(util.ReadFile("./input.txt"))
|
||||
} else {
|
||||
ans = part2(util.ReadFile("./input.txt"))
|
||||
}
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
|
||||
var dirs = map[string][2]int{
|
||||
"^": [2]int{-1, 0}, // row then col
|
||||
"v": [2]int{1, 0},
|
||||
"<": [2]int{0, -1},
|
||||
">": [2]int{0, 1},
|
||||
}
|
||||
|
||||
func part1(input string) int {
|
||||
houseCount := map[[2]int]int{[2]int{}: 1}
|
||||
coord := [2]int{0, 0}
|
||||
for _, char := range strings.Split(input, "") {
|
||||
diff := dirs[string(char)]
|
||||
nextCoord := [2]int{
|
||||
coord[0] + diff[0],
|
||||
coord[1] + diff[1],
|
||||
}
|
||||
coord = nextCoord
|
||||
houseCount[coord]++
|
||||
}
|
||||
return len(houseCount)
|
||||
}
|
||||
|
||||
func part2(input string) int {
|
||||
houseCount := map[[2]int]int{[2]int{}: 2}
|
||||
santaCoord := [2]int{0, 0}
|
||||
robotCoord := [2]int{0, 0}
|
||||
for i, char := range strings.Split(input, "") {
|
||||
diff := dirs[string(char)]
|
||||
if i%2 == 0 {
|
||||
nextSantaCoord := [2]int{
|
||||
santaCoord[0] + diff[0],
|
||||
santaCoord[1] + diff[1],
|
||||
}
|
||||
santaCoord = nextSantaCoord
|
||||
houseCount[santaCoord]++
|
||||
} else {
|
||||
nextRobotCoord := [2]int{
|
||||
robotCoord[0] + diff[0],
|
||||
robotCoord[1] + diff[1],
|
||||
}
|
||||
robotCoord = nextRobotCoord
|
||||
houseCount[robotCoord]++
|
||||
}
|
||||
}
|
||||
return len(houseCount)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func Test_part1(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want int
|
||||
}{
|
||||
{"actual", util.ReadFile("input.txt"), 2565},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := part1(tt.input); got != tt.want {
|
||||
t.Errorf("part1() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_part2(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want int
|
||||
}{
|
||||
{"actual", util.ReadFile("input.txt"), 2639},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := part2(tt.input); got != tt.want {
|
||||
t.Errorf("part2() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user