Skip to content

Commit

Permalink
Day 25: Snowverload
Browse files Browse the repository at this point in the history
  • Loading branch information
abeltay committed Dec 27, 2023
1 parent b42ca3c commit 070c03e
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
163 changes: 163 additions & 0 deletions 25/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package main

import (
"math/rand"
"sort"
"strings"

"github.com/abeltay/advent-of-code-2023/utilities"
)

func parseInput(filename string) [][]bool {
in := utilities.ParseFile(filename)

all := make(map[string]bool)
for _, row := range in {
s := strings.Split(row, ": ")
all[s[0]] = true
for _, v := range strings.Split(s[1], " ") {
all[v] = true
}
}
names := make([]string, 0, len(all))
for k := range all {
names = append(names, k)
}
sort.Strings(names)
connections := make([][]bool, len(names))
for i := range names {
connections[i] = make([]bool, len(names))
}
for _, row := range in {
s := strings.Split(row, ": ")
from := sort.SearchStrings(names, s[0])
for _, v := range strings.Split(s[1], " ") {
to := sort.SearchStrings(names, v)
connections[from][to] = true
connections[to][from] = true
}
}
return connections
}

type node struct {
num int
parent *node
}

func bfs(connections [][]bool, breaks []connect, from, to int) *node {
visited := make([]bool, len(connections))
visited[from] = true
queue := []node{{num: from}}
for len(queue) > 0 {
last := queue[0]
queue = queue[1:]
if last.num == to {
return &last
}
for i := range connections[last.num] {
if !connections[last.num][i] || visited[i] {
continue
}
var broken bool
for _, v := range breaks {
if v.from == last.num && v.to == i {
broken = true
}
}
if broken {
continue
}
visited[i] = true
queue = append(queue, node{num: i, parent: &last})
}
}
return nil
}

type connect struct {
from int
to int
}

func connectedAfter3Breaks(connections [][]bool, from, to int) []connect {
var breaks []connect
for i := 0; i < 3; i++ {
path := bfs(connections, breaks, from, to)
from := path.num
next := path.parent
for next != nil {
breaks = append(breaks, connect{from, next.num})
breaks = append(breaks, connect{next.num, from})
from = next.num
next = next.parent
}
}
path := bfs(connections, breaks, from, to)
if path != nil {
return nil
}
return breaks
}

func findSize(connections [][]bool, breaks []connect, from int) int {
visited := make([]bool, len(connections))
visited[from] = true
queue := []node{{num: from}}
for len(queue) > 0 {
last := queue[0]
queue = queue[1:]
for i := range connections[last.num] {
if !connections[last.num][i] || visited[i] {
continue
}
var broken bool
for _, v := range breaks {
if v.from == last.num && v.to == i {
broken = true
break
}
}
if broken {
continue
}
visited[i] = true
queue = append(queue, node{num: i, parent: &last})
}
}
var count int
for _, v := range visited {
if v {
count++
}
}
return count
}

func part1(filename string) int {
connected := parseInput(filename)
sameGroup := []int{0}
source := 0
r := rand.New(rand.NewSource(99))
var breaks []connect
for {
n := int(r.Int31n(int32(len(connected))))
var exists bool
for _, v := range sameGroup {
if v == n {
exists = true
break
}
}
if exists {
continue
}
breaks = connectedAfter3Breaks(connected, source, n)
if breaks != nil {
break
}
sameGroup = append(sameGroup, n)
}
size := findSize(connected, breaks, 0)
return size * (len(connected) - size)
}
19 changes: 19 additions & 0 deletions 25/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"testing"
)

func TestParts(t *testing.T) {
testFile := "testdata/test.txt"
got := part1(testFile)
want := 54
if got != want {
t.Fatalf("got %v want %v", got, want)
}

fmt.Println("Running input.txt")
actualFile := "testdata/input.txt"
fmt.Println("Answer for part 1:", part1(actualFile))
}
13 changes: 13 additions & 0 deletions 25/testdata/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jqt: rhn xhk nvd
rsh: frs pzl lsr
xhk: hfx
cmg: qnr nvd lhk bvb
rhn: xhk bvb hfx
bvb: xhk hfx
pzl: lsr hfx nvd
qnr: nvd
ntq: jqt hfx bvb xhk
nvd: lhk
lsr: lhk
rzs: qnr cmg lsr rsh
frs: qnr lhk lsr

0 comments on commit 070c03e

Please sign in to comment.