-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 2017/day02 initial commit * feat: Register 2017/day02 * chore: Add tests * feat: Implement 2017/day02 part1 * feat: Implement 2017/day02 solution * refactor: Fix code smells and linter bugs
- Loading branch information
1 parent
f81000a
commit 1f33a16
Showing
18 changed files
with
528 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
116 1470 2610 179 2161 2690 831 1824 2361 1050 2201 118 145 2275 2625 2333 | ||
976 220 1129 553 422 950 332 204 1247 1092 1091 159 174 182 984 713 | ||
84 78 773 62 808 83 1125 1110 1184 145 1277 982 338 1182 75 679 | ||
3413 3809 3525 2176 141 1045 2342 2183 157 3960 3084 2643 119 108 3366 2131 | ||
1312 205 343 616 300 1098 870 1008 1140 1178 90 146 980 202 190 774 | ||
4368 3905 3175 4532 3806 1579 4080 259 2542 221 4395 4464 208 3734 234 4225 | ||
741 993 1184 285 1062 372 111 118 63 843 325 132 854 105 956 961 | ||
85 79 84 2483 858 2209 2268 90 2233 1230 2533 322 338 68 2085 1267 | ||
2688 2022 112 130 1185 103 1847 3059 911 107 2066 1788 2687 2633 415 1353 | ||
76 169 141 58 161 66 65 225 60 152 62 64 156 199 80 56 | ||
220 884 1890 597 3312 593 4259 222 113 2244 3798 4757 216 1127 4400 178 | ||
653 369 216 132 276 102 265 889 987 236 239 807 1076 932 84 864 | ||
799 739 75 1537 82 228 69 1397 1396 1203 1587 63 313 1718 1375 469 | ||
1176 112 1407 136 1482 1534 1384 1202 604 851 190 284 1226 113 114 687 | ||
73 1620 81 1137 812 75 1326 1355 1545 1666 1356 1681 1732 85 128 902 | ||
571 547 160 237 256 30 496 592 385 576 183 692 192 387 647 233 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Package day02 contains solution for https://adventofcode.com/2017/day/2 puzzle. | ||
package day02 | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/obalunenko/advent-of-code/internal/puzzles" | ||
) | ||
|
||
func init() { | ||
puzzles.Register(solution{}) | ||
} | ||
|
||
type solution struct{} | ||
|
||
func (s solution) Day() string { | ||
return puzzles.Day02.String() | ||
} | ||
|
||
func (s solution) Year() string { | ||
return puzzles.Year2017.String() | ||
} | ||
|
||
func (s solution) Part1(input io.Reader) (string, error) { | ||
var f checksumFunc = func(row []string) (int, error) { | ||
var min, max int | ||
|
||
for i, number := range row { | ||
d, err := strconv.Atoi(number) | ||
if err != nil { | ||
return 0, fmt.Errorf("atoi: %w", err) | ||
} | ||
|
||
if i == 0 { | ||
min, max = d, d | ||
} | ||
|
||
if d < min { | ||
min = d | ||
} | ||
|
||
if d > max { | ||
max = d | ||
} | ||
} | ||
|
||
return max - min, nil | ||
} | ||
|
||
return findChecksum(input, f) | ||
} | ||
|
||
func (s solution) Part2(input io.Reader) (string, error) { | ||
var f checksumFunc = func(row []string) (int, error) { | ||
numbers, err := stringsToNumbers(row) | ||
if err != nil { | ||
return 0, fmt.Errorf("strings to numbers: %w", err) | ||
} | ||
|
||
for i := 0; i < len(numbers); i++ { | ||
d1 := numbers[i] | ||
|
||
for j := i + 1; j < len(numbers); j++ { | ||
d2 := numbers[j] | ||
|
||
var a, b = d1, d2 | ||
|
||
if a < b { | ||
a, b = b, a | ||
} | ||
|
||
if a%b == 0 { | ||
return a / b, nil | ||
} | ||
} | ||
} | ||
|
||
return 0, ErrNotFound | ||
} | ||
|
||
return findChecksum(input, f) | ||
} | ||
|
||
func stringsToNumbers(row []string) ([]int, error) { | ||
numbers := make([]int, 0, len(row)) | ||
|
||
for _, n := range row { | ||
d, err := strconv.Atoi(n) | ||
if err != nil { | ||
return nil, fmt.Errorf("atoi: %w", err) | ||
} | ||
|
||
numbers = append(numbers, d) | ||
} | ||
|
||
return numbers, nil | ||
} | ||
|
||
// ErrNotFound returns when checksum could not be found. | ||
var ErrNotFound = errors.New("checksum not found") | ||
|
||
type checksumFunc func(row []string) (int, error) | ||
|
||
func findChecksum(spreadsheet io.Reader, f checksumFunc) (string, error) { | ||
scanner := bufio.NewScanner(spreadsheet) | ||
|
||
var checksum int | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
|
||
line = strings.ReplaceAll(line, "\t", " ") | ||
|
||
numbers := strings.Split(line, " ") | ||
|
||
n, err := f(numbers) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
checksum += n | ||
} | ||
|
||
return strconv.Itoa(checksum), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package day02 | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_solution_Year(t *testing.T) { | ||
var s solution | ||
|
||
want := "2017" | ||
got := s.Year() | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func Test_solution_Day(t *testing.T) { | ||
var s solution | ||
|
||
want := "day02" | ||
got := s.Day() | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func Test_solution_Part1(t *testing.T) { | ||
var s solution | ||
|
||
type args struct { | ||
input io.Reader | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "example from spec", | ||
args: args{ | ||
input: strings.NewReader("5 1 9 5\n7 5 3\n2 4 6 8\n"), | ||
}, | ||
want: "18", | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := s.Part1(tt.args.input) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
|
||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func Test_solution_Part2(t *testing.T) { | ||
var s solution | ||
|
||
type args struct { | ||
input io.Reader | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "example from spec", | ||
args: args{ | ||
input: strings.NewReader("5 9 2 8\n9 4 7 3\n3 8 6 5\n"), | ||
}, | ||
want: "9", | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := s.Part2(tt.args.input) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
|
||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.