58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
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
|
|
}
|