Skip to content

Commit

Permalink
refactor(puzzles): 2018/day02 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko committed Nov 4, 2021
1 parent f782dd8 commit ecea98f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 24 deletions.
58 changes: 34 additions & 24 deletions internal/puzzles/solutions/2018/day01/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,16 @@ func part1(in io.Reader) (string, error) {
for scanner.Scan() {
line := scanner.Text()

matches := re.FindStringSubmatch(line)

if len(matches) != totalmatches {
return "", fmt.Errorf("wrong matches[%d] for line[%s], should be [%d]",
len(matches), line, totalmatches)
}

d, err := strconv.Atoi(matches[digits])
delta, err := getFreqDelta(line)
if err != nil {
return "", fmt.Errorf("strconv atoi: %w", err)
return "", err
}

switch matches[sign] {
switch delta.sign {
case "+":
curfreq += d
curfreq += delta.d
case "-":
curfreq -= d
curfreq -= delta.d
}
}

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

matches := re.FindStringSubmatch(line)

if len(matches) != totalmatches {
return "", fmt.Errorf("wrong matches[%d] for line[%s], should be [%d]",
len(matches), line, totalmatches)
}

d, err := strconv.Atoi(matches[digits])
delta, err := getFreqDelta(line)
if err != nil {
return "", fmt.Errorf("strconv atoi: %w", err)
return "", err
}

switch matches[sign] {
switch delta.sign {
case "+":
curfreq += d
curfreq += delta.d
case "-":
curfreq -= d
curfreq -= delta.d
}

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

return strconv.Itoa(curfreq), nil
}

type freqDelta struct {
sign string
d int
}

func getFreqDelta(line string) (freqDelta, error) {
matches := re.FindStringSubmatch(line)

if len(matches) != totalmatches {
return freqDelta{}, fmt.Errorf("wrong matches[%d] for line[%s], should be [%d]",
len(matches), line, totalmatches)
}

d, err := strconv.Atoi(matches[digits])
if err != nil {
return freqDelta{}, fmt.Errorf("strconv atoi: %w", err)
}

return freqDelta{
sign: matches[sign],
d: d,
}, nil
}
48 changes: 48 additions & 0 deletions internal/puzzles/solutions/2018/day01/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,51 @@ func Test_solution_Part2(t *testing.T) {
})
}
}

func Test_getFreqDelta(t *testing.T) {
type args struct {
line string
}
tests := []struct {
name string
args args
want freqDelta
wantErr bool
}{
{
name: "",
args: args{
line: "+2",
},
want: freqDelta{
sign: "+",
d: 2,
},
wantErr: false,
},
{
name: "",
args: args{
line: "2",
},
want: freqDelta{
sign: "",
d: 0,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getFreqDelta(tt.args.line)
if tt.wantErr {
assert.Error(t, err)

return
}

assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit ecea98f

Please sign in to comment.