Abderrahmane Faiz 2 veckor sedan
förälder
incheckning
f38ee66223
3 ändrade filer med 77 tillägg och 11 borttagningar
  1. 4 10
      7/solve_1.go
  2. 72 0
      7/solve_2.go
  3. 1 1
      README.md

+ 4 - 10
7/solve_1.go

@@ -60,23 +60,17 @@ func count_splits(pos coord, splitters []coord, acc* int, limit coord){
 		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 {
+	for _,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
+			return
 		}
 	}
 
-	if !splitted {
-		count_splits(coord{pos.x+1,pos.y},splitters, acc, limit)
-	}
+	
+	count_splits(coord{pos.x+1,pos.y},splitters, acc, limit)
 }

+ 72 - 0
7/solve_2.go

@@ -0,0 +1,72 @@
+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]
+
+	
+}

+ 1 - 1
README.md

@@ -26,7 +26,7 @@ Each day's folder is named after the day number (`1` to `12`). Inside each folde
 | 4   | ✅     | ✅     |
 | 5   | ✅     | ✅     |
 | 6   | ✅     | ✅     |
-| 7   | ✅     |      |
+| 7   | ✅     |      |
 | 8   | ⬜     | ⬜     |
 | 9   | ⬜     | ⬜     |
 | 10  | ⬜     | ⬜     |