Day 1: Part 2

This commit is contained in:
James Griffin
2020-12-02 12:12:46 -04:00
parent d7f1ba55ed
commit 1225256007
3 changed files with 66 additions and 4 deletions

View File

@@ -8,4 +8,5 @@ import (
func main() { func main() {
fmt.Println(one.PartOne()) fmt.Println(one.PartOne())
fmt.Println(one.PartTwo())
} }

View File

@@ -30,7 +30,7 @@ func (e *expenseReport) load(filename string) error {
return nil return nil
} }
func (e *expenseReport) searchAndMultiply(target int) (int, error) { func (e *expenseReport) findPairAndMultiply(target int) (int, error) {
for i := 0; i < len(e.expenses); i++ { for i := 0; i < len(e.expenses); i++ {
if e.expenses[i] > target { if e.expenses[i] > target {
continue continue
@@ -50,6 +50,32 @@ func (e *expenseReport) searchAndMultiply(target int) (int, error) {
return -1, fmt.Errorf("Unable to find target pair that sums to %d", target) return -1, fmt.Errorf("Unable to find target pair that sums to %d", target)
} }
func (e *expenseReport) findTrioAndMultiply(target int) (int, error) {
for i := 0; i < len(e.expenses); i++ {
if e.expenses[i] > target {
continue
}
for j := i + 1; j < len(e.expenses); j++ {
if e.expenses[j] > target {
continue
}
for k := j + 1; k < len(e.expenses); k++ {
if e.expenses[k] > target {
continue
}
if (e.expenses[i] + e.expenses[j] + e.expenses[k]) == target {
return e.expenses[i] * e.expenses[j] * e.expenses[k], nil
}
}
}
}
return -1, fmt.Errorf("Unable to find target pair that sums to %d", target)
}
// PartOne Find the multiplied value of the two expenses totalling 2020 // PartOne Find the multiplied value of the two expenses totalling 2020
func PartOne() string { func PartOne() string {
report := expenseReport{} report := expenseReport{}
@@ -58,10 +84,26 @@ func PartOne() string {
return fmt.Sprintf("Unable to load expenses: %s", err.Error()) return fmt.Sprintf("Unable to load expenses: %s", err.Error())
} }
result, err := report.searchAndMultiply(2020) result, err := report.findPairAndMultiply(2020)
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
return fmt.Sprintf("The output of the expenses summing to 2020 is %d", result) return fmt.Sprintf("The output of the expenses summing to 2020 is %d", result)
} }
func PartTwo() string {
report := expenseReport{}
err := report.load("one/expenses.txt")
if err != nil {
return fmt.Sprintf("Unable to load expenses: %s", err.Error())
}
result, err := report.findTrioAndMultiply(2020)
if err != nil {
return err.Error()
}
return fmt.Sprintf("The output of the expenses summing to 2020 is %d", result)
}

View File

@@ -15,7 +15,7 @@ func Test_expenseReport_load(t *testing.T) {
} }
} }
func Test_expenseReport_searchAndMultiply(t *testing.T) { func Test_expenseReport_findPairAndMultiply(t *testing.T) {
report := expenseReport{ report := expenseReport{
expenses: []int{ expenses: []int{
1721, 1721,
@@ -27,9 +27,28 @@ func Test_expenseReport_searchAndMultiply(t *testing.T) {
}, },
} }
result, _ := report.searchAndMultiply(2020) result, _ := report.findPairAndMultiply(2020)
if result != 514579 { if result != 514579 {
t.Logf("Expected 514579, Got %d", result) t.Logf("Expected 514579, Got %d", result)
t.FailNow() t.FailNow()
} }
} }
func Test_expenseReport_findTrioAndMultiply(t *testing.T) {
report := expenseReport{
expenses: []int{
1721,
979,
366,
299,
675,
1456,
},
}
result, _ := report.findTrioAndMultiply(2020)
if result != 241861950 {
t.Logf("Expected 241861950, Got %d", result)
t.FailNow()
}
}