Skip to content

Commit 82385fd

Browse files
authored
refactor: Fix linter bugs (#40)
* refactor: Fix linter bugs * refactor: Reduce cognitive complexity
1 parent c34d5b0 commit 82385fd

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This repository contains solutions for puzzles and cli tool to run solutions to
3131
<summary>2015</summary>
3232

3333
- [x] [Day 1: Not Quite Lisp](https://adventofcode.com/2015/day/1)
34-
- [ ] [Day 2: I Was Told There Would Be No Math](https://adventofcode.com/2015/day/2)
34+
- [x] [Day 2: I Was Told There Would Be No Math](https://adventofcode.com/2015/day/2)
3535
- [ ] [Day 3: Perfectly Spherical Houses in a Vacuum](https://adventofcode.com/2015/day/3)
3636
- [ ] [Day 4: The Ideal Stocking Stuffer](https://adventofcode.com/2015/day/4)
3737
- [ ] [Day 5: Doesn't He Have Intern-Elves For This?](https://adventofcode.com/2015/day/5)

internal/puzzles/solutions/2020/day01/solution.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"github.com/obalunenko/advent-of-code/internal/puzzles"
1212
)
1313

14+
func init() {
15+
puzzles.Register(solution{})
16+
}
17+
1418
type solution struct{}
1519

1620
func (s solution) Year() string {
@@ -21,10 +25,6 @@ func (s solution) Day() string {
2125
return puzzles.Day01.String()
2226
}
2327

24-
func init() {
25-
puzzles.Register(solution{})
26-
}
27-
2828
func (s solution) Part1(input io.Reader) (string, error) {
2929
scanner := bufio.NewScanner(input)
3030

@@ -85,14 +85,22 @@ func (s solution) Part2(input io.Reader) (string, error) {
8585

8686
sort.Ints(expensereport)
8787

88+
a, b, c, err := foundEntries(expensereport)
89+
if err != nil {
90+
return "", fmt.Errorf("found entries: %w", err)
91+
}
92+
93+
res := a * b * c
94+
95+
return strconv.Itoa(res), nil
96+
}
97+
98+
func foundEntries(expensereport []int) (a, b, c int, err error) {
8899
const (
89100
dest = 2020
90101
)
91102

92-
var (
93-
a, b, c int
94-
found bool
95-
)
103+
var found bool
96104

97105
loop:
98106
for i := 0; i < len(expensereport)-2; i++ {
@@ -113,10 +121,8 @@ loop:
113121
}
114122

115123
if !found {
116-
return "", fmt.Errorf("answer not found")
124+
return 0, 0, 0, fmt.Errorf("answer not found")
117125
}
118126

119-
res := a * b * c
120-
121-
return strconv.Itoa(res), nil
127+
return a, b, c, nil
122128
}

internal/puzzles/solutions/2020/day01/spec.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# --- Day 1: Report Repair ---
2+
3+
## --- Part One ---
4+
25
After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island.
36
Surely, Christmas will go on without you.
47

@@ -33,7 +36,8 @@ Multiplying them together produces `1721 * 299 = 514579`, so the correct answer
3336
Of course, your expense report is much larger. `Find the two entries that sum to 2020;
3437
what do you get if you multiply them together`?
3538

36-
--- Part Two ---
39+
## --- Part Two ---
40+
3741
The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over
3842
from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet
3943
the same criteria.

internal/puzzles/utils/intcomputer/intcomputer.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const (
3131
shift = 4
3232
)
3333

34+
// ErrNotExist returns in case when value not found in memory.
35+
var ErrNotExist = errors.New("value not exist")
36+
3437
// New creates instance of IntComputer from passed intcode program.
3538
func New(in io.Reader) (IntComputer, error) {
3639
var c IntComputer
@@ -98,12 +101,12 @@ loop:
98101
func (c *IntComputer) add(aPos, bPos, resPos int) error {
99102
a, ok := c.memory[aPos]
100103
if !ok {
101-
return fmt.Errorf("value not exist [apos:%d]", aPos)
104+
return fmt.Errorf("apos:%d: %w", aPos, ErrNotExist)
102105
}
103106

104107
b, ok := c.memory[bPos]
105108
if !ok {
106-
return fmt.Errorf("value not exist [bpos:%d]", bPos)
109+
return fmt.Errorf("bpos:%d: %w", bPos, ErrNotExist)
107110
}
108111

109112
res := a + b
@@ -115,12 +118,12 @@ func (c *IntComputer) add(aPos, bPos, resPos int) error {
115118
func (c *IntComputer) mult(aPos, bPos, resPos int) error {
116119
a, ok := c.memory[aPos]
117120
if !ok {
118-
return errors.New("value not exist")
121+
return ErrNotExist
119122
}
120123

121124
b, ok := c.memory[bPos]
122125
if !ok {
123-
return errors.New("value not exist")
126+
return ErrNotExist
124127
}
125128

126129
res := a * b
@@ -132,7 +135,7 @@ func (c *IntComputer) mult(aPos, bPos, resPos int) error {
132135
func (c *IntComputer) abort() (int, error) {
133136
res, ok := c.memory[0]
134137
if !ok {
135-
return 0, errors.New("value not exist")
138+
return 0, ErrNotExist
136139
}
137140

138141
return res, nil
@@ -150,8 +153,9 @@ func (c *IntComputer) Input(noun, verb int) {
150153
func (c *IntComputer) Reset() {
151154
c.memory = make(map[int]int, len(c.initial))
152155

153-
for i, n := range c.initial {
154-
n := n
156+
for i := range c.initial {
157+
n := c.initial[i]
158+
155159
c.memory[i] = n
156160
}
157161
}

0 commit comments

Comments
 (0)