Browse Source

Solved part1

Piotr Czajkowski 2 weeks ago
parent
commit
936b60b80f
2 changed files with 160 additions and 3 deletions
  1. 30 3
      06/code.go
  2. 130 0
      06/input

+ 30 - 3
06/code.go

@@ -8,7 +8,7 @@ import (
 )
 )
 
 
 var directions [][]int = [][]int{
 var directions [][]int = [][]int{
-	{0, 1}, {1, 0}, {0, -1}, {-1, 0},
+	{0, -1}, {1, 0}, {0, 1}, {-1, 0},
 }
 }
 
 
 type Point struct {
 type Point struct {
@@ -17,7 +17,7 @@ type Point struct {
 }
 }
 
 
 func (p *Point) key() string {
 func (p *Point) key() string {
-	return fmt.Sprintf("%d_%s", p.x, p.y)
+	return fmt.Sprintf("%d_%d", p.x, p.y)
 }
 }
 
 
 func findGuard(line string) *Point {
 func findGuard(line string) *Point {
@@ -56,6 +56,33 @@ func readInput(file *os.File) (*Point, [][]byte) {
 	return guard, matrix
 	return guard, matrix
 }
 }
 
 
+func walk(guard *Point, matrix [][]byte, xMax, yMax int, visited map[string]bool) {
+	if guard.x < 0 || guard.x > xMax || guard.y < 0 || guard.y > yMax {
+		return
+	}
+
+	if matrix[guard.y][guard.x] == '#' {
+		guard.x -= directions[guard.direction][0]
+		guard.y -= directions[guard.direction][1]
+		guard.direction = (guard.direction + 1) % 4
+	} else {
+		visited[guard.key()] = true
+		guard.x += directions[guard.direction][0]
+		guard.y += directions[guard.direction][1]
+	}
+
+	walk(guard, matrix, xMax, yMax, visited)
+}
+
+func part1(guard *Point, matrix [][]byte) int {
+	xMax := len(matrix[0]) - 1
+	yMax := len(matrix) - 1
+	visited := make(map[string]bool)
+
+	walk(guard, matrix, xMax, yMax, visited)
+	return len(visited)
+}
+
 func main() {
 func main() {
 	if len(os.Args) < 2 {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
 		log.Fatal("You need to specify a file!")
@@ -68,5 +95,5 @@ func main() {
 	}
 	}
 
 
 	guard, matrix := readInput(file)
 	guard, matrix := readInput(file)
-	fmt.Println(guard, matrix)
+	fmt.Println("Part1:", part1(guard, matrix))
 }
 }

+ 130 - 0
06/input

@@ -0,0 +1,130 @@
+.......................#....#....................#....#..##.##.........................................#...........#...........#..
+.............#........................................#....................#............#.......................#............#....
+.........#.............#..#....#..........................#.......#.#.......#...............................................##....
+..........#....#....................................................#....................#...........#....#.......................
+.............#...#....................#...........#.........#...........................#............#............................
+..........................................................................................#.....#.......#...................##....
+...............................................#.#......#..............#...#...................##...........#....#..#...#.........
+..#.................#......#........#......................#.......#...............................#..............................
+.........................#...............#.....................................#....#..#..#............................#..........
+......................#.................................................................#..........#..............#...#......#....
+............................#.....##..........#.....................#..........#..#.......#.......................................
+......#...###...........#.................................................................#.......................................
+......................#.#..#................#..........#................#...........#............#..............#...#.........#...
+....#.........................#..#...............................................#....#.............#.............................
+..#............#...#............#..........#...............#.....#....##.........................#........#.......................
+..................#............#..............#..........................................................#..#..#........#.........
+.......#...#............#..........................................................#..............................#.............#.
+..#................#....................#.#...#.................................#.........#....................#.........#........
+...................................#..#..........................#........#.....#.........................................#.......
+...........##.................##............#................#...#..................................##...#........................
+..............#..................#....................................................................#...........................
+.#...#....#..........##..................................#.............................................................#......#...
+......#.....#................................#...............................................................#......#.............
+..........#.............................................#............................##....#.........................#............
+..........#......#.................#...#........#...........#.#..........#........#....................................#..........
+.#..................#.#............................#.......................#.........#.......#....................................
+.....................#....#..................#..................................................##..#................#............
+......#......................#..#..............##.......#..................................................#..............#.......
+..........................................................................#........#...#..............#.................#.........
+....................##....#.......................#........#...................#.............##.......#...........................
+.........#............#..................................................................................#........................
+........................................................................#................#........................#.............#.
+............#.......#.................................#............#..............................................................
+.#.............#...............................................#....#.........#....#..................................#.#.........
+.......................................#.....................................................................................#....
+........#........................................#...............#........................................................#.......
+...............................#.........#........#...........#.......................................................#.........#.
+.....................##...#....................................................#............#..................#..#..........##...
+................#..........#...#............................................................#...##................................
+...................#........#...............................#.................#.........#.........................................
+.................................................................................................................#................
+............................#......................................................#.....................................#......#.
+......#................#............................#.............................................................................
+..#...........#.....#.........#....#................#............................................#................................
+..................................#.......#.......#...........#.........#................#.....................................#.#
+...............................................#............................................#.#.....................#.......##....
+..............................#...............#..............................#...#...#.............................#...........#..
+.......#...........#..........................................................#........#..#.........#.....#.....#...#.............
+......................................................#.#............#.........#.#...........#........##.....#....................
+...........................................................................#..........#.........................#.........#.......
+....#.........................#.....................................................................................#...#......#..
+...#.......#............................................................#...............................#....................##.#.
+.................................#.....#................................................................#.........................
+.....................................#........................#....................................#..............................
+..............#.......................................................................#.......#....................#..........#...
+................................#..........................................#.................................#.....#..............
+...........#.......................................................#...............#.....................#..............#......#..
+..................................#..........................#.................................................................#..
+.............##......#....................................................#......#...............................................#
+#.......#...................#..........#...................#.................#..................................................#.
+.......#..........#.....##...........#.......................#............#..............................#.............#..........
+.............#...................##......................................................................................#........
+#.............................#...................................................................#...............................
+.#...................................#....##..................................................................................#...
+.................................#.#..................#..#..#..........................#.......#..................................
+..................#....#....#....................................................#....................................#........#..
+..........................................#...#.....#...........#.........#............#............#..........................#..
+...............................................................................#..............................................#...
+.............................#......#....................#....#....................................................#.#.#..........
+................##......#........#........#...........#..........................#......................#..............#..........
+.....................#.....#..#.............................................................................................#.....
+.......#..#...........#................................................................#..........................................
+#.......................................................................................................#...#.....................
+.#..........................................................#.....................................................#...............
+#.....#...#...............#..#......#........#..#...................#...................#.................#.......................
+..#.................................................................................#.#......................#....................
+.......................#.............#..........#.......................#...................................#.....................
+.................#......#......#...#.#.#.............................#.......................................................#....
+..#...........................................................................................................#...................
+.................................#..................#.....#........#.........................................................#....
+.............#......................#...............................................#...#.............................#...#....#..
+.#............................#..........................#.#...........................#..........................#.#.............
+.............................#.........#.................##............................#...................#......................
+.........#......#....#.........#..............................................................................................#..#
+.....................#......#.................#..#..........................#..................#..............................#.#.
+........................................................#......................#............#.......#......#......................
+........................................#....##.#........#......#..............................#..........#..........#.##.........
+.............#.......#.......#......................#..........................................#.........#............#...#...#.#.
+..................#........................................................#...............................#.....#.....#..........
+.........#.................#........#..............^..............#.................##......#...................................#.
+....#...................#.#.................................#...#.................................................#...............
+.................#.............................................#................#.....#......#.......#...................#........
+...............#...............................................................................#..#...............#...............
+........................................##..........................................................................##............
+............................................#...................#........#...#...#....#.........#...................#.............
+.....#...#...................#....................................#......#...........#...............#............................
+........................#..................#.....................#.................................................#......##......
+...#.................#....................#..................................#.#.........................#.......#................
+....................#............#..................................#...#......#....................#...............#.............
+.....#...............#.......###........#............#......................#.......#....#........................................
+....................#........#...........#..................................#..#................#.............#...................
+.....#..................#...................#.#..................................#.......#..#.....................................
+..#..#.......#........................................#.................................................................#.........
+.......#............#...#....#.......................#.#...................................#.........................#..#.#.......
+..............#......................................................................................................#............
+............#....................#........................#.........#...#.........................................................
+...................................#..............................#.........................................................#.....
+......##...........#.#........................#.........................................................................#.........
+..........................#.....................................................#..............................#..............#...
+.........................#...#.....................#..#................#................................................#....#....
+...#.................................#...#.......#...............................................................#................
+..................................#....................................#...........................##..#.#.....................#..
+.#.............#.....#...............................................................#...............#...............#..#.........
+.........#.............................#........................#.............#......................#........#............#......
+...................................#..#...#.....................................#.................................................
+........................................##..........#..............................................#.....................#........
+......#...........................................#....#.....................#...................#..#.........#...................
+.#....#.....#........##..................................#......#................................................#....#..##.......
+..........#....................................................................##.........................#...........#...........
+...........#.#......#.......#..................#............#...............................................#...#..............#..
+.................................................................#.......#.........#.....#........#....##....#..........#.........
+.............................#......................................................#.....#...#...#....#...........#..........#.#.
+.....................#........#.........................#......................................#................#...............#.
+.........................................................................#...................................................#....
+..............#...#..#................#....................................#.......#....#.#...............#.........#.............
+.......#......#.#............................................#.......#............................................................
+##......................................................#..#.................................##...................#..#...........#
+...........#.#...#........#............#..............................#........................#.........................#..#.....
+................................................##.........................................#.................#....................
+............................#....................................................#...#........................#.#......#........#.