Skip to content

Commit 32abccc

Browse files
committed
2022 Day 03
1 parent cfe3952 commit 32abccc

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

2022/day-03/main.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ func main() {
1212

1313
func processInput(input []string) int32 {
1414
var answer int32
15+
var groupOfThree [3]string
16+
groupIndex := 0
1517
for _, v := range input {
16-
parts := splitStringIntoTwo(v)
17-
commonLetter := findCommonLetter(parts[0], parts[1])
18-
19-
numberOfLetter := convertLetterToNumber(commonLetter)
20-
answer = answer + numberOfLetter
18+
// get 3 lines
19+
groupOfThree[groupIndex] = v
20+
if groupIndex == 2 {
21+
commonLetter := findCommonLetter(groupOfThree[0], groupOfThree[1], groupOfThree[2])
22+
log.Printf("commonLetter: %v(%v)", commonLetter, string(commonLetter))
23+
numberOfLetter := convertLetterToNumber(commonLetter)
24+
answer = answer + numberOfLetter
25+
groupIndex = 0
26+
}
27+
groupIndex = groupIndex + 1
2128
}
2229
return answer
2330
}
@@ -34,14 +41,19 @@ func convertLetterToNumber(letter rune) (convertedLetter int32) {
3441
return -1
3542
}
3643

37-
func findCommonLetter(string1, string2 string) (common rune) {
44+
func findCommonLetter(string1, string2, string3 string) (common rune) {
3845
for _, l1 := range string1 {
3946
for _, l2 := range string2 {
40-
if l1 == l2 {
41-
return l1
47+
for _, l3 := range string3 {
48+
if l1 == l2 && l2 == l3 {
49+
log.Printf("l1: %v(%s), l2: %v(%s), l3: %v(%s)", l1, string(l1), l2, string(l2), l3, string(l3))
50+
//log.Println("found it:", l1, l2, l3)
51+
return l1
52+
}
4253
}
4354
}
4455
}
56+
log.Fatalln("no common letters found:", common)
4557
return common
4658
}
4759

2022/day-03/main_test.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (this *Main) TestInputCanBeProcessed() {
4444
input := loadInput("input_test")
4545
this.So(input, should.HaveSameTypeAs, []string{""})
4646
answer := processInput(input)
47-
this.So(answer, should.Equal, 157)
47+
this.So(answer, should.Equal, 70)
4848
}
4949

5050
func (this *Main) TestSplitStringIntoTwoEqualParts() {
@@ -54,30 +54,16 @@ func (this *Main) TestSplitStringIntoTwoEqualParts() {
5454
}
5555

5656
func (this *Main) TestFindCommonLetter() {
57-
string1 := "vJrwpWtwJgWr"
58-
string2 := "hcsFMMfFFhFp"
59-
common := findCommonLetter(string1, string2)
60-
this.So(common, should.Equal, 'p')
61-
string1 = "jqHRNqRjqzjGDLGL"
62-
string2 = "rsFMfFZSrLrFZsSL"
63-
common = findCommonLetter(string1, string2)
64-
this.So(common, should.Equal, 'L')
65-
string1 = "PmmdzqPrV"
66-
string2 = "vPwwTWBwg"
67-
common = findCommonLetter(string1, string2)
68-
this.So(common, should.Equal, 'P')
69-
string1 = "wMqvLMZHhHMvwLH"
70-
string2 = "jbvcjnnSBnvTQFn"
71-
common = findCommonLetter(string1, string2)
72-
this.So(common, should.Equal, 'v')
73-
string1 = "ttgJtRGJ"
74-
string2 = "QctTZtZT"
75-
common = findCommonLetter(string1, string2)
76-
this.So(common, should.Equal, 't')
77-
string1 = "CrZsJsPPZsGz"
78-
string2 = "wwsLwLmpwMDw"
79-
common = findCommonLetter(string1, string2)
80-
this.So(common, should.Equal, 's')
57+
string1 := "vJrwpWtwJgWrhcsFMMfFFhFp"
58+
string2 := "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL"
59+
string3 := "PmmdzqPrVvPwwTWBwg"
60+
common := findCommonLetter(string1, string2, string3)
61+
this.So(common, should.Equal, 'r')
62+
string1 = "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn"
63+
string2 = "ttgJtRGJQctTZtZT"
64+
string3 = "CrZsJsPPZsGzwwsLwLmpwMDw"
65+
common = findCommonLetter(string1, string2, string3)
66+
this.So(common, should.Equal, 'Z')
8167
}
8268

8369
func (this *Main) TestMatchLettersToNumbers() {

0 commit comments

Comments
 (0)