Added solutions for 12th day: caves pathing
This commit is contained in:
parent
26b9d8e224
commit
86a723beeb
6 changed files with 213 additions and 0 deletions
7
2021/12/example
Normal file
7
2021/12/example
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
start-A
|
||||||
|
start-b
|
||||||
|
A-c
|
||||||
|
A-b
|
||||||
|
b-d
|
||||||
|
A-end
|
||||||
|
b-end
|
10
2021/12/example2
Normal file
10
2021/12/example2
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
dc-end
|
||||||
|
HN-start
|
||||||
|
start-kj
|
||||||
|
dc-start
|
||||||
|
dc-HN
|
||||||
|
LN-dc
|
||||||
|
HN-end
|
||||||
|
kj-sa
|
||||||
|
kj-HN
|
||||||
|
kj-dc
|
18
2021/12/example3
Normal file
18
2021/12/example3
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
fs-end
|
||||||
|
he-DX
|
||||||
|
fs-he
|
||||||
|
start-DX
|
||||||
|
pj-DX
|
||||||
|
end-zg
|
||||||
|
zg-sl
|
||||||
|
zg-pj
|
||||||
|
pj-he
|
||||||
|
RW-he
|
||||||
|
fs-DX
|
||||||
|
pj-RW
|
||||||
|
zg-RW
|
||||||
|
start-pj
|
||||||
|
he-WI
|
||||||
|
zg-he
|
||||||
|
pj-fs
|
||||||
|
start-RW
|
75
2021/12/first.go
Normal file
75
2021/12/first.go
Normal file
|
@ -0,0 +1,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)
|
||||||
|
}
|
21
2021/12/input
Normal file
21
2021/12/input
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
TR-start
|
||||||
|
xx-JT
|
||||||
|
xx-TR
|
||||||
|
hc-dd
|
||||||
|
ab-JT
|
||||||
|
hc-end
|
||||||
|
dd-JT
|
||||||
|
ab-dd
|
||||||
|
TR-ab
|
||||||
|
vh-xx
|
||||||
|
hc-JT
|
||||||
|
TR-vh
|
||||||
|
xx-start
|
||||||
|
hc-ME
|
||||||
|
vh-dd
|
||||||
|
JT-bm
|
||||||
|
end-ab
|
||||||
|
dd-xx
|
||||||
|
end-TR
|
||||||
|
hc-TR
|
||||||
|
start-vh
|
82
2021/12/second.go
Normal file
82
2021/12/second.go
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
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, doublePass bool) {
|
||||||
|
if !caves[id].big {
|
||||||
|
if contains(id, passed) {
|
||||||
|
if doublePass {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
doublePass = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
passed = append(passed, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, v := range caves[id].leafs {
|
||||||
|
if v == END {
|
||||||
|
score++
|
||||||
|
continue
|
||||||
|
} else if v == START {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
parcours(v, passed, doublePass)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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, false)
|
||||||
|
fmt.Println(score)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue