From 8a1c5bacbfda86e0b0de19ad7622fc153789289a Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 23 Dec 2024 13:37:12 +0100 Subject: [PATCH] day23p1 --- day23p1/solution.go | 47 +++++++++++++++++++++++++++++ day23p1/solution_test.go | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 day23p1/solution.go create mode 100644 day23p1/solution_test.go diff --git a/day23p1/solution.go b/day23p1/solution.go new file mode 100644 index 0000000..4a6d8d4 --- /dev/null +++ b/day23p1/solution.go @@ -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) +} diff --git a/day23p1/solution_test.go b/day23p1/solution_test.go new file mode 100644 index 0000000..7c508cd --- /dev/null +++ b/day23p1/solution_test.go @@ -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) + } + } +}