This commit is contained in:
2022-12-03 12:14:30 +01:00
parent e84c13c689
commit aee2b49c64
3 changed files with 111 additions and 0 deletions

57
day03/common/rucksack.go Normal file
View File

@@ -0,0 +1,57 @@
package common
import (
"bufio"
)
type Compartment struct {
items []int
}
func (compartment *Compartment) AddItem(itemName byte) {
if itemName < 'a' {
compartment.items = append(compartment.items, int(itemName - 'A' + 27))
} else {
compartment.items = append(compartment.items, int(itemName - 'a' + 1))
}
}
func (compartment Compartment) GetItems() []int {
return compartment.items
}
func (compartment Compartment) ContainsItem(item int) bool {
for _, cItem := range compartment.items {
if item == cItem {
return true
}
}
return false
}
type Rucksack struct {
compartments [2]Compartment
}
func (rucksack Rucksack) GetCompartment(id int) Compartment {
return rucksack.compartments[id-1]
}
func Parse(scanner bufio.Scanner) []Rucksack {
rucksacks := []Rucksack{}
for scanner.Scan() {
currentRucksack := Rucksack{}
line := scanner.Text()
compartmentLength := len(line) / 2
for i := 0; i < compartmentLength; i++ {
currentRucksack.compartments[0].AddItem(line[i])
}
for i := 0; i < compartmentLength; i++ {
currentRucksack.compartments[1].AddItem(line[compartmentLength+i])
}
rucksacks = append(rucksacks, currentRucksack)
}
return rucksacks
}