Skip to content

Commit 4b706e7

Browse files
committed
d5
1 parent be39c20 commit 4b706e7

File tree

6 files changed

+1553
-140
lines changed

6 files changed

+1553
-140
lines changed

2024/day04/input2.txt

Lines changed: 0 additions & 140 deletions
This file was deleted.

2024/day04/sample2.txt

Whitespace-only changes.

2024/day05/day05.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package day05
2+
3+
import (
4+
"aoc/util"
5+
)
6+
7+
var (
8+
rules = [][]int{}
9+
updates = [][]int{}
10+
nodes = map[int]node{}
11+
)
12+
13+
type node struct {
14+
val int
15+
children map[int]bool
16+
parents map[int]bool
17+
}
18+
19+
func load(inputFile string) {
20+
data, _ := util.ReadFileToStringSlice(inputFile)
21+
// data, _ := util.ReadFileToIntSlice(inputFile)
22+
// data, _ := util.ReadFileToStringSliceWithDelim(inputFile, "\n\n")
23+
// grid := util.NewInfinityGridFromFile(inputfile, ".")
24+
25+
onRules := true
26+
for _, line := range data {
27+
if line == "" {
28+
onRules = false
29+
continue
30+
}
31+
32+
// tokens := util.ParseTokens(line)
33+
ints := util.ParseInts(line)
34+
// strs := util.ParseStrs(line)
35+
// words := util.ParseWords(line)
36+
37+
if onRules {
38+
rules = append(rules, ints)
39+
continue
40+
}
41+
42+
updates = append(updates, ints)
43+
}
44+
45+
for _, rule := range rules {
46+
l := rule[0]
47+
r := rule[1]
48+
49+
if _, ok := nodes[l]; !ok {
50+
nodes[l] = node{val: l, children: map[int]bool{}, parents: map[int]bool{}}
51+
}
52+
if _, ok := nodes[r]; !ok {
53+
nodes[r] = node{val: r, children: map[int]bool{}, parents: map[int]bool{}}
54+
}
55+
nodes[l].children[r] = true
56+
nodes[r].parents[l] = true
57+
}
58+
59+
// fmt.Printf("Rules: %v\n", rules)
60+
// fmt.Printf("Updes: %v\n", updates)
61+
// fmt.Printf("Nodes: %v\n", nodes)
62+
}
63+
64+
func part1(inputFile string) int {
65+
load(inputFile)
66+
67+
sum := 0
68+
for _, pages := range updates {
69+
bad := false
70+
for i := 0; i < len(pages); i++ {
71+
l := pages[i]
72+
for j := i + 1; j < len(pages); j++ {
73+
r := pages[j]
74+
if !nodes[l].children[r] {
75+
bad = true
76+
i = len(pages)
77+
break
78+
}
79+
}
80+
}
81+
82+
if !bad {
83+
sum += pages[(len(pages) / 2)]
84+
}
85+
}
86+
87+
return sum
88+
}
89+
90+
func part2(inputFile string) int {
91+
load(inputFile)
92+
93+
sum := 0
94+
for _, pages := range updates {
95+
bad := false
96+
for i := 0; i < len(pages); i++ {
97+
l := pages[i]
98+
for j := i + 1; j < len(pages); j++ {
99+
r := pages[j]
100+
if !nodes[l].children[r] {
101+
bad = true
102+
i = len(pages)
103+
break
104+
}
105+
}
106+
}
107+
108+
if !bad {
109+
// sum += pages[(len(pages) / 2)]
110+
continue
111+
}
112+
113+
// reorder
114+
115+
for i := 0; i < len(pages); i++ {
116+
for j := 0; j < len(pages)-i-1; j++ {
117+
l := pages[j]
118+
r := pages[j+1]
119+
120+
if nodes[l].parents[r] {
121+
// nodes in wrong order
122+
pages[j] = r
123+
pages[j+1] = l
124+
}
125+
}
126+
}
127+
128+
sum += pages[(len(pages) / 2)]
129+
}
130+
131+
return sum
132+
}

2024/day05/day05_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package day05
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestP1(t *testing.T) {
9+
got := part1("sample.txt")
10+
fmt.Printf("Got: %v\n", got)
11+
want := 143
12+
if got != want {
13+
t.Errorf("got %v but want %v", got, want)
14+
}
15+
}
16+
17+
func TestP1_Actual(t *testing.T) {
18+
got := part1("input.txt")
19+
fmt.Printf("Got: %v\n", got)
20+
want := 5166
21+
if got != want {
22+
t.Errorf("got %v but want %v", got, want)
23+
}
24+
}
25+
26+
func TestP2(t *testing.T) {
27+
got := part2("sample.txt")
28+
fmt.Printf("Got: %v\n", got)
29+
want := 123
30+
if got != want {
31+
t.Errorf("got %v but want %v", got, want)
32+
}
33+
}
34+
35+
func TestP2_Actual(t *testing.T) {
36+
got := part2("input.txt")
37+
fmt.Printf("Got: %v\n", got)
38+
want := 4679
39+
if got != want {
40+
t.Errorf("got %v but want %v", got, want)
41+
}
42+
}

0 commit comments

Comments
 (0)