|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +//go:embed data.txt |
| 12 | +var fileContents string |
| 13 | + |
| 14 | +type rule struct { |
| 15 | + before, after int |
| 16 | +} |
| 17 | + |
| 18 | +func parse(input string) ([]rule, [][]int) { |
| 19 | + lines := strings.Split(strings.TrimSpace(input), "\n") |
| 20 | + |
| 21 | + separatorIdx := -1 |
| 22 | + for i, line := range lines { |
| 23 | + if line == "" { |
| 24 | + separatorIdx = i |
| 25 | + break |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + rules := make([]rule, len(lines[:separatorIdx])) |
| 30 | + for i, line := range lines[:separatorIdx] { |
| 31 | + parts := strings.Split(line, "|") |
| 32 | + before, _ := strconv.Atoi(parts[0]) |
| 33 | + after, _ := strconv.Atoi(parts[1]) |
| 34 | + rules[i] = rule{before, after} |
| 35 | + } |
| 36 | + |
| 37 | + // parse updates |
| 38 | + var updates [][]int |
| 39 | + for _, line := range lines[separatorIdx+1:] { |
| 40 | + var update []int |
| 41 | + for _, num := range strings.Split(line, ",") { |
| 42 | + n, _ := strconv.Atoi(num) |
| 43 | + update = append(update, n) |
| 44 | + } |
| 45 | + updates = append(updates, update) |
| 46 | + } |
| 47 | + |
| 48 | + return rules, updates |
| 49 | +} |
| 50 | + |
| 51 | +func isValidUpdate(update []int, rules []rule) bool { |
| 52 | + // for quick lookups |
| 53 | + indexMap := make(map[int]int) |
| 54 | + for i, num := range update { |
| 55 | + indexMap[num] = i |
| 56 | + } |
| 57 | + |
| 58 | + for _, rule := range rules { |
| 59 | + beforeIdx, beforeExists := indexMap[rule.before] |
| 60 | + afterIdx, afterExists := indexMap[rule.after] |
| 61 | + |
| 62 | + if beforeExists && afterExists && beforeIdx > afterIdx { |
| 63 | + return false |
| 64 | + } |
| 65 | + } |
| 66 | + return true |
| 67 | +} |
| 68 | + |
| 69 | +func buildGraph(update []int, rules []rule) map[int][]int { |
| 70 | + graph := make(map[int][]int) |
| 71 | + inDegree := make(map[int]int) |
| 72 | + |
| 73 | + for _, num := range update { |
| 74 | + if _, exists := graph[num]; !exists { |
| 75 | + graph[num] = []int{} |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + for _, rule := range rules { |
| 80 | + if slices.Contains(update, rule.before) && slices.Contains(update, rule.after) { |
| 81 | + graph[rule.before] = append(graph[rule.before], rule.after) |
| 82 | + inDegree[rule.after]++ |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return graph |
| 87 | +} |
| 88 | + |
| 89 | +func topologicalSort(update []int, rules []rule) []int { |
| 90 | + graph := buildGraph(update, rules) |
| 91 | + inDegree := make(map[int]int) |
| 92 | + result := make([]int, 0, len(update)) |
| 93 | + |
| 94 | + // initial in-degrees |
| 95 | + for _, num := range update { |
| 96 | + inDegree[num] = 0 |
| 97 | + } |
| 98 | + for _, rule := range rules { |
| 99 | + if slices.Contains(update, rule.before) && slices.Contains(update, rule.after) { |
| 100 | + inDegree[rule.after]++ |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // find nodes with no incoming edges |
| 105 | + var queue []int |
| 106 | + for _, num := range update { |
| 107 | + if inDegree[num] == 0 { |
| 108 | + queue = append(queue, num) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + for len(queue) > 0 { |
| 113 | + current := queue[0] |
| 114 | + queue = queue[1:] |
| 115 | + result = append(result, current) |
| 116 | + |
| 117 | + for _, neighbor := range graph[current] { |
| 118 | + inDegree[neighbor]-- |
| 119 | + if inDegree[neighbor] == 0 { |
| 120 | + queue = append(queue, neighbor) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return result |
| 126 | +} |
| 127 | + |
| 128 | +func main() { |
| 129 | + rules, updates := parse(fileContents) |
| 130 | + |
| 131 | + sum := 0 |
| 132 | + for _, update := range updates { |
| 133 | + if !isValidUpdate(update, rules) { |
| 134 | + sortedUpdate := topologicalSort(update, rules) |
| 135 | + middleIdx := len(sortedUpdate) / 2 |
| 136 | + sum += sortedUpdate[middleIdx] |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + fmt.Println(sum) |
| 141 | +} |
0 commit comments