add day 6 with part 2 being incomplete

This commit is contained in:
2024-12-06 23:08:31 +01:00
parent 366d38c5a2
commit 17395adcc0
3 changed files with 434 additions and 0 deletions

178
day6/loop_guard.go Normal file
View File

@@ -0,0 +1,178 @@
package main
import (
"os"
"fmt"
"strings"
)
// type definitions
type Direction int
const (
Up Direction = iota + 1
Down
Left
Right
)
type state struct {
field *[][]rune
guard_x int
guard_y int
direction Direction
}
func new_state(text string) *state {
lines := strings.Split(text, "\n")
field := make([][]rune, len(lines))
guard := [2]int{-1,-1}
for y := range lines {
field[y] = []rune(lines[y])
for x, r := range field[y] {
if r == '^' {
guard = [2]int{x, y}
field[y][x] = '.'
}
}
}
s := state{field: &field, guard_x: guard[0], guard_y: guard[1], direction: Up}
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
func check(e error) {
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
func iter(s *state) *state {
new_guard_x := s.guard_x
new_guard_y := s.guard_y
// try to move
switch {
case s.direction == Up:
new_guard_y--
case s.direction == Down:
new_guard_y++
case s.direction == Left:
new_guard_x--
case s.direction == Right:
new_guard_x++
}
// if guard is outside of the field
if new_guard_x < 0 || new_guard_y < 0 || new_guard_x >= len((*s.field)[0]) || new_guard_y >= len(*s.field) {
new_guard_x = -1
new_guard_y = -1
} else if (*s.field)[new_guard_y][new_guard_x] == '#' {
// correct if path is blocked
switch {
case s.direction == Up:
new_guard_y++
new_guard_x++
s.direction = Right
case s.direction == Down:
new_guard_y--
new_guard_x--
s.direction = Left
case s.direction == Left:
new_guard_x++
new_guard_y--
s.direction = Up
case s.direction == Right:
new_guard_x--
new_guard_y++
s.direction = Down
}
}
// set new guard coordinates
s.guard_x = new_guard_x
s.guard_y = new_guard_y
return s
}
func will_loop(s *state) bool {
distinct_states := make([]*state, 0, 130 * 130 * 4)
// while guard is within the field
for s.guard_x >= 0 {
// 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
s = iter(s)
}
// the guard is no longer in the field, not a loop
return false
}
func print(s *state) {
out := make([]string, 0, len(*s.field))
for _, line := range *s.field {
out = append(out, string(line))
}
fmt.Println(strings.Join(out, "\n"))
fmt.Println("guard", s.guard_x, s.guard_y)
}
// main loop
func main() {
// read in file
dat, err := os.ReadFile("data.txt")
check(err)
dat_str := strings.TrimSpace(string(dat))
stateobj := new_state(dat_str)
start_of_guard := [2]int{stateobj.guard_x, stateobj.guard_y}
// check loops
counter := 0
for y := range *stateobj.field {
for x, elem := range (*stateobj.field)[y] {
// skip already existing borders
if elem == '#' { continue }
// because the placement of the start position of the guard is not allowed
if x == start_of_guard[0] && y == start_of_guard[1] { continue }
// set border
(*stateobj.field)[y][x] = '#'
// check for loop
if will_loop(stateobj) { counter++ }
// restore state
stateobj = new_state(dat_str)
}
}
fmt.Println(counter)
}