Skip to content

Commit

Permalink
solve 6 pt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle.brown committed Dec 6, 2024
1 parent 2876803 commit af440d6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 16 deletions.
65 changes: 51 additions & 14 deletions 6/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,52 @@ func main() {
_ = f.Close()

start := time.Now()
res := countGuardSpaces(guardMap, guard)
res, _ := countGuardSpaces(guardMap, guard)
elapsed := time.Since(start)
fmt.Println(res)
fmt.Println(elapsed)

//start2 := time.Now()
//res2 := countMiddleOfCorrectedOrders(rules, orders)
//elapsed2 := time.Since(start2)
//fmt.Println(res2)
//fmt.Println(elapsed2)
start2 := time.Now()
res2 := countPossibleLoops(guardMap, guard)
elapsed2 := time.Since(start2)
fmt.Println(res2)
fmt.Println(elapsed2)
}

func countGuardSpaces(guardMap [][]bool, guard guardPosition) int {
// this is brute force nonsense. I have might be able to do better by restricting to locations where we have visited
// at least once. That would cut down the search space significantly. I think there might even be a far more
// effecient solution out there that would require using a graph of paths instead and finding a loop? unsure.
// a 2s run time is pathetic though.
func countPossibleLoops(guardMap [][]bool, guard guardPosition) int {
var acc int
for j := 0; j < len(guardMap); j++ {
for i := 0; i < len(guardMap[j]); i++ {
if !guardMap[j][i] && !(j == guard.y && i == guard.x) {
guardMap[j][i] = true
_, loop := countGuardSpaces(guardMap, guard)
if loop {
acc++
}
guardMap[j][i] = false
}
}
}
return acc
}

func countGuardSpaces(guardMap [][]bool, guard guardPosition) (int, bool) {
// assume the guard is on the map someplace
acc := 1
visited := make([][]bool, 0, len(guardMap))
visited := make([][][]bool, 0, len(guardMap))
for _, _ = range guardMap {
visited = append(visited, make([]bool, len(guardMap[0])))
visitLine := make([][]bool, len(guardMap[0]))
for i, _ := range visitLine {
// 4 directions plus 1 ignored
visitLine[i] = make([]bool, 5)
}
visited = append(visited, visitLine)
}
visited[guard.y][guard.x] = true
visited[guard.y][guard.x][guard.d] = true

for guard.x >= 0 && guard.x < len(guardMap[0]) && guard.y >= 0 && guard.y < len(guardMap) {
var nextX int
Expand Down Expand Up @@ -70,16 +96,27 @@ func countGuardSpaces(guardMap [][]bool, guard guardPosition) int {
guard.d = Dir_DOWN
}
} else {
if ok && !visited[nextY][nextX] {
visited[nextY][nextX] = true
acc++
if ok {
if visited[nextY][nextX][guard.d] { // loop detected, we have already been to the next space in the same direction
return acc, true
}

// no loop but have we ever been here?
var seen bool
for i := 0; !seen && i < len(visited[nextY][nextX]); i++ {
seen = visited[nextY][nextX][i]
}
if !seen {
visited[nextY][nextX][guard.d] = true
acc++
}
}
guard.x = nextX
guard.y = nextY
}
}

return acc
return acc, false
}

type direction int
Expand Down
43 changes: 41 additions & 2 deletions 6/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,47 @@ func Test_countGuardSpaces(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := countGuardSpaces(tt.args.guardMap, tt.args.guard); got != tt.want {
t.Errorf("countGuardSpaces() = %v, want %v", got, tt.want)
if got, loop := countGuardSpaces(tt.args.guardMap, tt.args.guard); got != tt.want || loop {
t.Errorf("countGuardSpaces() = %v, %v, want %v, false", got, loop, tt.want)
}
})
}
}

func Test_countPossibleLoops(t *testing.T) {
type args struct {
guardMap [][]bool
guard guardPosition
}
tests := []struct {
name string
args args
want int
}{
{
name: "example",
args: args{
guard: guardPosition{x: 4, y: 6, d: Dir_UP},
guardMap: [][]bool{
{false, false, false, false, true, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, true},
{false, false, false, false, false, false, false, false, false, false},
{false, false, true, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, true, false, false},
{false, false, false, false, false, false, false, false, false, false},
{false, true, false, false, false /* guard pos */, false, false, false, false, false},
{false, false, false, false, false, false, false, false, true, false},
{true, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, true, false, false, false},
},
},
want: 6,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := countPossibleLoops(tt.args.guardMap, tt.args.guard); got != tt.want {
t.Errorf("countPossibleLoops() = %v, want %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit af440d6

Please sign in to comment.