Skip to content

Commit ffdd8b2

Browse files
authored
Implement 2021/day04 puzzle solution (#62)
1 parent 5568d09 commit ffdd8b2

File tree

9 files changed

+908
-15
lines changed

9 files changed

+908
-15
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Package utils provide common used functionality to work with files, readers and so on.
2+
package utils
3+
4+
import (
5+
"io"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
// ReaderFromFile reads file from fpath and returns content as io.Reader.
14+
// File descriptor will be closed on tests teardown.
15+
func ReaderFromFile(tb testing.TB, fpath string) io.Reader {
16+
tb.Helper()
17+
18+
file, err := os.Open(filepath.Clean(fpath))
19+
require.NoError(tb, err)
20+
21+
tb.Cleanup(func() {
22+
require.NoError(tb, file.Close())
23+
})
24+
25+
return file
26+
}

internal/puzzles/input/content.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ func Get(ctx context.Context, d Date, session string) ([]byte, error) {
4141

4242
client := http.DefaultClient
4343

44+
const (
45+
timeoutSecs = 5
46+
)
47+
48+
ctx, cancel := context.WithTimeout(ctx, time.Second*timeoutSecs)
49+
defer cancel()
50+
51+
req = req.Clone(ctx)
52+
4453
resp, err := client.Do(req)
4554
if err != nil {
4655
return nil, fmt.Errorf("send request: %w", err)
@@ -105,5 +114,8 @@ func createInputReq(ctx context.Context, d Date, sessionID string) (*http.Reques
105114
Unparsed: nil,
106115
})
107116

117+
req.Header.Set("User-Agent",
118+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36")
119+
108120
return req, nil
109121
}

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package day01
22

33
import (
4-
"io"
5-
"os"
64
"path/filepath"
75
"testing"
86

97
"github.com/stretchr/testify/assert"
10-
"github.com/stretchr/testify/require"
8+
9+
"github.com/obalunenko/advent-of-code/internal/puzzles/common/utils"
1110
)
1211

1312
func Test_solution_Year(t *testing.T) {
@@ -200,15 +199,6 @@ func Test_calcPart2(t *testing.T) {
200199
close(in)
201200
}
202201

203-
func readerFromFile(tb testing.TB, fpath string) io.Reader {
204-
tb.Helper()
205-
206-
file, err := os.Open(fpath)
207-
require.NoError(tb, err)
208-
209-
return file
210-
}
211-
212202
func Test_calc(t *testing.T) {
213203
type args struct {
214204
inputPath string
@@ -245,7 +235,7 @@ func Test_calc(t *testing.T) {
245235
tt := tt
246236

247237
t.Run(tt.name, func(t *testing.T) {
248-
input := readerFromFile(t, tt.args.inputPath)
238+
input := utils.ReaderFromFile(t, tt.args.inputPath)
249239
got, err := calc(input, tt.args.calcFn)
250240

251241
if tt.wantErr {

0 commit comments

Comments
 (0)