| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package main
- import (
- "bufio"
- "fmt"
- "os"
- )
- type coord struct {
- x,y int
- }
- func main() {
- fmt.Println("Advent of Code - Day 7 - Part 2")
- 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
- store_split := make(map[coord]int)
- res = count_timelines(initial_position, splitters, limit, &store_split)
- fmt.Println(res)
- }
- func count_timelines(pos coord, splitters []coord, limit coord, store_split* map[coord]int) int {
- ss, ok := (*store_split)[pos]
- if ok {
- return ss
- }
-
- if pos.x >= limit.x || pos.y >= limit.y || pos.y < 0 {
- return 1
- }
- for _,s := range splitters {
- if pos.x == s.x && pos.y == s.y {
- n_pos1:=coord{pos.x, pos.y-1}
- n_pos2:=coord{pos.x, pos.y+1}
- (*store_split)[n_pos1] = count_timelines(coord{pos.x,pos.y-1},splitters, limit, store_split)
- (*store_split)[n_pos2] = count_timelines(coord{pos.x,pos.y+1},splitters, limit, store_split)
- return (*store_split)[n_pos1] + (*store_split)[n_pos2]
- }
- }
- n_pos := coord{pos.x+1,pos.y}
- (*store_split)[n_pos] = count_timelines(coord{pos.x+1,pos.y},splitters, limit, store_split)
- return (*store_split)[n_pos]
-
- }
|