aboutsummaryrefslogtreecommitdiff
path: root/2021/12/first.go
blob: 7b9df44b4bdf3ace1860c5ac3b1c55a6045277a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

type cave struct {
	name  string
	big   bool
	leafs []int // index in caves array
}

var (
	caves = []cave{cave{name: "start"}, cave{name: "end"}}
	score = 0
)

const (
	START = 0
	END   = 1
)

func getIdOrAdd(name string) int {
	for i, v := range caves {
		if v.name == name {
			return i
		}
	}
	caves = append(caves, cave{
		name: name,
		big:  name[0] >= 'A' && name[0] <= 'Z',
	})
	return len(caves) - 1
}

func contains(id int, passed []int) bool {
	for _, v := range passed {
		if v == id {
			return true
		}
	}
	return false
}

func parcours(id int, passed []int) {
	if !caves[id].big {
		if contains(id, passed) {
			return
		}
		passed = append(passed, id)
	}
	for _, v := range caves[id].leafs {
		if v == END {
			score++
			continue
		}
		parcours(v, passed)
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		line := strings.Split(scanner.Text(), "-")
		a := getIdOrAdd(line[0])
		b := getIdOrAdd(line[1])
		caves[a].leafs = append(caves[a].leafs, b)
		caves[b].leafs = append(caves[b].leafs, a)
	}
	parcours(START, nil)
	fmt.Println(score)
}