Skip to content

Commit

Permalink
feat(angch/2024-05): Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
angch committed Dec 5, 2024
1 parent b9451d0 commit 8728bbf
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
121 changes: 121 additions & 0 deletions angch/2024-05/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)

func day5(file string) (part1, part2 int) {
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer f.Close()

scanner := bufio.NewScanner(f)
rules := make(map[int][]int)
printbefore := make(map[int][]int)
for scanner.Scan() {
t := scanner.Text()
if t == "" {
break
}
p1, p2 := 0, 0
fmt.Sscanf(t, "%d|%d", &p1, &p2)

_, ok := rules[p1]
if !ok {
rules[p1] = make([]int, 0)
}
rules[p1] = append(rules[p1], p2)

_, ok = printbefore[p2]
if !ok {
printbefore[p2] = make([]int, 0)
}
printbefore[p2] = append(printbefore[p2], p1)
}
// log.Println(rules)

updates := [][]int{}
for scanner.Scan() {
t := scanner.Text()
f := strings.Split(t, ",")
u := []int{}
for _, v := range f {
i, err := strconv.Atoi(v)
if err == nil {
u = append(u, i)
}
}
updates = append(updates, u)
}
// log.Println(updates)

for _, update := range updates {
ispart2 := false
a:
printed := make(map[int]int)
good := true

toprint := make(map[int]bool)
for _, v := range update {
toprint[v] = true
}
swap1, swap2 := 0, 0
page := 0
b:
for k1, v := range update {
if rules[v] != nil {
for _, p := range rules[v] {
if !toprint[p] {
continue
}
if printed[p] > 0 {
swap1 = printed[p]
swap2 = k1
good = false
break b
}
}
}
page++
printed[v] = page
}
if good {
mid := len(update) / 2
if !ispart2 {
part1 += update[mid]
} else {
part2 += update[mid]
}
} else {
// part2 reorder
swap1--
// log.Println(swap1, swap2)
update[swap1], update[swap2] = update[swap2], update[swap1]
// log.Println(update)
ispart2 = true
goto a // Meh
}
}

return
}

func main() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
t1 := time.Now()
part1, part2 := day5("test.txt")
fmt.Println(part1, part2)
if part1 != 143 || part2 != 123 {
log.Fatal("Test failed ", part1, part2)
}
fmt.Println(day5("input.txt"))
fmt.Println("Elapsed time:", time.Since(t1))
}
28 changes: 28 additions & 0 deletions angch/2024-05/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13

75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

0 comments on commit 8728bbf

Please # to comment.