solve_1.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. type coord struct {
  8. x,y int
  9. }
  10. func main() {
  11. fmt.Println("Advent of Code - Day 7 - Part 1")
  12. f,_ := os.Open("7/input")
  13. defer f.Close()
  14. scanner := bufio.NewScanner(f)
  15. y := 0
  16. scanner.Scan()
  17. y_limit := len(scanner.Text())
  18. for i := range(scanner.Text()){
  19. if scanner.Text()[i] == 'S'{
  20. y = i
  21. }
  22. }
  23. initial_position := coord{0,y}
  24. x:=1
  25. var splitters []coord
  26. for scanner.Scan(){
  27. line := scanner.Text()
  28. for i:= range line{
  29. if line[i] == '^'{
  30. splitters = append(splitters, coord{x,i})
  31. }
  32. }
  33. x++
  34. }
  35. limit := coord{x,y_limit}
  36. res := 0
  37. count_splits(initial_position, splitters, &res, limit)
  38. fmt.Println(res)
  39. }
  40. var visited []coord
  41. func is_visited(pos coord) bool {
  42. for _,v := range visited {
  43. if pos.x == v.x && pos.y == v.y {
  44. return true
  45. }
  46. }
  47. return false
  48. }
  49. func count_splits(pos coord, splitters []coord, acc* int, limit coord){
  50. if (is_visited(pos)){
  51. return
  52. } else {
  53. visited = append(visited, pos)
  54. }
  55. if pos.x >= limit.x || pos.y >= limit.y || pos.y < 0 {return}
  56. for _,s := range splitters {
  57. if pos.x == s.x && pos.y == s.y {
  58. *acc = *acc + 1
  59. count_splits(coord{pos.x,pos.y-1},splitters, acc, limit)
  60. count_splits(coord{pos.x,pos.y+1},splitters, acc, limit)
  61. return
  62. }
  63. }
  64. count_splits(coord{pos.x+1,pos.y},splitters, acc, limit)
  65. }