diff --git a/README.md b/README.md index 21989612..f76ce2f2 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ This repository contains solutions for puzzles and cli tool to run solutions to 2015 - [x] [Day 1: Not Quite Lisp](https://adventofcode.com/2015/day/1) - - [ ] [Day 2: I Was Told There Would Be No Math](https://adventofcode.com/2015/day/2) + - [x] [Day 2: I Was Told There Would Be No Math](https://adventofcode.com/2015/day/2) - [ ] [Day 3: Perfectly Spherical Houses in a Vacuum](https://adventofcode.com/2015/day/3) - [ ] [Day 4: The Ideal Stocking Stuffer](https://adventofcode.com/2015/day/4) - [ ] [Day 5: Doesn't He Have Intern-Elves For This?](https://adventofcode.com/2015/day/5) diff --git a/internal/puzzles/solutions/2020/day01/solution.go b/internal/puzzles/solutions/2020/day01/solution.go index 60da3dcc..64253c07 100644 --- a/internal/puzzles/solutions/2020/day01/solution.go +++ b/internal/puzzles/solutions/2020/day01/solution.go @@ -11,6 +11,10 @@ import ( "github.com/obalunenko/advent-of-code/internal/puzzles" ) +func init() { + puzzles.Register(solution{}) +} + type solution struct{} func (s solution) Year() string { @@ -21,10 +25,6 @@ func (s solution) Day() string { return puzzles.Day01.String() } -func init() { - puzzles.Register(solution{}) -} - func (s solution) Part1(input io.Reader) (string, error) { scanner := bufio.NewScanner(input) @@ -85,14 +85,22 @@ func (s solution) Part2(input io.Reader) (string, error) { sort.Ints(expensereport) + a, b, c, err := foundEntries(expensereport) + if err != nil { + return "", fmt.Errorf("found entries: %w", err) + } + + res := a * b * c + + return strconv.Itoa(res), nil +} + +func foundEntries(expensereport []int) (a, b, c int, err error) { const ( dest = 2020 ) - var ( - a, b, c int - found bool - ) + var found bool loop: for i := 0; i < len(expensereport)-2; i++ { @@ -113,10 +121,8 @@ loop: } if !found { - return "", fmt.Errorf("answer not found") + return 0, 0, 0, fmt.Errorf("answer not found") } - res := a * b * c - - return strconv.Itoa(res), nil + return a, b, c, nil } diff --git a/internal/puzzles/solutions/2020/day01/spec.md b/internal/puzzles/solutions/2020/day01/spec.md index 57e43e1f..dae4cdf9 100644 --- a/internal/puzzles/solutions/2020/day01/spec.md +++ b/internal/puzzles/solutions/2020/day01/spec.md @@ -1,4 +1,7 @@ # --- Day 1: Report Repair --- + +## --- Part One --- + After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you. @@ -33,7 +36,8 @@ Multiplying them together produces `1721 * 299 = 514579`, so the correct answer Of course, your expense report is much larger. `Find the two entries that sum to 2020; what do you get if you multiply them together`? ---- Part Two --- +## --- Part Two --- + The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet the same criteria. diff --git a/internal/puzzles/utils/intcomputer/intcomputer.go b/internal/puzzles/utils/intcomputer/intcomputer.go index 66e53487..45ffa283 100644 --- a/internal/puzzles/utils/intcomputer/intcomputer.go +++ b/internal/puzzles/utils/intcomputer/intcomputer.go @@ -31,6 +31,9 @@ const ( shift = 4 ) +// ErrNotExist returns in case when value not found in memory. +var ErrNotExist = errors.New("value not exist") + // New creates instance of IntComputer from passed intcode program. func New(in io.Reader) (IntComputer, error) { var c IntComputer @@ -98,12 +101,12 @@ loop: func (c *IntComputer) add(aPos, bPos, resPos int) error { a, ok := c.memory[aPos] if !ok { - return fmt.Errorf("value not exist [apos:%d]", aPos) + return fmt.Errorf("apos:%d: %w", aPos, ErrNotExist) } b, ok := c.memory[bPos] if !ok { - return fmt.Errorf("value not exist [bpos:%d]", bPos) + return fmt.Errorf("bpos:%d: %w", bPos, ErrNotExist) } res := a + b @@ -115,12 +118,12 @@ func (c *IntComputer) add(aPos, bPos, resPos int) error { func (c *IntComputer) mult(aPos, bPos, resPos int) error { a, ok := c.memory[aPos] if !ok { - return errors.New("value not exist") + return ErrNotExist } b, ok := c.memory[bPos] if !ok { - return errors.New("value not exist") + return ErrNotExist } res := a * b @@ -132,7 +135,7 @@ func (c *IntComputer) mult(aPos, bPos, resPos int) error { func (c *IntComputer) abort() (int, error) { res, ok := c.memory[0] if !ok { - return 0, errors.New("value not exist") + return 0, ErrNotExist } return res, nil @@ -150,8 +153,9 @@ func (c *IntComputer) Input(noun, verb int) { func (c *IntComputer) Reset() { c.memory = make(map[int]int, len(c.initial)) - for i, n := range c.initial { - n := n + for i := range c.initial { + n := c.initial[i] + c.memory[i] = n } }