Skip to content

Commit

Permalink
Fixed 2015.13 tests + comments
Browse files Browse the repository at this point in the history
  • Loading branch information
janreggie committed Nov 22, 2020
1 parent eb260f7 commit 71020f7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
35 changes: 18 additions & 17 deletions aoc2015/day13.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (happiness happiness) int() int {
// visitor is represented by a string
type visitor string

func (visitor visitor) string() string {
func (visitor visitor) String() string {
return string(visitor)
}

Expand Down Expand Up @@ -59,6 +59,7 @@ func (pair visitorPair) String() string {
}

// tableScenario is a representation of a "scenario"
// which is represented by a list of visitorPairs and their respective happiness gains.
type tableScenario struct {
// potentialHappiness is the potential happiness gain of a visitor pair
potentialHappiness map[visitorPair]happiness
Expand All @@ -67,11 +68,14 @@ type tableScenario struct {
visitors []visitor
}

// newTableScenario creates a table scenario from a scanner object
func newTableScenario(scanner *bufio.Scanner) (tableScenario, error) {
// newTableScenario creates a table scenario from a scanner object.
// It uses parseTableScenario to read text such as:
//
// Alice would lose 2 happiness units by sitting next to David.
func newTableScenario(scanner *bufio.Scanner) (*tableScenario, error) {
potentialHappiness := make(map[visitorPair]happiness)
visitors := make([]visitor, 0)
result := tableScenario{potentialHappiness: potentialHappiness, visitors: visitors}
result := &tableScenario{potentialHappiness: potentialHappiness, visitors: visitors}

// now parse each line
for scanner.Scan() {
Expand All @@ -85,7 +89,7 @@ func newTableScenario(scanner *bufio.Scanner) (tableScenario, error) {
return result, nil
}

func (scenario tableScenario) String() string {
func (scenario *tableScenario) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("Visitors: %v\n", scenario.visitors))
sb.WriteString("Happiness:\n")
Expand All @@ -96,7 +100,7 @@ func (scenario tableScenario) String() string {
return sb.String()
}

func (scenario tableScenario) copy() tableScenario {
func (scenario *tableScenario) copy() tableScenario {
result := tableScenario{}
result.potentialHappiness = make(map[visitorPair]happiness)
for kk, vv := range scenario.potentialHappiness {
Expand All @@ -108,7 +112,7 @@ func (scenario tableScenario) copy() tableScenario {
}

// parseTableScenario parses each line of Day 13's input
// and modifies the scenario accordingly.
// and modifies the scenario in-place accordingly.
// A line should look like the following two:
//
// Alice would gain 26 happiness units by sitting next to Carol.
Expand Down Expand Up @@ -170,9 +174,6 @@ func (scenario *tableScenario) addToVisitors(pair visitorPair) {
}

// influence adds happiness to the happiness that visitorPair would have.
// For instance, calling influence(A+B, 3) and influence(A+B, -2)
// would make the pair A and B give off a net happiness of 1.
// A need not be lexographically less than B.
// Will give off an error if pair refers to only one person
// or if one of pair is an empty string.
func (scenario *tableScenario) influence(pair visitorPair, h happiness) error {
Expand All @@ -191,7 +192,7 @@ func (scenario *tableScenario) influence(pair visitorPair, h happiness) error {

// permutations creates a channel that contains all the possible circular seating arrangements
// and will close said channel.
func (scenario tableScenario) permutations() <-chan []visitor {
func (scenario *tableScenario) permutations() <-chan []visitor {
permutate := func(c chan []visitor, inputs []visitor) {
output := make([]visitor, len(inputs))
copy(output, inputs)
Expand Down Expand Up @@ -235,7 +236,7 @@ func (scenario tableScenario) permutations() <-chan []visitor {
// or if the visitors are equal to each other.
// If the happiness between these visitors does not exist return zero
// as if they are neutral towards each other.
func (scenario tableScenario) get(a, b visitor) (happiness, error) {
func (scenario *tableScenario) get(a, b visitor) (happiness, error) {
if a == b {
return 0, fmt.Errorf("could not evaluate happiness of %v and themselves", a)
}
Expand Down Expand Up @@ -267,12 +268,12 @@ func (scenario tableScenario) get(a, b visitor) (happiness, error) {

// happiestFrom determines the happiest seating arrangement starting from some person.
// This uses the concept of seatingArrangements and a seatingArrangementQueue to record all arrangements.
func (scenario tableScenario) happiestFrom(visitor visitor) happiness {
func (scenario *tableScenario) happiestFrom(visitor visitor) happiness {
queue := newSeatingArrangementQueue()

// check all visitors in the list
for _, vv := range scenario.visitors {
arrangement, err := newSeatingArrangement(visitor, vv, &scenario)
arrangement, err := newSeatingArrangement(visitor, vv, scenario)
if err != nil {
continue // maybe same person
}
Expand Down Expand Up @@ -306,13 +307,13 @@ func (scenario tableScenario) happiestFrom(visitor visitor) happiness {
// by exhausting all visitors in scenario.
// This simply gets the happiest arrangement from the first visitor in the scenario
// as the list is circular and it doesn't really matter who the first visitor is.
func (scenario tableScenario) happiestExhaustive() happiness {
func (scenario *tableScenario) happiestExhaustive() happiness {
return scenario.happiestFrom(scenario.visitors[0])
}

// happiestPermutative determines the happiest arrangement
// by checking all possible circular seating arrangements.
func (scenario tableScenario) happiestPermutative() happiness {
func (scenario *tableScenario) happiestPermutative() happiness {
arrangements := scenario.permutations()
var record happiness

Expand Down Expand Up @@ -359,7 +360,7 @@ func (arrangement seatingArrangement) String() string {
var sb strings.Builder
sb.WriteString("Visitors: ")
for _, vv := range arrangement.raw {
sb.WriteString(vv.string())
sb.WriteString(vv.String())
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("; Happiness: %v", arrangement.happiness))
Expand Down
58 changes: 29 additions & 29 deletions aoc2015/day13_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,27 @@ Mallory would lose 73 happiness units by sitting next to Frank.
Mallory would lose 89 happiness units by sitting next to George.
`

const day13sampleInput = `"Alice would gain 54 happiness units by sitting next to Bob.
"Alice would lose 79 happiness units by sitting next to Carol.
"Alice would lose 2 happiness units by sitting next to David.
"Bob would gain 83 happiness units by sitting next to Alice.
"Bob would lose 7 happiness units by sitting next to Carol.
"Bob would lose 63 happiness units by sitting next to David.
"Carol would lose 62 happiness units by sitting next to Alice.
"Carol would gain 60 happiness units by sitting next to Bob.
"Carol would gain 55 happiness units by sitting next to David.
"David would gain 46 happiness units by sitting next to Alice.
"David would lose 7 happiness units by sitting next to Bob.
"David would gain 41 happiness units by sitting next to Carol.
const day13sampleInput = `Alice would gain 54 happiness units by sitting next to Bob.
Alice would lose 79 happiness units by sitting next to Carol.
Alice would lose 2 happiness units by sitting next to David.
Bob would gain 83 happiness units by sitting next to Alice.
Bob would lose 7 happiness units by sitting next to Carol.
Bob would lose 63 happiness units by sitting next to David.
Carol would lose 62 happiness units by sitting next to Alice.
Carol would gain 60 happiness units by sitting next to Bob.
Carol would gain 55 happiness units by sitting next to David.
David would gain 46 happiness units by sitting next to Alice.
David would lose 7 happiness units by sitting next to Bob.
David would gain 41 happiness units by sitting next to Carol.
`

func day13sampleScenario() tableScenario {
func day13sampleScenario() *tableScenario {
// guaranteed not to error
scenario, _ := newTableScenario(bufio.NewScanner(strings.NewReader(day13sampleInput)))
return scenario
}

func day13myScenario() tableScenario {
func day13myScenario() *tableScenario {
scenario, _ := newTableScenario(bufio.NewScanner(strings.NewReader(day13myInput)))
return scenario
}
Expand Down Expand Up @@ -130,10 +130,10 @@ func Test_tableScenario_happiestPermutative(t *testing.T) {
func Test_seatingArrangement(t *testing.T) {
assert := assert.New(t)
scenario := day13myScenario()
arrangement, err := newSeatingArrangement("Alice", "Bob", &scenario)
arrangement, err := newSeatingArrangement("Alice", "Bob", scenario)
assert.NoError(err)
assert.Equal(seatingArrangement{
basis: &scenario,
basis: scenario,
raw: []visitor{"Alice", "Bob"},
remaining: []visitor{"Carol", "David", "Eric", "Frank", "George", "Mallory"},
happiness: 42,
Expand All @@ -142,7 +142,7 @@ func Test_seatingArrangement(t *testing.T) {
arrangement, err = arrangement.add("David")
assert.NoError(err)
assert.Equal(seatingArrangement{
basis: &scenario,
basis: scenario,
raw: []visitor{"Alice", "Bob", "David"},
remaining: []visitor{"Carol", "Eric", "Frank", "George", "Mallory"},
happiness: 9,
Expand All @@ -156,7 +156,7 @@ func Test_seatingArrangement(t *testing.T) {
arrangement, err = arrangement.add("Mallory")
assert.NoError(err)
assert.Equal(seatingArrangement{
basis: &scenario,
basis: scenario,
raw: []visitor{"Alice", "Bob", "David", "Mallory"},
remaining: []visitor{"Carol", "Eric", "Frank", "George"},
happiness: -30,
Expand All @@ -168,7 +168,7 @@ func Test_seatingArrangement(t *testing.T) {
arrangement, err = arrangement.add("George")
assert.NoError(err)
assert.Equal(seatingArrangement{
basis: &scenario,
basis: scenario,
raw: []visitor{"Alice", "Bob", "David", "Mallory", "George"},
remaining: []visitor{"Carol", "Eric", "Frank"},
happiness: -22,
Expand All @@ -177,7 +177,7 @@ func Test_seatingArrangement(t *testing.T) {
arrangement, err = arrangement.add("Eric")
assert.NoError(err)
assert.Equal(seatingArrangement{
basis: &scenario,
basis: scenario,
raw: []visitor{"Alice", "Bob", "David", "Mallory", "George", "Eric"},
remaining: []visitor{"Carol", "Frank"},
happiness: -59,
Expand All @@ -197,7 +197,7 @@ func Test_seatingArrangement(t *testing.T) {
arrangement, err = arrangement.add("Carol")
assert.NoError(err)
assert.Equal(seatingArrangement{
basis: &scenario,
basis: scenario,
raw: []visitor{"Alice", "Bob", "David", "Mallory", "George", "Eric", "Carol"},
remaining: []visitor{"Frank"},
happiness: 136,
Expand All @@ -206,7 +206,7 @@ func Test_seatingArrangement(t *testing.T) {
arrangement, err = arrangement.add("Frank")
assert.NoError(err)
assert.Equal(seatingArrangement{
basis: &scenario,
basis: scenario,
raw: []visitor{"Alice", "Bob", "David", "Mallory", "George", "Eric", "Carol", "Frank"},
remaining: []visitor{},
happiness: 228,
Expand All @@ -221,21 +221,21 @@ func Test_seatingArrangementQueue(t *testing.T) {

// suppose we start from Alice.
// should not error!
arrangement, err := newSeatingArrangement("Alice", "Alice", &scenario)
arrangement, err := newSeatingArrangement("Alice", "Alice", scenario)
assert.Error(err)
arrangement, err = newSeatingArrangement("Alice", "Bob", &scenario)
arrangement, err = newSeatingArrangement("Alice", "Bob", scenario)
assert.NoError(err)
queue.push(arrangement)
arrangement, err = newSeatingArrangement("Alice", "Carol", &scenario)
arrangement, err = newSeatingArrangement("Alice", "Carol", scenario)
assert.NoError(err)
queue.push(arrangement)
arrangement, err = newSeatingArrangement("Alice", "David", &scenario)
arrangement, err = newSeatingArrangement("Alice", "David", scenario)
assert.NoError(err)
queue.push(arrangement)
assert.ElementsMatch([]seatingArrangement{
{&scenario, []visitor{"Alice", "Carol"}, []visitor{"Bob", "David"}, -141},
{&scenario, []visitor{"Alice", "David"}, []visitor{"Bob", "Carol"}, 44},
{&scenario, []visitor{"Alice", "Bob"}, []visitor{"Carol", "David"}, 137},
{scenario, []visitor{"Alice", "Carol"}, []visitor{"Bob", "David"}, -141},
{scenario, []visitor{"Alice", "David"}, []visitor{"Bob", "Carol"}, 44},
{scenario, []visitor{"Alice", "Bob"}, []visitor{"Carol", "David"}, 137},
}, queue)
}

Expand Down

0 comments on commit 71020f7

Please sign in to comment.