Skip to content

Commit

Permalink
refactor: Fix linter bugs (#40)
Browse files Browse the repository at this point in the history
* refactor: Fix linter bugs

* refactor: Reduce cognitive complexity
  • Loading branch information
obalunenko committed Nov 8, 2021
1 parent c34d5b0 commit 82385fd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This repository contains solutions for puzzles and cli tool to run solutions to
<summary>2015</summary>

- [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)
Expand Down
30 changes: 18 additions & 12 deletions internal/puzzles/solutions/2020/day01/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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++ {
Expand All @@ -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
}
6 changes: 5 additions & 1 deletion internal/puzzles/solutions/2020/day01/spec.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions internal/puzzles/utils/intcomputer/intcomputer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
}

0 comments on commit 82385fd

Please sign in to comment.