Skip to content

Commit e843cb3

Browse files
committed
2024.06 Part 1
1 parent cceb546 commit e843cb3

File tree

3 files changed

+231
-0
lines changed

3 files changed

+231
-0
lines changed

2024/6-guard/example.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
....#.....
2+
.........#
3+
..........
4+
..#.......
5+
.......#..
6+
..........
7+
.#..^.....
8+
........#.
9+
#.........
10+
......#...

2024/6-guard/input.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
..#.....................#............#....#...........#...............................#.....#.....................................
2+
.................#............................#.............##..#...............................#....#..#.........#...............
3+
.............................................##.........................................#....................#...............#...#
4+
...##....................#...............#.....#....................#......##..............#............................#....#....
5+
.................................#..#..............#...........#.............#..#........................#............#...........
6+
.................#.........................#...........................#........................#........................#........
7+
...#....................................#....#.........#................#...............#.....#................#.....#............
8+
.............#.#..........................................................#...#..................#....#.......#.....#...#.........
9+
.....#........................#....##...#............................................................#............................
10+
..................#...#.........................................#..................#.............................#.#........#.....
11+
......#...............................#.#..........##..#...........#.........................#.......#....................#.......
12+
.................................#..............#...............#........#..........#.........#............#.........#.......#...#
13+
..#..#............#...#.....................#...............#............#.#........................#........#...............#....
14+
..............#..#.#........................#.........#...........##.......#..........#.....................#..............#...#..
15+
..........................#.................##...................................#....#.........#.................................
16+
..................#..#........#....#...............#..............#.......#.......#......................................#..#...#.
17+
..........#.#.................#.........#...................#.......................##......#....................#.......#........
18+
..........#.........#...#......................................................................#.....#............#........#......
19+
...##.............#...........................................................#..#....................##.#...#.#..#.........#..#..
20+
.....#......#.............................#.......#...#...#...............#.....#............#........#..#.#......................
21+
........#................#.........#.......................................................................#..........#...........
22+
.......#........................................#..........#...............................#..#...................................
23+
..................##.......#.......#.....#............#.......#...#.........#....##...............#..#.............#..........#...
24+
.......#...........#...........#...........................#.............#.........#..#........................#.##...............
25+
.....................................................................#........#....#...........................................#..
26+
....#...........#......#.........#....................................#.................................#......#..................
27+
...#........................................#..#...............................#.....................#.#......#...................
28+
..#.....#.................................#.................#....#............................................#......#............
29+
...................................................##...........#.......#.............#.................#.......#.................
30+
..........................................................##..............#....#....#.............#...............................
31+
........................................................#.....................................#................................#..
32+
..............##....#..#..........#...........#......#..#....#.......#................#............................#..#...#.......
33+
..........................#....................#.................................................#................................
34+
#................................................#.....................#..#...............##........#...#...#.........#...........
35+
...........#.................................#..#....................#.#.......#..........................#.......#.......#.......
36+
..............................#......#......#.................#.................#.................#.#.............#..#.#..........
37+
.......................#....................#....................................#.....#.#.......#............................#...
38+
...............................................................................................#.#................................
39+
#..................#.....#...............................#..............#...#.....................................................
40+
..........................................................#.................................................#.....................
41+
..........#.........................#.....................................................#.........#..........#................#.
42+
........#..............#..#......................................#.........#............#.................................#.......
43+
.......#.........................................................#.........#................#...............................#.....
44+
...........#............................................#...#.............................#.....^................................#
45+
................#.............#...#..............................#..................................#.............................
46+
.........................................................................................................#........................
47+
.................................................#........#.......................#.#.....#......................#................
48+
.........#.........#...#....................................................#.....................................................
49+
##.##..#...................................#......................................................................................
50+
.................#...........................................................................................#........#...........
51+
.........................................................................#........................#....#......................#...
52+
..............#......................#........#..........................#.........#..............................................
53+
.......................#............................................................................##............................
54+
.....#.................#........#...#...............#.............................#......................#........................
55+
....#...................................................................................#............##...........................
56+
................#............................#............................................#.......................................
57+
.......................................................................................................#......#...................
58+
......................................................................................#...#.......................................
59+
...............................................#........#..........#............................#..............#.......#..........
60+
..............#.....................#........#..#............................#...#..#.#...................................#.......
61+
..........#...............................#............................#...........................................#..............
62+
....#............#.#....#......................................................................#..................................
63+
.......................#.......................#................................#..............#.....................#.........#..
64+
...........#.........................#.............#.#........#........#..........................................#...............
65+
.......#......................#....................#...............................#......................#...........#...........
66+
............#.....................................................................................................................
67+
....#.......#............................................................#.........#.............#..........#....#..........#.....
68+
.......#.........................................................................#..................#..#....#.......#.............
69+
............................#....#.......#......#.......#.....................#.................#.................................
70+
.......................................................#.............#..........................#.................................
71+
....................................#.....................................##......................#............................#..
72+
..............#...............#....................#.......................#................#......................#..............
73+
....................#...........................#...................................................................#.............
74+
...#..........................#...........................#............................................................#..........
75+
..............#.................#.............................#..#.......#.......................#.##.............................
76+
....................#.....#...#..............................................#......#...........#.................#..#....#.......
77+
#......................................#.....................#..#..#.#...........#.............#.................................#
78+
.....................#...........................................................#..........#............##..........#............
79+
.......#..................................#...............#..........#....#...................................................#...
80+
.............#.................#...........................#..............#.................................................#.....
81+
...............................#......................................##...................................#.#......#..........#..
82+
.................##....................#.................................................#......................#................#
83+
........#....................................#..........#.........................#..................#......................#.....
84+
...#....#...........................#..............................#.#..........................#.................................
85+
.#...................................#..#......#.................................................................#................
86+
.................................#...........#................#.#........................#....................................##..
87+
#..#.....##...............#................................................................#....#......#.........................#
88+
.#....#......................................................#.........................#.......................................#..
89+
.#..............................................#.................................................................#...............
90+
...............#..........................................#...#...............................#.#..#.#........#...................
91+
..............#................................#.......#..................................#.........................#........#....
92+
.......................##..................................................#......................................................
93+
..............................#...............................##..................................................................
94+
#....................#...........................................................#.............................#..................
95+
#..................................................................#...#..........................................................
96+
#..#........#...................................#..#..........##..............#...................................................
97+
.......................#.....................#....................................................................................
98+
.....................#...........#.............................................................#........................#....#....
99+
...............#............................................................................##...#..........................#.....
100+
...#......................................#.......................................................................................
101+
....#...........................#.........#.................................................#..........#...#........#....#........
102+
......#....#.#............................#..........................................#........................................#...
103+
...........#...#.............................................#......................#..................#.....................#....
104+
...#..........#........#..........#.......#..............#.............................#.......................#................#.
105+
...........#...............#...........................................................#..........................................
106+
..#..........##.................................................#...#...............#.......................#...............##....
107+
.......................#..............#.............#......................................................#..................#...
108+
..............#..#........##..........#...................................................#.............##....................#...
109+
...................#..................................................#................#......#.....#.........#.#...#.............
110+
..................#.................................................#.................................................#.........#.
111+
......................................#.#........#.....#...#..........#..............#.........................#...#..............
112+
.................................#..............#..#.....................#................#.............#..#..#...................
113+
.........#.......#.#..............#..#...............#.................#..................................#..#....................
114+
.............................#....#.......##..................................................................................##..
115+
.....................................................................................#........#.......#......#....................
116+
........#...........#..........#..........#............................#...............#........#.#...........................#...
117+
.#.................#...#..............#...#...................#........#...............#..........................................
118+
.........#............................................##........#......#.....................#.......................#............
119+
.......................................#..........#..#......#........#..........#..........................#....................#.
120+
....#......#..........................#....................#.......................................#.....###.......#..............
121+
.......#..................#....#.......................................................................#..........................
122+
....#........................#.............................................................#.......#..........................#...
123+
......................#.................................#..........#................#..#.........................#................
124+
...........#....................................................#..............#...........................#......................
125+
.....#................#........#...#....#........#............#...............#.......................#..#.........#..............
126+
#..................................................#...........................#....#...........#.................................
127+
....#.#.....................................#.#...............#................#....................................#.............
128+
............#....................#.#.#...............................................#...................##.......................
129+
...##..............#........................#.............#.......#...........#.#.##..............................#....##.........
130+
......#...........#....................................................#.........................##...............................

