diff --git a/main.go b/main.go index be8ef8f..20c111f 100644 --- a/main.go +++ b/main.go @@ -8,4 +8,5 @@ import ( func main() { fmt.Println(one.PartOne()) + fmt.Println(one.PartTwo()) } diff --git a/one/day_one.go b/one/day_one.go index e9bede9..2cf1015 100644 --- a/one/day_one.go +++ b/one/day_one.go @@ -30,7 +30,7 @@ func (e *expenseReport) load(filename string) error { 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++ { if e.expenses[i] > target { 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) } +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 func PartOne() string { report := expenseReport{} @@ -58,10 +84,26 @@ func PartOne() string { return fmt.Sprintf("Unable to load expenses: %s", err.Error()) } - result, err := report.searchAndMultiply(2020) + result, err := report.findPairAndMultiply(2020) if err != nil { return err.Error() } 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) + +} diff --git a/one/day_one_test.go b/one/day_one_test.go index 35e29f5..ff5a304 100644 --- a/one/day_one_test.go +++ b/one/day_one_test.go @@ -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{ expenses: []int{ 1721, @@ -27,9 +27,28 @@ func Test_expenseReport_searchAndMultiply(t *testing.T) { }, } - result, _ := report.searchAndMultiply(2020) + result, _ := report.findPairAndMultiply(2020) if result != 514579 { t.Logf("Expected 514579, Got %d", result) 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() + } +}