Skip to content

Commit a4cb2cd

Browse files
committed
3rd day
1 parent 68915a4 commit a4cb2cd

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

day03/day03.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
. "majcn.si/advent-of-code-2022/util"
8+
)
9+
10+
type DataType [][]byte
11+
12+
func parseData(data string) DataType {
13+
dataSplit := strings.Split(data, "\n")
14+
15+
result := make([][]byte, len(dataSplit))
16+
for i, line := range dataSplit {
17+
result[i] = []byte(line)
18+
}
19+
20+
return result
21+
}
22+
23+
func priority(s Set[byte]) int {
24+
for el := range s {
25+
if 'a' <= el && el <= 'z' {
26+
return int(el) - 96
27+
} else {
28+
return int(el) - 38
29+
}
30+
}
31+
32+
return 0
33+
}
34+
35+
func solvePart1(data DataType) (rc int) {
36+
for _, line := range data {
37+
part1Set := NewSet(line[0 : len(line)/2])
38+
part2Set := NewSet(line[len(line)/2:])
39+
intersection := part1Set.Intersection(&part2Set)
40+
rc += priority(intersection)
41+
}
42+
43+
return
44+
}
45+
46+
func solvePart2(data DataType) (rc int) {
47+
for i := 0; i < len(data); i += 3 {
48+
part1Set := NewSet(data[i])
49+
part2Set := NewSet(data[i+1])
50+
part3Set := NewSet(data[i+2])
51+
intersection := part1Set.Intersection(&part2Set)
52+
intersection = intersection.Intersection(&part3Set)
53+
rc += priority(intersection)
54+
}
55+
56+
return
57+
}
58+
59+
func main() {
60+
data := parseData(FetchInputData(3))
61+
fmt.Println(solvePart1(data))
62+
fmt.Println(solvePart2(data))
63+
}

day03/day03_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
. "majcn.si/advent-of-code-2022/util"
8+
)
9+
10+
func ExpectedPart1() int {
11+
return 157
12+
}
13+
14+
func ExpectedPart2() int {
15+
return 70
16+
}
17+
18+
var data DataType
19+
20+
func init() {
21+
input, _ := os.ReadFile("../examples/03.txt")
22+
data = parseData(string(input))
23+
}
24+
25+
func TestPart1(t *testing.T) {
26+
AssertTestPartX(t, ExpectedPart1(), solvePart1(data))
27+
}
28+
29+
func TestPart2(t *testing.T) {
30+
AssertTestPartX(t, ExpectedPart2(), solvePart2(data))
31+
}

examples/03.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vJrwpWtwJgWrhcsFMMfFFhFp
2+
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
3+
PmmdzqPrVvPwwTWBwg
4+
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
5+
ttgJtRGJQctTZtZT
6+
CrZsJsPPZsGzwwsLwLmpwMDw

0 commit comments

Comments
 (0)