Skip to content

Commit

Permalink
solve p1
Browse files Browse the repository at this point in the history
  • Loading branch information
maxi0604 committed Dec 21, 2023
1 parent 52b6e9c commit 549bff5
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions d21/d21.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bufio"
"fmt"
"os"
"slices"
"strconv"
)

func main() {
reader := bufio.NewReader(os.Stdin)
var lines [][]byte
var extra [][]int
x, y, i := 0, 0, 0

if len(os.Args) != 2 {
Expand All @@ -35,13 +35,14 @@ func main() {
}
}
lines = append(lines, line)
extra = append(extra, make([]int, len(line)))
} else {
break
}
i++
}

recurse(lines, x, y, n, make([]int, 0))
recurse(lines, x, y, n)
sum := 0
for _, line := range lines {
fmt.Print(string(line))
Expand All @@ -54,34 +55,39 @@ func main() {
fmt.Printf("x %d y %d n %d result %d\n", x, y, n, sum)
}

func recurse(grid [][]byte, x int, y int, d int, stack []int) {
if x < 0 || y < 0 || y >= len(grid) || x >= len(grid[0]) {
return
}
func recurse(grid [][]byte, x int, y int, d int) {

if grid[y][x] == '#' || grid[y][x] == '\n' {
return
}

// We have int tuples at home. Int tuples at home:
if slices.Contains(stack, x << 16 | y) {
return
}
if d != 0 {
tmp := make([]int, len(stack) + 1)
copy(tmp, stack)
tmp = append(tmp, x << 16 | y)
recurse(grid, x + 1, y, d - 1, tmp)
recurse(grid, x - 1, y, d - 1, tmp)
recurse(grid, x, y + 1, d - 1, tmp)
recurse(grid, x, y - 1, d - 1, tmp)
if d % 2 == 0 {
grid[y][x] = 'O'
} else {
grid[y][x] = 'P'
q := []int{x, y, d}

for len(q) > 0 {
cx := q[0] // we have queue of int triples at home.
cy := q[1] // queue of int triples at home.
cd := q[2]
q = q[3:]
if cx < 0 || cy < 0 || cy >= len(grid) || cx >= len(grid[0]) {
continue
}
return
}

grid[y][x] = 'o'
if grid[cy][cx] == '#' || grid[cy][cx] == '\n' {
continue
}

if grid[cy][cx] == 'O' || grid[cy][cx] == 'P' {
continue
}

if cd == -1 {
continue
}
grid[cy][cx] = min(byte('O' + cd % 2), max(grid[cy][cx], 'P')) // cursed hack. 'O' + 1 = 'P'. no ternaries.
q = append(q, cx + 1, cy, cd - 1)
q = append(q, cx - 1, cy, cd - 1)
q = append(q, cx, cy + 1, cd - 1)
q = append(q, cx, cy - 1, cd - 1)

}
// if extra[y][x] >= d && (extra[y][x] % 2 != d % 2) {
// return
// }
}

0 comments on commit 549bff5

Please sign in to comment.