-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package day23p2 | ||
|
||
import ( | ||
"io" | ||
"sort" | ||
"strings" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
type Party map[string]bool | ||
|
||
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 | ||
} | ||
|
||
var largestParty Party | ||
|
||
for c1 := range network { | ||
party := dfs(network, c1, make(Party)) | ||
if len(party) > len(largestParty) { | ||
largestParty = party | ||
} | ||
} | ||
largestPartyPW := make([]string, 0, len(largestParty)) | ||
for k := range largestParty { | ||
largestPartyPW = append(largestPartyPW, k) | ||
} | ||
sort.Strings(largestPartyPW) | ||
|
||
return strings.Join(largestPartyPW, ",") | ||
} | ||
|
||
func dfs(network map[string]map[string]bool, c string, visited Party) Party { | ||
visited[c] = true | ||
var largestParty Party | ||
NextParty: | ||
for c2 := range network[c] { | ||
if visited[c2] { | ||
continue | ||
} | ||
for prev := range visited { | ||
if !network[c2][prev] { | ||
continue NextParty | ||
} | ||
} | ||
party := dfs(network, c2, visited) | ||
if len(party) > len(largestParty) { | ||
largestParty = party | ||
} | ||
} | ||
if len(largestParty) == 0 { | ||
// deep clone visited | ||
largestParty = make(Party) | ||
for k, v := range visited { | ||
largestParty[k] = v | ||
} | ||
} | ||
return visited | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package day23p2 | ||
|
||
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 string | ||
}{ | ||
{testInput, "co,de,ka,ta"}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(string) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %s, got %s", test.answer, result) | ||
} | ||
} | ||
} |