commit b77c6a8dcef11abbf40225fe6897f2bae8388350 Author: gutmet Date: Thu Jun 10 21:27:25 2021 +0200 Init diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5f405ab --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +SRC = $(wildcard *.dot) +FMT = jpg +OBJ = ${SRC:.dot=.${FMT}} +VID = search.mp4 + +all: search recurse movie + +clean: + rm -f *.${FMT} + rm -f step*.dot + rm -f ${VID} + +graphs: ${OBJ} + +recurse: + @echo "Generating .${FMT}s..." + @$(MAKE) graphs + @echo "...done." + +search: + @echo "Searching path..." + @go run hamilton.go || echo "Found nothing." + +movie: + @ffmpeg -y -f image2 -r 2/1 -i step%07d.jpg -c:v libx264 -s "1920x1080" ${VID} + +%.${FMT}: %.dot + @neato $< -T${FMT} -o $@ + +.PHONY: all clean search graphs recurse movie diff --git a/Readme b/Readme new file mode 100644 index 0000000..44b18b1 --- /dev/null +++ b/Readme @@ -0,0 +1,8 @@ +Beispielcode für das Rätsel in Rezos Video ( https://youtu.be/MVajSA0Itq4?t=240 ) + +Was ihr braucht, um das Programm auszuführen und das Rätsel zu durchlaufen: + +- make +- Go +- Graphviz +- ffmpeg diff --git a/grid0.dot b/grid0.dot new file mode 100644 index 0000000..fc7f13a --- /dev/null +++ b/grid0.dot @@ -0,0 +1,26 @@ +graph grid +{ + dpi=300; + layout=dot + labelloc = "c" + node [label="",shape=circle, style=filled, color=chartreuse4] + // arbitrary path on rigid grid + edge [weight=1000 color=dimgrey minlen=2.0 len=2.0] + + // uncomment to hide the grid + edge [style=invis] + + A0 -- A1 -- A2 -- A3 -- A4 + B0 -- B1 -- B2 -- B3 -- B4 + C0 -- C1 -- C2 -- C3 -- C4 + D0 -- D1 -- D2 -- D3 -- D4 + E0 -- E1 -- E2 -- E3 -- E4 + + rank=same {A0 -- B0 -- C0 -- D0 -- E0} + rank=same {A1 -- B1 -- C1 -- D1 -- E1} + rank=same {A2 -- B2 -- C2 -- D2 -- E2} + rank=same {A3 -- B3 -- C3 -- D3 -- E3} + rank=same {A4 -- B4 -- C4 -- D4 -- E4} + + A0,A1,B0,B1,C1,D1,D2 [color=crimson] +} diff --git a/grid1.dot b/grid1.dot new file mode 100644 index 0000000..ab21170 --- /dev/null +++ b/grid1.dot @@ -0,0 +1,26 @@ +graph grid +{ + dpi=300; + layout=dot + labelloc = "c" + node [label="",shape=circle, style=filled, color=chartreuse4] + // arbitrary path on rigid grid + edge [weight=1000 color=dimgrey minlen=2.0 len=2.0] + + // uncomment to hide the grid + //edge [style=invis] + + A0 -- A1 -- A2 -- A3 -- A4 + B0 -- B1 -- B2 -- B3 -- B4 + C0 -- C1 -- C2 -- C3 -- C4 + D0 -- D1 -- D2 -- D3 -- D4 + E0 -- E1 -- E2 -- E3 -- E4 + + rank=same {A0 -- B0 -- C0 -- D0 -- E0} + rank=same {A1 -- B1 -- C1 -- D1 -- E1} + rank=same {A2 -- B2 -- C2 -- D2 -- E2} + rank=same {A3 -- B3 -- C3 -- D3 -- E3} + rank=same {A4 -- B4 -- C4 -- D4 -- E4} + + A0,A1,B0,B1,C1,D1,D2 [color=crimson] +} diff --git a/grid2.dot b/grid2.dot new file mode 100644 index 0000000..efb1c14 --- /dev/null +++ b/grid2.dot @@ -0,0 +1,26 @@ +graph grid +{ + dpi=300; + layout=dot + labelloc = "c" + node [label="",shape=circle, style=filled, color=chartreuse4] + // arbitrary path on rigid grid + edge [weight=1000 color=dimgrey minlen=2.0 len=2.0] + + // uncomment to hide the grid + //edge [style=invis] + + A2 -- A3 -- A4 + B2 -- B3 -- B4 + C2 -- C3 -- C4 + D3 -- D4 + E0 -- E1 -- E2 -- E3 -- E4 + + rank=same {C0 -- D0 -- E0} + rank=same {E1} + rank=same {A2 -- B2 -- C2} + rank=same {A3 -- B3 -- C3 -- D3 -- E3} + rank=same {A4 -- B4 -- C4 -- D4 -- E4} + + //A0,A1,B0,B1,C1,D1,D2 [color=crimson] +} diff --git a/grid3.dot b/grid3.dot new file mode 100644 index 0000000..bb89e02 --- /dev/null +++ b/grid3.dot @@ -0,0 +1,43 @@ +graph grid +{ + dpi=300; + layout=dot + labelloc = "c" + node [label="",shape=circle, style=filled, color=chartreuse4] + // arbitrary path on rigid grid + edge [weight=1000 color=dimgrey minlen=2.0 len=2.0] + + // uncomment to hide the grid + //edge [style=invis] + + A2 -- A3 -- A4 + B2 -- B3 -- B4 + C2 -- C3 -- C4 + D3 -- D4 + E0 -- E1 -- E2 -- E3 -- E4 + + rank=same {C0 -- D0 -- E0} + rank=same {E1} + rank=same {A2 -- B2 -- C2} + rank=same {A3 -- B3 -- C3 -- D3 -- E3} + rank=same {A4 -- B4 -- C4 -- D4 -- E4} + + C0 [xlabel="0"] + D0 [xlabel="1"] + E0 [xlabel="2"] + E1 [xlabel="3"] + E2 [xlabel="4"] + E3 [xlabel="5"] + E4 [xlabel="6"] + D4 [xlabel="7"] + C4 [xlabel="8"] + B4 [xlabel="9"] + A4 [xlabel="10"] + A3 [xlabel="11"] + B3 [xlabel="12"] + C3 [xlabel="13"] + D3 [xlabel="14"] + A2 [xlabel="15"] + B2 [xlabel="16"] + C2 [xlabel="17"] +} diff --git a/grid4.dot b/grid4.dot new file mode 100644 index 0000000..20aff53 --- /dev/null +++ b/grid4.dot @@ -0,0 +1,50 @@ +digraph { +dpi=200 +node [shape=circle, style=filled] +0 -> 1 +1 -> 0 +1 -> 2 +2 -> 1 +2 -> 3 +3 -> 2 +3 -> 4 +4 -> 3 +4 -> 5 +5 -> 4 +5 -> 6 +5 -> 14 +6 -> 5 +6 -> 7 +7 -> 6 +7 -> 8 +7 -> 14 +8 -> 7 +8 -> 9 +8 -> 13 +9 -> 8 +9 -> 10 +9 -> 12 +10 -> 9 +10 -> 11 +11 -> 10 +11 -> 12 +11 -> 15 +12 -> 9 +12 -> 11 +12 -> 13 +12 -> 16 +13 -> 8 +13 -> 12 +13 -> 14 +13 -> 17 +14 -> 5 +14 -> 7 +14 -> 13 +15 -> 11 +15 -> 16 +16 -> 12 +16 -> 15 +16 -> 17 +17 -> 13 +17 -> 16 +} diff --git a/hamilton.go b/hamilton.go new file mode 100644 index 0000000..3e2cb78 --- /dev/null +++ b/hamilton.go @@ -0,0 +1,92 @@ +package main + +import ( + "fmt" + "os" +) + +var g [][]int = [][]int{ + []int{1}, + []int{0, 2}, + []int{1, 3}, + []int{2, 4}, + []int{3, 5}, + []int{4, 6, 14}, + []int{5, 7}, + []int{6, 8, 14}, + []int{7, 9, 13}, + []int{8, 10, 12}, + []int{9, 11}, + []int{10, 12, 15}, + []int{9, 11, 13, 16}, + []int{8, 12, 14, 17}, + []int{5, 7, 13}, + []int{11, 16}, + []int{12, 15, 17}, + []int{13, 16}, +} + +var step int + +func writeGraphfile(g [][]int, path []int) { + filename := fmt.Sprintf("step%07d.dot", step) + f, _ := os.Create(filename) + step++ + fmt.Fprintln(f, "digraph {") + fmt.Fprintln(f, "dpi=200") + fmt.Fprintln(f, "node [shape=circle, style=filled, label=\"\"]") + for v1, adjlist := range g { + for _, v2 := range adjlist { + fmt.Fprintln(f, v1, " -> ", v2) + } + } + for i, _ := range g { + fmt.Fprintln(f, i, "[xlabel=x]") + } + for i, v := range path { + fmt.Fprintln(f, v, "[xlabel=", i, "]") + fillcolor := "red" + if i == 0 { + fillcolor = "blue" + } else if i == len(path)-1 { + fillcolor = "yellow" + } + fmt.Fprintln(f, v, "[fillcolor=", fillcolor, "]") + } + fmt.Fprintln(f, "}") +} + +func contains(path []int, v int) bool { + for _, w := range path { + if w == v { + return true + } + } + return false +} + +func DFS(g [][]int, path []int) { + deadend := true + for _, v2 := range g[path[len(path)-1]] { + if contains(path, v2) { + continue + } + deadend = false + path2 := append(path, v2) + DFS(g, path2) + } + if len(path) == len(g) { + fmt.Println("Found path: ", path) + writeGraphfile(g, path) + os.Exit(0) + } else if deadend { + writeGraphfile(g, path) + } +} + +func main() { + step = 0 + writeGraphfile(g, []int{}) + DFS(g, []int{0}) + os.Exit(-1) +}