Skip to content

Commit ecea98f

Browse files
committed
refactor(puzzles): 2018/day02 refactor
1 parent f782dd8 commit ecea98f

File tree

2 files changed

+82
-24
lines changed

2 files changed

+82
-24
lines changed

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

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,16 @@ func part1(in io.Reader) (string, error) {
6464
for scanner.Scan() {
6565
line := scanner.Text()
6666

67-
matches := re.FindStringSubmatch(line)
68-
69-
if len(matches) != totalmatches {
70-
return "", fmt.Errorf("wrong matches[%d] for line[%s], should be [%d]",
71-
len(matches), line, totalmatches)
72-
}
73-
74-
d, err := strconv.Atoi(matches[digits])
67+
delta, err := getFreqDelta(line)
7568
if err != nil {
76-
return "", fmt.Errorf("strconv atoi: %w", err)
69+
return "", err
7770
}
7871

79-
switch matches[sign] {
72+
switch delta.sign {
8073
case "+":
81-
curfreq += d
74+
curfreq += delta.d
8275
case "-":
83-
curfreq -= d
76+
curfreq -= delta.d
8477
}
8578
}
8679

@@ -118,23 +111,16 @@ func part2(in io.Reader) (string, error) {
118111
for scanner.Scan() {
119112
line := scanner.Text()
120113

121-
matches := re.FindStringSubmatch(line)
122-
123-
if len(matches) != totalmatches {
124-
return "", fmt.Errorf("wrong matches[%d] for line[%s], should be [%d]",
125-
len(matches), line, totalmatches)
126-
}
127-
128-
d, err := strconv.Atoi(matches[digits])
114+
delta, err := getFreqDelta(line)
129115
if err != nil {
130-
return "", fmt.Errorf("strconv atoi: %w", err)
116+
return "", err
131117
}
132118

133-
switch matches[sign] {
119+
switch delta.sign {
134120
case "+":
135-
curfreq += d
121+
curfreq += delta.d
136122
case "-":
137-
curfreq -= d
123+
curfreq -= delta.d
138124
}
139125

140126
if seenfreqs[curfreq] {
@@ -155,3 +141,27 @@ func part2(in io.Reader) (string, error) {
155141

156142
return strconv.Itoa(curfreq), nil
157143
}
144+
145+
type freqDelta struct {
146+
sign string
147+
d int
148+
}
149+
150+
func getFreqDelta(line string) (freqDelta, error) {
151+
matches := re.FindStringSubmatch(line)
152+
153+
if len(matches) != totalmatches {
154+
return freqDelta{}, fmt.Errorf("wrong matches[%d] for line[%s], should be [%d]",
155+
len(matches), line, totalmatches)
156+
}
157+
158+
d, err := strconv.Atoi(matches[digits])
159+
if err != nil {
160+
return freqDelta{}, fmt.Errorf("strconv atoi: %w", err)
161+
}
162+
163+
return freqDelta{
164+
sign: matches[sign],
165+
d: d,
166+
}, nil
167+
}

internal/puzzles/solutions/2018/day01/solution_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,51 @@ func Test_solution_Part2(t *testing.T) {
196196
})
197197
}
198198
}
199+
200+
func Test_getFreqDelta(t *testing.T) {
201+
type args struct {
202+
line string
203+
}
204+
tests := []struct {
205+
name string
206+
args args
207+
want freqDelta
208+
wantErr bool
209+
}{
210+
{
211+
name: "",
212+
args: args{
213+
line: "+2",
214+
},
215+
want: freqDelta{
216+
sign: "+",
217+
d: 2,
218+
},
219+
wantErr: false,
220+
},
221+
{
222+
name: "",
223+
args: args{
224+
line: "2",
225+
},
226+
want: freqDelta{
227+
sign: "",
228+
d: 0,
229+
},
230+
wantErr: true,
231+
},
232+
}
233+
for _, tt := range tests {
234+
t.Run(tt.name, func(t *testing.T) {
235+
got, err := getFreqDelta(tt.args.line)
236+
if tt.wantErr {
237+
assert.Error(t, err)
238+
239+
return
240+
}
241+
242+
assert.NoError(t, err)
243+
assert.Equal(t, tt.want, got)
244+
})
245+
}
246+
}

0 commit comments

Comments
 (0)