| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- 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 pos.x >= limit.x || pos.y >= limit.y || pos.y < 0 {return}
- for _,s := range splitters {
- if pos.x == s.x && pos.y == s.y {
- *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)
- return
- }
- }
-
- count_splits(coord{pos.x+1,pos.y},splitters, acc, limit)
- }
|