|
@@ -0,0 +1,82 @@
|
|
|
|
|
+package main
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "bufio"
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+ "os"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+type coord struct {
|
|
|
|
|
+ x,y int
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func main() {
|
|
|
|
|
+ fmt.Println("Advent of Code - Day 7 - Part 1")
|
|
|
|
|
+ f,_ := os.Open("7/input")
|
|
|
|
|
+ defer f.Close()
|
|
|
|
|
+ scanner := bufio.NewScanner(f)
|
|
|
|
|
+ y := 0
|
|
|
|
|
+ scanner.Scan()
|
|
|
|
|
+ y_limit := len(scanner.Text())
|
|
|
|
|
+ for i := range(scanner.Text()){
|
|
|
|
|
+ if scanner.Text()[i] == 'S'{
|
|
|
|
|
+ y = i
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ initial_position := coord{0,y}
|
|
|
|
|
+ x:=1
|
|
|
|
|
+ var splitters []coord
|
|
|
|
|
+ for scanner.Scan(){
|
|
|
|
|
+ line := scanner.Text()
|
|
|
|
|
+ for i:= range line{
|
|
|
|
|
+ if line[i] == '^'{
|
|
|
|
|
+ splitters = append(splitters, coord{x,i})
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ x++
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ limit := coord{x,y_limit}
|
|
|
|
|
+ res := 0
|
|
|
|
|
+ count_splits(initial_position, splitters, &res, limit)
|
|
|
|
|
+ fmt.Println(res)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+var visited []coord
|
|
|
|
|
+func is_visited(pos coord) bool {
|
|
|
|
|
+ for _,v := range visited {
|
|
|
|
|
+ if pos.x == v.x && pos.y == v.y {
|
|
|
|
|
+ return true
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func count_splits(pos coord, splitters []coord, acc* int, limit coord){
|
|
|
|
|
+
|
|
|
|
|
+ if (is_visited(pos)){
|
|
|
|
|
+ return
|
|
|
|
|
+ } else {
|
|
|
|
|
+ visited = append(visited, pos)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if len(splitters) == 0 {return}
|
|
|
|
|
+ if pos.x >= limit.x || pos.y >= limit.y || pos.y < 0 {return}
|
|
|
|
|
+
|
|
|
|
|
+ splitted := false
|
|
|
|
|
+ for i,s := range splitters {
|
|
|
|
|
+ if pos.x == s.x && pos.y == s.y {
|
|
|
|
|
+ splitters[i] = splitters[len(splitters)-1]
|
|
|
|
|
+ splitters = splitters[:len(splitters)-1]
|
|
|
|
|
+ *acc = *acc + 1
|
|
|
|
|
+ count_splits(coord{pos.x,pos.y-1},splitters, acc, limit)
|
|
|
|
|
+ count_splits(coord{pos.x,pos.y+1},splitters, acc, limit)
|
|
|
|
|
+ splitted = true
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if !splitted {
|
|
|
|
|
+ count_splits(coord{pos.x+1,pos.y},splitters, acc, limit)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|