2024/6-guard/main.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main
2+
3+
import (
4+
"advent-of-code/util"
5+
"bufio"
6+
"fmt"
7+
"strings"
8+
)
9+
10+
const (
11+
freeBlock = '.'
12+
obstacleBlock = '#'
13+
visiteBlock = 'X'
14+
)
15+
16+
var direction = []int{-1, 0, 1, 0}
17+
var grid [][]byte
18+
var guardStart position
19+
20+
func main() {
21+
file := util.FileFromArgs()
22+
scan := bufio.NewScanner(file)
23+
24+
// Map the grid
25+
var row string
26+
var guardIndex int
27+
for scan.Scan() {
28+
row = scan.Text()
29+
30+
guardIndex = strings.Index(row, "^")
31+
if guardIndex > -1 {
32+
guardStart = position{guardIndex, len(grid)}
33+
row = strings.Replace(row, "^", "X", 1)
34+
}
35+
36+
grid = append(grid, []byte(row))
37+
}
38+
39+
// Walk the grid
40+
fmt.Println(walkGrid(copyGrid(grid)))
41+
}
42+
43+
type position struct {
44+
X, Y int
45+
}
46+
47+
func walkGrid(grid [][]byte) int {
48+
guard := position{guardStart.X, guardStart.Y}
49+
50+
stepCount := 1
51+
var nx, ny int
52+
dx, dy := 1, 0
53+
for {
54+
nx, ny = guard.X+direction[dx], guard.Y+direction[dy]
55+
56+
// Did we leave the grid?
57+
if nx < 0 || ny < 0 || nx >= len(grid[0]) || ny >= len(grid) {
58+
return stepCount
59+
}
60+
61+
if grid[ny][nx] == obstacleBlock {
62+
dx, dy = (dx+1)%4, (dy+1)%4
63+
continue
64+
} else if grid[ny][nx] == 0 {
65+
stepCount++
66+
} else if grid[ny][nx] > 4 {
67+
return -1 // Infinite loop detected!
68+
}
69+
70+
grid[ny][nx]++
71+
guard.X, guard.Y = nx, ny
72+
}
73+
}
74+
75+
func copyGrid(grid [][]byte) [][]byte {
76+
gridCopy := make([][]byte, len(grid))
77+
78+
for i := range grid {
79+
barry := make([]byte, len(grid[i]))
80+
for j := range grid[i] {
81+
if grid[i][j] == freeBlock {
82+
barry[j] = 0
83+
} else {
84+
barry[j] = grid[i][j]
85+
}
86+
}
87+
gridCopy[i] = barry
88+
}
89+
90+
return gridCopy
91+
}

0 commit comments

Comments
 (0)