Skip to content

Commit fc695aa

Browse files
committed
d3
1 parent 9e3c5be commit fc695aa

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

2024/day03/day03.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package day03
2+
3+
import (
4+
"aoc/util"
5+
"fmt"
6+
"regexp"
7+
"strings"
8+
)
9+
10+
var (
11+
raw string
12+
tokens = []string{
13+
"mul",
14+
"don't()",
15+
"do()",
16+
}
17+
)
18+
19+
func load(inputFile string) {
20+
data, _ := util.ReadFileToStringSlice(inputFile)
21+
raw = strings.Join(data, "")
22+
// data, _ := util.ReadFileToIntSlice(inputFile)
23+
// data, _ := util.ReadFileToStringSliceWithDelim(inputFile, "\n\n")
24+
// grid := util.NewInfinityGridFromFile(inputfile, ".")
25+
26+
// for _, line := range data {
27+
// tokens := util.ParseTokens(line)
28+
// // ints := util.ParseInts(line)
29+
// // strs := util.ParseStrs(line)
30+
// // words := util.ParseWords(line)
31+
32+
// fmt.Println(tokens)
33+
// }
34+
}
35+
36+
func reset(poses []int) {
37+
for i := 0; i < len(poses); i++ {
38+
poses[i] = 0
39+
}
40+
}
41+
42+
func part1(inputFile string) int {
43+
load(inputFile)
44+
45+
re := regexp.MustCompile(`^\([0-9]{1,3},[0-9]{1,3}\)`)
46+
47+
tokenPoses := make([]int, len(tokens))
48+
49+
sum := 0
50+
for i := 0; i < len(raw); i++ {
51+
c := raw[i]
52+
for j := 0; j < len(tokens); j++ {
53+
if tokens[j][tokenPoses[j]] == c {
54+
tokenPoses[j]++
55+
if tokenPoses[j] == len(tokens[j]) {
56+
// token match found, preprocess next stuff
57+
t := raw[i+1:]
58+
str := re.FindString(t)
59+
if str != "" {
60+
tokenPoses[j] = 0
61+
fmt.Printf("Yippie: %s\n", str)
62+
tokens := util.ParseInts(str)
63+
sum += tokens[0] * tokens[1]
64+
}
65+
reset(tokenPoses)
66+
i += len(str)
67+
break
68+
}
69+
}
70+
}
71+
}
72+
73+
return sum
74+
}
75+
76+
func part2(inputFile string) int {
77+
load(inputFile)
78+
79+
// re := regexp.MustCompile(`mul\([0-9]{1,3},[0-9]{1,3}\)`)
80+
re := regexp.MustCompile(`^\([0-9]{1,3},[0-9]{1,3}\)`)
81+
82+
tokenPoses := make([]int, len(tokens))
83+
84+
enabled := true
85+
sum := 0
86+
for i := 0; i < len(raw); i++ {
87+
c := raw[i]
88+
for j := 0; j < len(tokens); j++ {
89+
if tokens[j][tokenPoses[j]] == c {
90+
tokenPoses[j]++
91+
if tokenPoses[j] == len(tokens[j]) {
92+
if j == 0 && enabled {
93+
// token match found, preprocess next stuff
94+
t := raw[i+1:]
95+
str := re.FindString(t)
96+
if str != "" {
97+
tokenPoses[j] = 0
98+
fmt.Printf("Yippie: %s\n", str)
99+
tokens := util.ParseInts(str)
100+
sum += tokens[0] * tokens[1]
101+
}
102+
i += len(str)
103+
}
104+
105+
if j == 1 {
106+
enabled = false
107+
}
108+
109+
if j == 2 {
110+
enabled = true
111+
}
112+
113+
reset(tokenPoses)
114+
break
115+
}
116+
}
117+
}
118+
}
119+
120+
return sum
121+
}

2024/day03/day03_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package day03
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 := 161
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 := 175700056
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("sample2.txt")
28+
fmt.Printf("Got: %v\n", got)
29+
want := 48
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 := 71668682
39+
if got != want {
40+
t.Errorf("got %v but want %v", got, want)
41+
}
42+
}

0 commit comments

Comments
 (0)