performance improvement on part 2 of day 6, but still not a correct result

This commit is contained in:
2024-12-07 00:47:35 +01:00
parent 17395adcc0
commit 2d9dc1b236
2 changed files with 17 additions and 34 deletions

View File

@@ -128,4 +128,3 @@
...........................#..............#.....#............#.....................#........#....#.......................#........ ...........................#..............#.....#............#.....................#........#....#.......................#........
........#..##................#.....#.#..............#..............................#........................#.........#........... ........#..##................#.....#.#..............#..............................#........................#.........#...........
.........................................................#...........................##..........#........#.##.................... .........................................................#...........................##..........#........#.##....................

View File

@@ -5,7 +5,10 @@ import (
"fmt" "fmt"
"strings" "strings"
) )
// type definitions // type definitions
type Direction int type Direction int
const ( const (
Up Direction = iota + 1 Up Direction = iota + 1
@@ -26,8 +29,8 @@ func new_state(text string) *state {
field := make([][]rune, len(lines)) field := make([][]rune, len(lines))
guard := [2]int{-1,-1} guard := [2]int{-1,-1}
for y := range lines { for y, line := range lines {
field[y] = []rune(lines[y]) field[y] = []rune(line)
for x, r := range field[y] { for x, r := range field[y] {
if r == '^' { if r == '^' {
@@ -41,24 +44,16 @@ func new_state(text string) *state {
return &s return &s
} }
func (s *state) equals(other *state) bool {
return s.guard_x == other.guard_x && s.guard_y == other.guard_y && s.direction == other.direction
}
// general util // general util
func check(e error) { func check(e error) {
if e != nil { panic(e) } if e != nil { panic(e) }
} }
func add_non_duplicate(slice []*state, elem *state) []*state {
for _, e := range slice {
if e.equals(elem) { return slice }
}
return append(slice, elem)
}
// simulation functions // simulation functions
func iter(s *state) *state { func iter(s *state) *state {
new_guard_x := s.guard_x new_guard_x := s.guard_x
new_guard_y := s.guard_y new_guard_y := s.guard_y
@@ -109,35 +104,24 @@ func iter(s *state) *state {
} }
func will_loop(s *state) bool { func will_loop(s *state) bool {
distinct_states := make([]*state, 0, 130 * 130 * 4) counter := 0
// while guard is within the field // while guard is within the field
for s.guard_x >= 0 { for s.guard_x >= 0 && counter < 10000 {
// store copy of the current state
old_len := len(distinct_states)
cpy := state{
field: s.field,
guard_x: s.guard_x,
guard_y: s.guard_y,
direction: s.direction,
}
distinct_states = add_non_duplicate(distinct_states, &cpy)
// if duplicate state found, loop found
if len(distinct_states) == old_len {
return true
}
// iterate // iterate
s = iter(s) s = iter(s)
counter++
}
if s.guard_x < 0 {
// the guard is no longer in the field, not a loop
return false
} else {
return true
} }
// the guard is no longer in the field, not a loop
return false
} }
func print(s *state) { func print(s *state) {
out := make([]string, 0, len(*s.field)) out := make([]string, 0, len(*s.field))
for _, line := range *s.field { for line := range *s.field {
out = append(out, string(line)) out = append(out, string(line))
} }
fmt.Println(strings.Join(out, "\n")) fmt.Println(strings.Join(out, "\n"))