Skip to content

Commit

Permalink
day23p1
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 23, 2024
1 parent 3e5d606 commit 8a1c5ba
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
47 changes: 47 additions & 0 deletions day23p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package day23p1

import (
"io"
"sort"
"strings"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

network := make(map[string]map[string]bool)

for _, line := range lines {
parts := strings.Split(line, "-")
a, b := parts[0], parts[1]
if _, ok := network[a]; !ok {
network[a] = make(map[string]bool)
}
network[a][b] = true
if _, ok := network[b]; !ok {
network[b] = make(map[string]bool)
}
network[b][a] = true
}

triples := make(map[[3]string]bool)
for c1, cs := range network {
for c2 := range cs {
for c3 := range network[c2] {
if c3 == c1 || !network[c3][c1] {
continue
}
triple := [3]string{c1, c2, c3}

if c1[0] == 't' || c2[0] == 't' || c3[0] == 't' {
sort.Strings(triple[:])
triples[triple] = true
}
}
}
}

return len(triples)
}
64 changes: 64 additions & 0 deletions day23p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package day23p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 7},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}

0 comments on commit 8a1c5ba

Please sign in to comment.