From 7272fee707e7f9231889374b5052ca96aef67f1e Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Thu, 19 Dec 2024 10:01:57 +0100 Subject: [PATCH] add day18 --- day18/blocked.go | 204 +++ day18/data.txt | 3450 ++++++++++++++++++++++++++++++++++++++++++++++ day18/maze.go | 191 +++ 3 files changed, 3845 insertions(+) create mode 100644 day18/blocked.go create mode 100644 day18/data.txt create mode 100644 day18/maze.go diff --git a/day18/blocked.go b/day18/blocked.go new file mode 100644 index 0000000..fcbbf61 --- /dev/null +++ b/day18/blocked.go @@ -0,0 +1,204 @@ +package main + +import ( + "os" + "fmt" + "strings" + "strconv" +) + + +// ---------------------------------------- +type Direction uint8 + +const ( + Up Direction = iota + Down Direction = iota + Left Direction = iota + Right Direction = iota +) + + +// ---------------------------------------- +type Position struct { + coord [2]uint + direc Direction +} + +func (p Position) StepUnbound(d Direction) Position { + switch { + case d == Down: p.coord[1]++ + case d == Up: p.coord[1]-- + case d == Left: p.coord[0]-- + case d == Right: p.coord[0]++ + } + p.direc = d + + return p +} + + +// ---------------------------------------- +type Maze struct { + start [2]uint + end [2]uint + field [][]byte + blocks_added [][2]uint +} + +func NewMaze(byte_positions string, bytes_to_load uint, size [2]uint) *Maze { + /* size = [2]unit{x,y} */ + + var maze Maze + + // create board + maze.field = make([][]byte, 0, size[1] + 2) + for i := range size[1] + 2 { + line := make([]byte, size[0] + 2) + for j := range line { + if i == 0 || uint(i) == size[1] + 1 || j == 0 || uint(j) == size[0] + 1 { + line[j] = '#' + } else { + line[j] = '.' + } + } + maze.field = append(maze.field, line) + } + + // add byte corruptions + maze.blocks_added = make([][2]uint, 0) + lines := strings.Split(byte_positions, "\n") + for i, line := range lines { + if uint(i) >= bytes_to_load { break } + x, err := strconv.Atoi(strings.Split(line, ",")[0]) + check(err) + y, err := strconv.Atoi(strings.Split(line, ",")[1]) + check(err) + + maze.field[y + 1][x + 1] = '#' + maze.blocks_added = append(maze.blocks_added, [2]uint{uint(x + 1), uint(y + 1)}) + } + + // set start/ end + maze.start = [2]uint{1,1} + maze.end = size + + return &maze +} + +func (m *Maze) Step(pos Position) []Position { + out := make([]Position, 0, 4) + + if m.field[pos.coord[1] + 1][pos.coord[0]] == '.' && pos.direc != Up { + out = append(out, pos.StepUnbound(Down)) + } + if m.field[pos.coord[1] - 1][pos.coord[0]] == '.' && pos.direc != Down { + out = append(out, pos.StepUnbound(Up)) + } + if m.field[pos.coord[1]][pos.coord[0] + 1] == '.' && pos.direc != Left { + out = append(out, pos.StepUnbound(Right)) + } + if m.field[pos.coord[1]][pos.coord[0] - 1] == '.' && pos.direc != Right { + out = append(out, pos.StepUnbound(Left)) + } + + return out +} + +func (m *Maze) PopCorruptedBlock() [2]uint { + block := m.blocks_added[len(m.blocks_added)-1] + m.blocks_added = m.blocks_added[:len(m.blocks_added)-1] + + m.field[block[1]][block[0]] = '.' + + return block +} + +func (m *Maze) Start() Position { + return Position{m.start, Right} +} + +func (m *Maze) Finished(pos Position) bool { + return m.end == pos.coord +} + +func (m *Maze) Print() { + for _, line := range m.field { + fmt.Println(string(line)) + } +} + + +// ---------------------------------------- +type MazeGraph struct { + cost uint + children []*MazeGraph +} + +var new_maze_graph_visited map[Position]uint + +func NewMazeGraph(m *Maze, start Position, cost uint) *MazeGraph { + children := make([]*MazeGraph, 0, 3) + + // finish reached? + if m.Finished(start) { return &MazeGraph{ cost, children } } + + // check for loop + value, exists := new_maze_graph_visited[start] + if exists { + if value <= cost { return nil } + } + new_maze_graph_visited[start] = cost + + // build subgraphs/ children + for _, position := range m.Step(start) { + child := NewMazeGraph(m, position, cost + 1) + if child != nil { children = append(children, child) } + } + + // found dead end + if len(children) == 0 { return nil } + + // collapse if there is only one possible way + if len(children) == 0 { return children[0] } + + return &MazeGraph{ cost: cost, children: children } +} + +func (graph *MazeGraph) Leaves() []*MazeGraph { + out := make([]*MazeGraph, 0) + + if len(graph.children) == 0 { + out = append(out, graph) + return out + } + + for _, g := range graph.children { + out = append(out, g.Leaves()...) + } + return out +} + +// ---------------------------------------- +func check(err error) { if err != nil { panic(err) } } + + +func main() { + dat, err := os.ReadFile("data.txt") + check(err) + + input := strings.TrimSpace(string(dat)) + maze := NewMaze(input, 10e10, [2]uint{71,71}) + + // check if paths exits + var pos [2]uint + new_maze_graph_visited = make(map[Position]uint) + + for NewMazeGraph(maze, maze.Start(), 0) == nil { + pos = maze.PopCorruptedBlock() + new_maze_graph_visited = make(map[Position]uint) + } + + fmt.Println(pos[0] - 1, ",", pos[1] - 1) +} + diff --git a/day18/data.txt b/day18/data.txt new file mode 100644 index 0000000..0c8c6da --- /dev/null +++ b/day18/data.txt @@ -0,0 +1,3450 @@ +51,48 +33,65 +65,41 +47,58 +59,47 +45,43 +65,45 +64,53 +33,69 +14,9 +51,59 +40,45 +50,45 +23,2 +69,52 +52,61 +62,51 +37,49 +39,53 +1,15 +13,6 +11,9 +29,22 +21,13 +41,65 +24,9 +51,51 +27,26 +21,7 +3,5 +13,2 +66,57 +47,65 +63,55 +68,43 +29,9 +1,25 +38,65 +45,51 +65,37 +19,24 +21,20 +3,14 +35,53 +37,45 +53,39 +49,47 +26,11 +7,7 +45,65 +29,23 +59,58 +22,3 +44,69 +65,40 +57,43 +59,68 +13,4 +61,43 +12,21 +51,33 +31,21 +51,37 +17,7 +1,13 +22,15 +2,7 +51,24 +11,21 +27,15 +45,54 +21,21 +33,59 +17,9 +8,19 +39,46 +13,17 +1,2 +8,23 +48,55 +57,60 +53,61 +59,61 +43,38 +27,69 +33,15 +37,55 +35,25 +8,25 +1,4 +49,68 +59,57 +60,59 +3,10 +21,12 +29,24 +67,68 +34,63 +53,57 +22,25 +53,34 +25,26 +57,37 +4,7 +52,65 +9,6 +3,23 +45,69 +17,15 +43,61 +32,17 +47,41 +4,23 +18,5 +69,66 +50,63 +57,61 +43,42 +4,11 +53,63 +12,29 +39,21 +65,39 +52,33 +28,19 +19,28 +10,7 +47,61 +29,17 +34,65 +39,51 +61,59 +48,59 +59,55 +13,9 +67,63 +15,1 +57,68 +33,63 +29,19 +24,19 +55,44 +63,48 +59,52 +39,50 +39,69 +10,31 +35,17 +46,63 +45,39 +51,53 +10,9 +47,53 +35,68 +25,15 +29,6 +69,37 +23,66 +53,67 +24,11 +55,56 +21,4 +27,60 +9,12 +53,47 +25,62 +17,2 +23,29 +60,69 +24,59 +6,9 +37,65 +34,69 +53,33 +23,27 +41,51 +13,23 +55,53 +65,53 +37,17 +32,63 +44,61 +69,41 +10,13 +68,39 +56,55 +51,41 +27,11 +66,47 +3,17 +9,11 +49,43 +37,53 +13,7 +48,33 +67,55 +33,67 +69,60 +43,36 +51,69 +57,57 +45,49 +17,18 +59,29 +61,49 +16,27 +43,69 +19,8 +55,59 +54,59 +52,57 +14,21 +5,18 +45,67 +10,17 +60,65 +25,23 +19,0 +19,27 +17,19 +61,54 +29,68 +41,47 +18,3 +54,41 +37,61 +10,3 +23,21 +19,26 +25,3 +25,66 +68,69 +29,13 +46,57 +69,51 +2,13 +15,7 +50,53 +56,49 +30,61 +51,55 +64,65 +24,25 +53,58 +55,45 +67,60 +15,18 +8,17 +64,45 +33,61 +6,11 +56,57 +23,17 +25,65 +31,63 +52,43 +62,61 +11,20 +67,59 +22,17 +31,16 +67,39 +20,19 +57,55 +63,50 +53,51 +19,21 +67,47 +54,49 +67,69 +39,52 +60,47 +19,10 +43,58 +33,47 +53,43 +65,67 +47,45 +25,16 +51,35 +7,20 +19,7 +65,63 +45,66 +12,25 +61,53 +43,53 +45,52 +65,57 +3,9 +49,56 +27,68 +49,57 +9,17 +62,53 +67,64 +7,11 +1,3 +68,41 +48,53 +67,51 +49,67 +5,6 +62,67 +46,49 +50,59 +21,31 +21,29 +22,21 +61,41 +19,9 +57,51 +49,65 +41,67 +68,51 +61,61 +35,19 +65,43 +4,21 +2,23 +51,22 +64,57 +19,5 +17,20 +37,46 +38,55 +28,23 +59,42 +22,11 +7,17 +56,69 +29,21 +34,23 +70,43 +61,65 +63,43 +54,51 +40,61 +58,37 +48,49 +45,40 +44,63 +63,47 +46,35 +51,54 +13,31 +19,22 +67,43 +69,39 +4,9 +27,16 +64,69 +63,42 +68,65 +31,68 +25,9 +65,56 +47,57 +39,54 +55,65 +18,33 +69,35 +13,30 +67,67 +63,61 +5,5 +58,53 +65,62 +45,63 +14,27 +39,55 +52,37 +51,63 +48,43 +29,11 +31,19 +27,67 +47,46 +60,55 +17,23 +2,1 +47,59 +11,26 +34,53 +57,42 +9,5 +43,56 +67,41 +57,47 +43,47 +69,65 +43,45 +5,11 +57,45 +35,69 +21,3 +33,21 +53,65 +53,52 +63,63 +11,11 +41,55 +40,55 +59,59 +13,14 +35,51 +13,25 +39,4 +27,17 +51,56 +66,43 +17,5 +44,55 +9,15 +24,27 +12,15 +31,23 +3,7 +3,3 +19,12 +44,51 +33,17 +31,30 +9,9 +65,61 +13,22 +59,44 +31,66 +34,25 +43,65 +37,21 +25,64 +31,59 +13,27 +4,3 +10,27 +32,55 +69,44 +25,24 +35,67 +41,68 +3,4 +69,49 +31,67 +15,20 +45,68 +65,54 +15,4 +57,53 +43,57 +27,59 +68,47 +63,59 +39,70 +69,38 +47,48 +41,50 +35,57 +41,61 +11,29 +14,17 +49,69 +11,31 +54,35 +66,49 +7,4 +25,14 +60,41 +9,7 +49,49 +11,13 +19,25 +43,66 +25,13 +20,27 +42,69 +27,23 +64,61 +27,12 +63,57 +11,4 +23,23 +19,3 +13,13 +61,57 +46,33 +21,25 +46,55 +35,18 +25,25 +41,54 +53,41 +35,61 +7,16 +15,6 +19,58 +59,37 +32,67 +45,48 +23,0 +27,14 +55,54 +56,61 +32,61 +63,60 +39,41 +53,55 +27,70 +49,44 +49,63 +50,57 +53,35 +34,59 +36,13 +25,63 +51,65 +36,55 +37,68 +43,34 +31,70 +63,44 +46,61 +27,25 +47,43 +11,34 +39,62 +51,39 +65,59 +62,33 +59,66 +46,67 +37,63 +51,61 +23,19 +25,19 +55,63 +51,43 +66,39 +7,23 +28,57 +67,62 +58,49 +69,67 +15,31 +41,49 +41,69 +21,6 +53,45 +53,46 +17,1 +14,25 +33,26 +25,20 +27,65 +15,24 +68,49 +39,47 +0,7 +55,57 +28,11 +7,6 +48,41 +56,63 +9,25 +12,13 +28,15 +22,7 +45,45 +13,19 +40,35 +67,57 +49,37 +7,13 +31,18 +23,25 +63,53 +66,67 +27,61 +50,37 +12,17 +17,13 +54,65 +29,15 +64,51 +52,45 +67,49 +17,10 +49,55 +11,15 +37,67 +10,15 +65,69 +21,5 +21,33 +50,47 +19,11 +69,45 +57,64 +25,17 +50,65 +45,59 +61,45 +4,15 +26,19 +17,26 +43,63 +5,21 +12,11 +20,33 +13,29 +47,62 +53,37 +36,53 +28,9 +23,11 +59,53 +18,29 +59,41 +15,9 +45,38 +62,43 +37,60 +51,67 +55,66 +51,45 +54,43 +49,51 +16,15 +21,18 +43,35 +61,63 +32,23 +57,39 +23,13 +51,42 +39,59 +15,15 +41,66 +23,65 +45,36 +19,29 +42,59 +28,27 +49,59 +61,51 +29,61 +61,58 +10,19 +43,39 +63,49 +22,27 +11,32 +7,21 +39,68 +41,46 +19,16 +7,12 +53,31 +15,12 +69,55 +13,5 +41,63 +29,63 +41,62 +45,55 +6,7 +43,67 +31,17 +14,31 +39,64 +30,17 +12,1 +26,1 +6,19 +65,55 +35,35 +7,15 +62,69 +65,49 +3,13 +61,39 +15,27 +47,47 +11,25 +57,49 +61,33 +11,19 +51,47 +45,32 +35,58 +42,53 +1,7 +52,23 +5,9 +41,53 +43,49 +68,57 +49,41 +61,55 +5,7 +45,47 +25,21 +63,67 +69,46 +37,62 +15,3 +25,27 +41,52 +27,27 +13,8 +66,59 +53,54 +69,54 +57,67 +47,63 +58,69 +46,41 +41,41 +39,58 +67,45 +27,21 +47,51 +27,32 +63,64 +60,49 +47,69 +65,65 +47,70 +1,11 +21,27 +55,46 +67,53 +9,8 +7,9 +2,17 +23,1 +28,29 +3,11 +19,15 +49,61 +30,13 +41,64 +55,49 +1,12 +64,41 +57,66 +55,55 +9,3 +27,29 +49,39 +4,1 +27,13 +37,34 +55,52 +21,23 +40,59 +18,23 +19,17 +53,69 +65,35 +1,5 +24,17 +70,35 +51,36 +37,57 +11,5 +15,19 +53,68 +9,24 +45,53 +54,69 +36,61 +42,47 +51,27 +39,48 +65,34 +69,69 +35,59 +62,57 +43,55 +35,62 +55,69 +45,37 +37,43 +47,67 +37,51 +5,16 +51,50 +63,46 +7,5 +51,38 +49,40 +9,1 +61,69 +23,9 +61,46 +53,49 +0,13 +63,41 +8,13 +67,61 +51,49 +23,22 +33,16 +61,62 +26,23 +42,37 +11,3 +51,57 +67,37 +37,69 +57,58 +26,61 +68,35 +13,11 +7,2 +43,37 +27,18 +53,59 +23,15 +23,14 +61,67 +48,51 +25,11 +17,14 +23,5 +45,58 +28,63 +60,51 +18,15 +55,67 +39,61 +49,45 +63,39 +45,61 +21,10 +61,64 +41,59 +55,31 +3,1 +37,52 +70,67 +57,65 +46,43 +19,6 +17,8 +5,0 +10,1 +13,28 +45,57 +29,66 +6,3 +30,21 +53,40 +44,41 +68,55 +33,20 +5,1 +43,59 +57,59 +66,45 +43,51 +17,22 +25,69 +16,11 +29,65 +65,48 +58,47 +45,46 +43,44 +33,45 +39,45 +16,7 +48,65 +54,61 +15,32 +35,65 +33,56 +17,27 +60,45 +61,35 +25,67 +37,58 +49,31 +21,8 +19,1 +20,31 +39,49 +18,19 +51,52 +12,9 +13,1 +19,13 +69,63 +55,61 +54,63 +9,27 +51,60 +15,13 +32,13 +9,13 +2,9 +31,65 +46,51 +37,59 +43,60 +35,64 +17,21 +59,49 +16,1 +1,9 +11,17 +35,16 +13,21 +25,68 +52,63 +59,60 +33,23 +31,13 +25,28 +66,65 +27,66 +35,63 +21,17 +59,67 +63,66 +31,69 +5,3 +11,1 +17,31 +63,56 +19,19 +56,51 +13,18 +17,17 +28,59 +50,41 +35,66 +59,45 +27,19 +5,23 +57,63 +7,1 +47,55 +52,49 +43,64 +14,11 +66,69 +9,10 +21,9 +35,22 +15,23 +5,13 +30,65 +37,56 +9,19 +21,15 +20,13 +17,4 +69,40 +53,53 +39,44 +30,63 +59,51 +58,55 +63,51 +58,33 +31,20 +55,47 +17,12 +14,1 +50,67 +13,15 +63,69 +65,51 +52,69 +26,21 +47,64 +53,48 +20,1 +69,57 +5,17 +26,9 +8,3 +48,37 +17,11 +44,47 +47,49 +11,7 +33,14 +5,14 +15,16 +47,68 +45,31 +13,3 +1,23 +11,23 +49,62 +62,49 +69,53 +5,19 +41,39 +16,23 +67,65 +39,57 +21,65 +5,15 +43,50 +45,44 +15,5 +20,15 +3,21 +11,27 +69,59 +66,51 +36,21 +27,64 +65,47 +20,63 +70,57 +15,21 +43,43 +1,1 +5,32 +65,20 +47,16 +15,35 +8,65 +61,38 +45,29 +7,34 +6,23 +3,36 +47,28 +5,69 +6,31 +44,3 +60,3 +61,19 +17,42 +54,19 +55,36 +56,1 +12,33 +63,6 +3,63 +16,69 +33,60 +61,5 +14,35 +67,21 +23,35 +41,6 +17,66 +69,12 +56,35 +45,13 +15,65 +62,31 +39,6 +21,1 +33,9 +16,63 +6,37 +30,3 +19,46 +61,25 +17,35 +7,53 +9,54 +4,45 +29,35 +34,43 +45,15 +13,64 +55,5 +6,29 +47,9 +9,49 +31,9 +27,41 +65,33 +32,1 +50,13 +14,69 +1,18 +33,5 +7,27 +49,15 +43,24 +65,29 +44,29 +53,15 +37,12 +1,17 +65,10 +31,25 +62,17 +17,67 +45,11 +23,33 +55,9 +56,21 +51,29 +7,38 +47,25 +39,34 +68,3 +5,44 +42,9 +33,39 +62,25 +29,40 +39,23 +55,35 +15,37 +33,43 +67,28 +37,48 +26,43 +41,20 +35,27 +41,34 +32,47 +29,47 +56,43 +43,17 +63,2 +57,4 +7,70 +41,27 +11,65 +1,53 +11,35 +21,59 +11,55 +37,40 +57,8 +63,7 +38,3 +43,41 +23,47 +1,54 +70,23 +17,56 +67,23 +43,23 +3,50 +15,55 +18,53 +35,15 +7,51 +39,27 +23,31 +14,49 +19,55 +37,11 +22,37 +10,23 +68,9 +25,34 +58,27 +33,53 +27,34 +61,29 +5,56 +2,57 +31,44 +58,23 +49,11 +29,59 +65,27 +1,55 +17,62 +35,33 +55,27 +15,48 +4,39 +61,14 +40,9 +10,59 +48,31 +9,56 +49,3 +29,31 +61,11 +3,49 +15,25 +58,21 +46,19 +9,39 +56,33 +50,19 +39,35 +46,1 +47,20 +35,45 +37,66 +6,53 +49,9 +33,31 +31,35 +49,1 +35,50 +62,27 +27,44 +23,55 +35,55 +53,27 +51,31 +21,67 +26,49 +69,1 +8,45 +25,47 +9,30 +7,61 +38,9 +67,1 +10,49 +14,61 +59,69 +36,5 +7,43 +5,29 +29,25 +48,19 +3,25 +65,19 +15,60 +11,68 +16,59 +41,9 +17,45 +51,28 +0,51 +63,0 +68,15 +49,17 +39,30 +51,16 +67,18 +3,31 +69,15 +17,39 +26,57 +23,45 +27,5 +31,31 +37,29 +43,5 +22,33 +51,20 +29,3 +17,69 +55,37 +15,29 +68,33 +7,19 +14,43 +47,15 +29,2 +9,45 +38,41 +57,5 +31,11 +21,55 +52,15 +3,48 +22,61 +67,31 +63,27 +11,39 +45,5 +40,37 +37,42 +37,13 +11,62 +25,37 +59,23 +65,31 +34,9 +13,37 +49,53 +29,41 +25,2 +66,17 +16,29 +59,30 +69,62 +18,43 +64,31 +16,51 +57,11 +38,15 +17,25 +37,31 +33,49 +69,58 +9,21 +15,33 +69,31 +47,6 +48,35 +33,55 +51,18 +59,12 +54,23 +57,27 +47,14 +55,7 +18,61 +0,37 +11,47 +57,13 +13,51 +27,49 +29,56 +0,69 +9,63 +63,25 +22,29 +10,65 +51,21 +70,19 +21,51 +55,18 +65,15 +31,33 +12,63 +35,23 +63,33 +51,25 +59,33 +60,15 +27,9 +47,27 +17,49 +70,13 +39,63 +1,59 +19,32 +55,20 +50,11 +65,7 +45,41 +65,30 +15,53 +27,55 +47,21 +6,41 +47,17 +21,45 +5,59 +13,66 +57,69 +35,44 +54,13 +33,3 +67,35 +47,26 +59,21 +45,20 +31,15 +5,43 +11,42 +37,47 +61,13 +3,54 +67,29 +3,67 +55,16 +48,17 +53,11 +17,59 +26,5 +57,35 +29,45 +23,67 +35,43 +3,64 +1,52 +35,5 +36,19 +60,63 +9,61 +49,29 +57,41 +27,38 +1,33 +9,50 +7,3 +9,36 +51,66 +11,43 +55,26 +15,63 +29,53 +3,59 +33,57 +9,67 +20,43 +10,41 +55,51 +9,31 +31,40 +13,50 +17,48 +57,12 +21,54 +50,27 +13,33 +12,67 +48,29 +6,51 +27,45 +13,61 +55,8 +41,11 +63,28 +40,21 +30,33 +23,54 +67,27 +41,1 +33,27 +1,63 +23,49 +43,9 +5,60 +15,11 +18,37 +69,8 +13,59 +42,11 +25,33 +1,56 +43,1 +31,53 +3,46 +37,9 +55,15 +55,25 +49,26 +12,55 +35,14 +19,35 +3,30 +55,32 +19,42 +9,65 +44,5 +1,29 +11,33 +37,28 +49,7 +38,21 +47,22 +5,66 +41,23 +37,5 +61,37 +19,38 +53,2 +29,36 +24,31 +8,67 +53,26 +3,45 +48,5 +21,37 +1,42 +25,46 +59,36 +45,12 +15,46 +13,55 +24,5 +55,23 +57,15 +47,7 +41,43 +32,35 +50,7 +8,53 +37,1 +17,65 +26,7 +19,49 +63,45 +58,13 +3,35 +37,10 +59,17 +68,25 +69,11 +17,51 +40,3 +4,27 +7,69 +33,35 +1,19 +29,55 +47,8 +9,43 +63,15 +38,27 +41,33 +13,39 +17,61 +49,27 +2,69 +60,33 +19,37 +9,37 +17,41 +9,62 +7,25 +25,52 +25,1 +35,21 +25,41 +37,38 +0,31 +6,65 +3,69 +7,35 +39,3 +36,31 +19,70 +45,25 +27,57 +69,17 +69,10 +59,11 +55,10 +37,25 +7,39 +31,55 +24,39 +3,33 +26,31 +64,15 +57,19 +23,37 +31,58 +3,61 +5,53 +35,36 +39,67 +28,1 +6,47 +63,35 +25,39 +53,13 +41,5 +7,59 +32,37 +16,57 +41,42 +29,69 +39,7 +28,41 +57,23 +13,49 +23,39 +35,40 +43,25 +33,58 +50,5 +61,40 +65,13 +70,1 +53,21 +9,28 +51,7 +59,35 +47,39 +52,7 +58,17 +19,51 +34,47 +21,41 +39,13 +57,7 +2,67 +1,35 +2,43 +37,19 +23,62 +23,51 +2,33 +66,11 +35,39 +69,47 +20,45 +27,43 +20,65 +58,31 +25,58 +49,0 +32,7 +1,37 +27,37 +54,29 +23,43 +11,59 +12,39 +16,33 +25,35 +4,67 +13,52 +37,33 +51,8 +2,59 +39,11 +10,57 +55,11 +39,12 +58,15 +23,57 +51,30 +42,1 +57,21 +38,17 +7,56 +54,5 +60,25 +17,63 +57,22 +41,35 +1,27 +67,33 +3,39 +7,40 +49,23 +60,23 +61,36 +60,27 +33,42 +33,1 +45,35 +19,31 +65,2 +34,11 +25,55 +19,57 +41,22 +39,33 +31,10 +67,20 +61,16 +5,42 +61,31 +53,9 +40,41 +6,61 +39,9 +8,35 +31,29 +59,3 +31,47 +46,13 +69,7 +43,11 +14,67 +41,57 +33,29 +68,29 +33,37 +21,19 +19,63 +29,52 +11,61 +50,35 +41,17 +25,56 +36,49 +29,57 +25,5 +39,25 +48,9 +34,49 +60,29 +45,16 +13,48 +55,2 +31,1 +3,52 +17,50 +63,17 +57,3 +31,54 +43,12 +1,51 +37,3 +43,3 +53,7 +33,25 +56,13 +2,39 +18,65 +49,21 +19,33 +23,41 +13,35 +61,3 +7,42 +43,13 +11,69 +25,31 +64,13 +37,37 +11,45 +49,19 +64,5 +34,39 +21,63 +56,47 +27,35 +68,21 +20,59 +7,55 +28,45 +55,19 +21,62 +58,7 +31,38 +23,30 +31,39 +44,7 +20,55 +17,43 +37,35 +24,37 +61,20 +69,5 +22,57 +15,34 +29,1 +48,39 +57,33 +40,25 +44,15 +63,13 +58,25 +18,47 +31,26 +13,46 +9,51 +21,46 +52,27 +13,57 +62,5 +45,23 +21,66 +16,53 +41,29 +38,1 +3,22 +47,11 +39,31 +47,2 +9,60 +52,21 +33,11 +15,38 +67,54 +45,33 +55,41 +9,23 +13,43 +59,13 +38,19 +35,6 +17,57 +39,39 +62,19 +61,27 +4,41 +65,18 +67,2 +5,51 +38,23 +61,4 +18,59 +53,19 +30,57 +56,5 +35,11 +35,7 +1,65 +17,68 +37,39 +39,43 +65,8 +33,41 +13,67 +67,12 +35,13 +35,47 +13,69 +11,53 +27,53 +63,21 +53,6 +2,19 +9,57 +7,47 +8,27 +1,41 +15,57 +5,39 +37,8 +7,67 +9,44 +1,62 +3,51 +27,33 +69,28 +8,63 +45,10 +44,25 +45,3 +17,40 +5,68 +54,7 +19,65 +65,1 +8,51 +33,13 +19,59 +15,49 +63,31 +33,7 +41,31 +22,23 +55,24 +5,25 +59,5 +4,49 +1,43 +61,15 +36,25 +29,33 +37,41 +49,5 +47,35 +57,0 +3,53 +67,36 +37,27 +28,51 +33,46 +64,17 +41,30 +5,45 +35,38 +28,55 +1,67 +3,19 +47,3 +43,21 +1,40 +67,17 +4,61 +62,35 +56,25 +34,29 +33,19 +55,17 +3,57 +41,3 +13,38 +45,17 +29,60 +53,28 +52,11 +39,17 +66,33 +21,47 +21,56 +61,21 +2,49 +63,34 +63,3 +66,7 +25,57 +17,44 +24,7 +28,47 +7,31 +51,5 +41,16 +27,46 +21,61 +5,47 +61,1 +13,45 +19,61 +65,23 +49,10 +29,49 +53,17 +8,39 +64,37 +55,30 +31,50 +17,29 +65,5 +37,23 +5,49 +25,42 +31,61 +35,29 +31,3 +34,3 +21,40 +7,49 +47,5 +19,53 +65,26 +59,43 +25,53 +5,52 +23,68 +69,13 +13,41 +39,19 +58,45 +65,17 +42,5 +1,31 +27,50 +3,47 +41,15 +47,23 +55,13 +33,2 +69,33 +69,29 +67,22 +14,41 +61,22 +4,29 +69,6 +43,2 +61,8 +27,47 +19,64 +64,25 +51,1 +39,1 +5,35 +31,24 +45,22 +55,43 +17,55 +19,43 +54,39 +65,32 +32,33 +54,31 +36,11 +53,1 +15,52 +33,6 +7,65 +10,39 +61,17 +28,39 +36,43 +1,61 +47,31 +5,24 +5,36 +39,65 +25,61 +37,6 +30,37 +27,7 +29,51 +69,27 +29,67 +10,67 +49,25 +11,63 +39,26 +61,47 +4,59 +1,49 +25,45 +55,14 +53,3 +33,8 +31,45 +45,19 +19,60 +35,31 +23,36 +44,33 +21,69 +29,43 +9,55 +49,60 +15,69 +60,19 +63,5 +35,37 +62,11 +29,34 +2,37 +40,19 +33,33 +39,14 +15,17 +42,31 +7,63 +58,3 +62,9 +52,3 +41,40 +23,48 +33,32 +17,33 +69,9 +29,54 +60,31 +29,37 +57,17 +34,19 +59,15 +7,37 +11,37 +45,27 +43,7 +35,41 +2,63 +10,69 +24,45 +10,37 +21,43 +37,36 +33,51 +3,58 +35,2 +23,61 +59,6 +29,8 +50,31 +67,19 +43,26 +45,1 +32,51 +4,55 +1,57 +7,68 +14,55 +3,38 +13,44 +63,65 +0,47 +31,51 +41,37 +3,20 +21,52 +35,3 +57,25 +27,36 +10,47 +39,38 +57,28 +59,9 +30,45 +2,29 +2,25 +65,28 +29,27 +65,25 +42,13 +63,24 +46,29 +0,21 +59,27 +61,9 +7,58 +23,34 +11,51 +31,27 +10,43 +66,1 +19,52 +68,31 +23,60 +11,36 +30,27 +13,58 +7,48 +6,27 +53,16 +16,65 +35,0 +7,30 +26,53 +31,4 +57,40 +25,49 +32,29 +59,25 +19,39 +15,51 +1,47 +36,45 +9,22 +25,43 +25,51 +40,17 +9,59 +67,14 +25,29 +5,41 +1,39 +63,12 +47,24 +49,24 +67,13 +53,23 +19,41 +8,59 +8,47 +43,22 +30,51 +48,11 +55,33 +59,7 +31,43 +69,3 +4,63 +69,61 +35,26 +27,1 +67,9 +22,47 +9,47 +69,21 +27,3 +3,43 +57,18 +21,57 +49,13 +52,13 +42,43 +9,29 +5,55 +23,3 +48,3 +49,33 +65,11 +1,66 +43,14 +51,23 +68,17 +27,63 +37,7 +27,39 +70,31 +9,41 +59,1 +41,8 +25,54 +61,23 +23,70 +55,29 +55,21 +63,23 +37,24 +19,68 +5,57 +35,52 +64,9 +43,15 +67,25 +32,41 +9,33 +5,61 +57,9 +51,4 +25,59 +9,46 +7,57 +40,57 +59,2 +43,29 +17,38 +7,45 +56,9 +41,24 +59,18 +23,69 +53,10 +69,43 +3,29 +21,30 +45,9 +51,13 +25,7 +33,4 +62,1 +23,44 +25,48 +57,31 +12,45 +43,31 +19,23 +31,37 +47,1 +21,11 +57,29 +41,7 +31,7 +1,34 +17,47 +64,21 +65,4 +45,7 +11,50 +50,1 +29,28 +69,19 +41,32 +29,12 +60,9 +46,27 +39,15 +53,25 +1,64 +51,9 +9,32 +7,33 +43,27 +50,33 +18,55 +15,47 +3,55 +5,31 +37,15 +29,42 +51,11 +57,38 +65,3 +3,37 +12,53 +67,6 +44,9 +65,21 +67,3 +23,63 +14,57 +47,29 +23,53 +41,13 +15,39 +21,49 +15,40 +41,45 +1,16 +27,4 +20,69 +19,47 +61,12 +40,11 +59,10 +3,27 +45,21 +43,18 +17,30 +5,37 +6,63 +53,29 +59,65 +56,29 +29,48 +55,1 +20,49 +61,7 +35,34 +23,42 +31,42 +49,35 +17,37 +11,57 +12,61 +67,11 +19,36 +35,9 +1,21 +9,69 +43,28 +34,35 +35,28 +3,15 +43,19 +5,26 +39,5 +39,29 +36,3 +35,1 +62,39 +65,9 +59,19 +65,38 +69,26 +67,24 +31,49 +51,15 +15,41 +63,1 +5,34 +12,41 +55,39 +19,67 +47,37 +30,5 +25,4 +59,31 +10,53 +5,27 +1,28 +40,27 +13,65 +47,13 +17,53 +37,16 +27,51 +6,57 +15,45 +63,37 +31,48 +23,52 +21,50 +23,59 +15,54 +30,31 +59,39 +47,4 +23,7 +22,43 +21,53 +26,35 +51,17 +29,39 +31,5 +39,37 +7,41 +15,43 +16,35 +63,11 +23,50 +63,19 +38,31 +32,53 +9,35 +46,7 +20,35 +20,39 +63,22 +2,45 +23,40 +59,63 +23,64 +12,59 +13,47 +3,32 +50,15 +15,61 +51,19 +43,20 +51,3 +43,30 +49,22 +6,49 +27,31 +63,9 +31,41 +57,1 +55,38 +15,67 +48,13 +17,3 +46,31 +47,19 +13,36 +21,35 +11,41 +66,15 +43,33 +1,69 +7,29 +45,4 +60,39 +26,39 +16,45 +19,45 +21,39 +41,21 +1,26 +35,32 +66,23 +55,3 +67,5 +41,25 +5,67 +36,35 +13,53 +15,59 +1,45 +23,56 +53,5 +67,4 +63,29 +29,7 +5,63 +30,9 +19,69 +35,49 +41,19 +47,33 +54,3 +32,11 +65,36 +9,53 +29,5 +69,25 +5,33 +5,65 +40,1 +67,7 +69,23 +11,49 +27,30 +3,41 +67,15 +31,57 +42,17 +37,30 +11,67 +3,65 +13,63 +33,30 +29,29 +62,64 +46,22 +64,11 +28,36 +39,28 +46,15 +38,68 +58,65 +39,40 +40,64 +5,58 +65,12 +53,38 +44,16 +54,21 +39,56 +36,37 +0,40 +58,63 +20,37 +22,26 +28,18 +66,13 +53,62 +58,5 +3,70 +30,70 +54,25 +48,16 +24,54 +4,51 +46,50 +18,39 +14,51 +32,27 +47,50 +5,4 +33,52 +50,26 +28,60 +64,66 +46,52 +4,6 +8,11 +38,62 +53,30 +48,23 +44,49 +44,36 +49,70 +20,50 +48,48 +60,48 +6,70 +44,22 +58,39 +66,4 +21,34 +24,24 +1,58 +22,36 +66,64 +52,24 +6,30 +52,18 +30,36 +59,24 +47,42 +66,66 +56,15 +6,6 +22,24 +58,44 +8,26 +3,34 +42,52 +50,40 +58,22 +4,66 +16,60 +36,27 +62,50 +42,62 +30,44 +26,18 +54,17 +59,22 +68,66 +10,25 +38,24 +37,20 +32,66 +23,26 +54,36 +37,18 +2,32 +63,58 +0,60 +34,33 +52,31 +58,19 +18,35 +36,30 +46,30 +20,44 +30,15 +1,36 +52,44 +42,57 +12,70 +28,69 +16,38 +12,46 +32,42 +26,66 +68,48 +10,33 +47,32 +5,40 +38,11 +8,52 +54,6 +13,68 +4,35 +36,40 +8,10 +39,42 +1,70 +56,8 +6,40 +11,18 +44,31 +48,22 +31,62 +2,47 +6,25 +40,23 +26,46 +42,56 +40,22 +0,46 +6,18 +58,60 +20,4 +54,22 +36,29 +42,44 +64,20 +40,54 +24,33 +20,23 +44,52 +30,62 +68,62 +60,11 +12,51 +12,44 +70,47 +66,27 +58,57 +26,67 +14,66 +42,20 +30,66 +62,34 +2,68 +46,40 +2,54 +6,64 +28,67 +15,56 +70,63 +26,63 +4,50 +5,22 +43,40 +0,66 +66,5 +12,14 +26,58 +15,70 +12,66 +38,51 +60,8 +58,28 +44,18 +11,48 +34,51 +9,52 +36,64 +36,54 +34,60 +62,13 +22,69 +2,66 +4,58 +63,38 +64,38 +68,59 +6,42 +65,16 +48,36 +8,54 +2,52 +64,36 +32,64 +26,55 +48,18 +24,56 +64,39 +51,26 +26,34 +30,19 +50,48 +40,49 +30,49 +7,52 +4,10 +62,22 +46,23 +42,60 +49,32 +44,48 +47,38 +2,48 +38,54 +60,17 +36,6 +14,0 +21,2 +38,42 +16,46 +42,50 +38,58 +14,58 +34,36 +62,62 +22,0 +20,47 +18,22 +56,45 +12,8 +39,20 +39,18 +20,61 +44,26 +22,18 +65,66 +50,23 +64,64 +68,58 +31,64 +68,52 +60,66 +14,38 +34,26 +69,0 +36,57 +62,66 +60,6 +50,52 +44,37 +17,52 +57,34 +64,27 +50,14 +50,29 +39,24 +24,47 +41,14 +14,36 +46,42 +11,66 +27,48 +47,52 +29,32 +16,61 +53,32 +54,15 +28,62 +8,33 +14,40 +48,27 +65,60 +50,17 +22,44 +30,64 +31,36 +19,44 +15,44 +67,40 +64,33 +31,34 +27,2 +32,48 +30,41 +48,54 +50,38 +34,2 +40,70 +16,41 +57,36 +66,41 +42,41 +34,45 +44,43 +59,16 +38,61 +45,0 +21,48 +44,21 +1,0 +66,61 +20,34 +6,43 +4,46 +42,63 +32,52 +34,22 +40,50 +12,68 +8,48 +22,42 +59,4 +2,62 +26,38 +70,6 +20,41 +62,58 +60,37 +33,48 +6,58 +22,52 +34,42 +37,32 +61,18 +44,11 +50,32 +70,60 +12,65 +17,58 +59,40 +42,49 +24,34 +65,24 +54,28 +17,0 +48,14 +35,12 +19,66 +48,10 +54,0 +44,39 +49,36 +54,26 +23,38 +29,38 +57,44 +20,11 +52,41 +18,40 +60,16 +58,46 +26,32 +6,54 +29,50 +22,20 +30,47 +52,0 +62,56 +56,40 +14,50 +62,48 +62,59 +4,14 +4,70 +19,50 +0,49 +56,6 +0,65 +42,16 +8,34 +39,66 +40,53 +35,46 +26,40 +22,39 +42,22 +4,36 +28,42 +70,49 +20,20 +26,33 +34,32 +60,68 +34,24 +19,56 +8,60 +59,38 +2,38 +18,46 +22,38 +24,46 +34,50 +4,0 +16,0 +31,28 +40,42 +12,19 +11,46 +4,30 +48,38 +5,38 +41,36 +46,45 +56,26 +53,24 +11,60 +33,28 +36,7 +34,64 +21,58 +28,56 +56,7 +46,68 +61,60 +32,38 +20,36 +30,58 +47,18 +66,18 +62,20 +40,31 +31,14 +28,54 +9,18 +18,4 +59,34 +45,14 +52,35 +26,62 +63,36 +40,44 +27,20 +42,38 +68,64 +38,43 +34,61 +58,12 +0,36 +64,62 +10,46 +52,30 +56,46 +48,25 +42,64 +26,68 +9,40 +3,56 +20,5 +56,12 +36,10 +62,54 +7,26 +40,8 +69,16 +55,22 +30,54 +68,7 +60,52 +64,50 +20,3 +13,24 +14,48 +1,46 +70,26 +4,28 +62,70 +28,30 +15,42 +54,4 +50,4 +48,32 +20,51 +44,65 +48,62 +14,32 +18,14 +60,0 +10,8 +44,0 +16,3 +58,34 +44,17 +52,67 +41,26 +51,12 +22,34 +47,40 +0,67 +24,16 +46,66 +34,44 +8,68 +48,20 +58,6 +44,68 +46,17 +32,5 +32,32 +5,70 +39,16 +59,50 +17,34 +32,12 +8,4 +2,61 +8,64 +0,27 +28,37 +36,67 +40,40 +62,38 +36,65 +64,14 +20,68 +26,28 +24,66 +30,67 +10,2 +50,61 +53,22 +66,70 +15,66 +66,28 +31,52 +49,14 +35,70 +25,6 +12,48 +24,22 +44,35 +14,53 +57,70 +60,13 +69,42 +67,58 +62,16 +56,31 +20,7 +24,44 +28,68 +40,47 +17,6 +21,26 +22,60 +14,22 +69,36 +14,28 +54,57 +56,56 +66,34 +66,40 +28,5 +52,10 +46,11 +24,10 +46,38 +10,62 +8,49 +21,28 +28,22 +13,34 +61,44 +36,69 +28,64 +2,55 +26,6 +46,54 +22,22 +44,45 +49,64 +31,22 +62,55 +61,30 +55,64 +11,14 +10,64 +64,30 +28,17 +8,21 +27,6 +64,2 +64,40 +64,32 +7,10 +42,23 +63,54 +49,18 +46,2 +24,40 +61,48 +58,8 +18,25 +11,8 +39,60 +0,34 +38,4 +21,64 +25,44 +6,67 +64,70 +7,0 +32,16 +30,52 +0,39 +70,38 +36,18 +28,16 +38,26 +67,16 +61,68 +9,64 +55,34 +32,59 +8,28 +66,37 +60,57 +42,26 +62,37 +8,9 +40,68 +64,60 +6,60 +26,2 +60,5 +22,10 +70,22 +2,56 +70,46 +34,70 +24,70 +64,29 +60,40 +22,6 +24,1 +22,31 +6,24 +36,0 +6,26 +26,12 +58,4 +4,69 +38,16 +70,29 +20,25 +19,62 +56,42 +10,70 +40,56 +36,46 +26,41 +17,64 +14,4 +52,47 +6,68 +60,28 +13,60 +8,62 +45,2 +4,57 +18,38 +60,4 +30,1 +16,31 +38,18 +68,53 +38,35 +11,70 +14,46 +52,25 +36,20 +54,40 +15,58 +58,10 +32,19 +42,33 +56,17 +10,28 +18,28 +30,69 +28,13 +53,56 +66,38 +69,68 +14,19 +35,60 +3,8 +0,63 +18,34 +65,6 +4,48 +30,11 +9,14 +68,56 +52,62 +32,54 +69,64 +37,2 +2,16 +5,8 +46,4 +12,40 +36,14 +38,14 +54,44 +16,18 +0,41 +13,12 +21,44 +18,69 +52,46 +40,39 +54,1 +60,53 +30,7 +42,32 +12,47 +4,25 +2,50 +2,41 +13,32 +63,10 +44,70 +29,26 +16,4 +24,62 +44,30 +4,17 +62,15 +49,30 +68,40 +16,28 +60,61 +55,28 +16,58 +11,52 +52,5 +8,61 +11,58 +68,54 +38,20 +54,10 +4,12 +44,27 +64,4 +6,44 +16,2 +0,44 +52,58 +60,58 +64,67 +54,52 +10,58 +70,30 +11,56 +52,68 +68,14 +7,44 +34,15 +66,55 +56,22 +6,8 +50,2 +69,18 +49,38 +56,64 +28,2 +44,14 +70,8 +66,31 +0,58 +54,60 +10,44 +46,70 +53,14 +48,56 +17,54 +58,16 +18,52 +59,46 +54,55 +41,10 +14,44 +48,40 +14,45 +24,41 +34,56 +4,18 +45,8 +62,42 +28,20 +68,34 +70,9 +45,42 +54,66 +50,69 +40,67 +4,54 +29,46 +14,13 +6,16 +40,63 +17,24 +38,40 +26,8 +54,64 +28,24 +58,66 +6,34 +22,40 +26,69 +69,32 +64,49 +49,46 +66,6 +42,27 +10,45 +15,28 +34,46 +24,2 +0,17 +4,53 +2,14 +24,14 +18,2 +51,10 +36,38 +38,44 +20,57 +26,48 +42,61 +46,24 +44,4 +44,59 +68,28 +64,46 +56,16 +68,12 +57,20 +5,2 +34,14 +46,46 +36,50 +44,67 +30,56 +34,54 +26,44 +11,28 +27,42 +43,32 +0,11 +20,6 +33,18 +49,6 +18,1 +7,66 +53,44 +16,55 +36,63 +31,6 +4,34 +60,10 +34,10 +61,6 +26,17 +70,0 +5,30 +48,66 +4,24 +0,29 +39,10 +28,12 +46,53 +16,43 +34,41 +20,10 +36,48 +44,53 +57,24 +29,20 +33,22 +66,25 +14,70 +38,33 +67,10 +70,69 +54,54 +16,42 +37,26 +45,28 +26,0 +20,54 +18,58 +64,55 +10,51 +54,53 +70,15 +52,14 +54,46 +33,34 +17,36 +48,12 +26,27 +11,30 +40,0 +62,45 +50,6 +58,26 +30,28 +0,64 +49,66 +54,47 +24,30 +62,24 +20,17 +38,5 +66,46 +42,45 +32,40 +8,66 +53,70 +55,4 +69,48 +42,48 +10,29 +22,30 +53,8 +10,34 +33,40 +10,42 +20,8 +29,4 +66,9 +0,53 +57,46 +2,4 +54,68 +70,7 +24,48 +22,35 +2,5 +6,69 +0,48 +69,70 +2,21 +58,36 +25,18 +66,30 +54,50 +46,62 +48,45 +0,3 +12,69 +30,32 +18,11 +30,23 +25,38 +22,50 +49,52 +54,67 +44,28 +30,42 +41,44 +58,41 +23,32 +36,4 +40,2 +14,2 +60,24 +58,0 +5,46 +50,8 +39,8 +68,8 +10,22 +12,62 +64,54 +10,36 +11,22 +42,2 +44,20 +14,54 +62,3 +66,12 +48,7 +0,30 +12,54 +62,63 +67,44 diff --git a/day18/maze.go b/day18/maze.go new file mode 100644 index 0000000..f239ecd --- /dev/null +++ b/day18/maze.go @@ -0,0 +1,191 @@ +package main + +import ( + "os" + "fmt" + "strings" + "strconv" +) + + +// ---------------------------------------- +type Direction uint8 + +const ( + Up Direction = iota + Down Direction = iota + Left Direction = iota + Right Direction = iota +) + + +// ---------------------------------------- +type Position struct { + coord [2]uint + direc Direction +} + +func (p Position) StepUnbound(d Direction) Position { + switch { + case d == Down: p.coord[1]++ + case d == Up: p.coord[1]-- + case d == Left: p.coord[0]-- + case d == Right: p.coord[0]++ + } + p.direc = d + + return p +} + + + +// ---------------------------------------- +type Maze struct { + start [2]uint + end [2]uint + field [][]byte +} + +func NewMaze(byte_positions string, bytes_to_load uint, size [2]uint) *Maze { + /* size = [2]unit{x,y} */ + + var maze Maze + + // create board + maze.field = make([][]byte, 0, size[1] + 2) + for i := range size[1] + 2 { + line := make([]byte, size[0] + 2) + for j := range line { + if i == 0 || uint(i) == size[1] + 1 || j == 0 || uint(j) == size[0] + 1 { + line[j] = '#' + } else { + line[j] = '.' + } + } + maze.field = append(maze.field, line) + } + + // add byte corruptions + lines := strings.Split(byte_positions, "\n") + for i, line := range lines { + if uint(i) >= bytes_to_load { break } + x, err := strconv.Atoi(strings.Split(line, ",")[0]) + check(err) + y, err := strconv.Atoi(strings.Split(line, ",")[1]) + check(err) + + maze.field[y + 1][x + 1] = '#' + } + + // set start/ end + maze.start = [2]uint{1,1} + maze.end = size + + return &maze +} + +func (m *Maze) Step(pos Position) []Position { + out := make([]Position, 0, 4) + + if m.field[pos.coord[1] + 1][pos.coord[0]] == '.' && pos.direc != Up { + out = append(out, pos.StepUnbound(Down)) + } + if m.field[pos.coord[1] - 1][pos.coord[0]] == '.' && pos.direc != Down { + out = append(out, pos.StepUnbound(Up)) + } + if m.field[pos.coord[1]][pos.coord[0] + 1] == '.' && pos.direc != Left { + out = append(out, pos.StepUnbound(Right)) + } + if m.field[pos.coord[1]][pos.coord[0] - 1] == '.' && pos.direc != Right { + out = append(out, pos.StepUnbound(Left)) + } + + return out +} + +func (m *Maze) Start() Position { + return Position{m.start, Right} +} + +func (m *Maze) Finished(pos Position) bool { + return m.end == pos.coord +} + +func (m *Maze) Print() { + for _, line := range m.field { + fmt.Println(string(line)) + } +} + + +// ---------------------------------------- +type MazeGraph struct { + cost uint + children []*MazeGraph +} + +var new_maze_graph_visited map[Position]uint + +func NewMazeGraph(m *Maze, start Position, cost uint) *MazeGraph { + children := make([]*MazeGraph, 0, 3) + + // finish reached? + if m.Finished(start) { return &MazeGraph{ cost, children } } + + // check for loop + value, exists := new_maze_graph_visited[start] + if exists { + if value <= cost { return nil } + } + new_maze_graph_visited[start] = cost + + // build subgraphs/ children + for _, position := range m.Step(start) { + child := NewMazeGraph(m, position, cost + 1) + if child != nil { children = append(children, child) } + } + + // found dead end + if len(children) == 0 { return nil } + + // collapse if there is only one possible way + if len(children) == 0 { return children[0] } + + return &MazeGraph{ cost: cost, children: children } +} + +func (graph *MazeGraph) Leaves() []*MazeGraph { + out := make([]*MazeGraph, 0) + + if len(graph.children) == 0 { + out = append(out, graph) + return out + } + + for _, g := range graph.children { + out = append(out, g.Leaves()...) + } + return out +} + +// ---------------------------------------- +func check(err error) { if err != nil { panic(err) } } + + +func main() { + dat, err := os.ReadFile("data.txt") + check(err) + + input := strings.TrimSpace(string(dat)) + maze := NewMaze(input, 1024, [2]uint{71,71}) + //maze.Print() + + var best *MazeGraph + new_maze_graph_visited = make(map[Position]uint) + + for _, leave := range NewMazeGraph(maze, maze.Start(), 0).Leaves() { + if best == nil || best.cost > leave.cost { best = leave } + } + fmt.Println(best.cost) +} +