mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-12-21 13:21:54 +01:00
solve day 1 part 1 & 2
This commit is contained in:
parent
bf4169a0a9
commit
2a13616ae5
12 changed files with 1195 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -227,3 +227,4 @@ dmypy.json
|
|||
|
||||
# End of https://www.gitignore.io/api/osx,pycharm,python
|
||||
.direnv/
|
||||
.venv
|
||||
|
|
2
.rtx.toml
Normal file
2
.rtx.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
[tools]
|
||||
python = {version = "latest", virtualenv = ".venv"}
|
|
@ -1 +0,0 @@
|
|||
python 3.11.2
|
104
2023/day01_trebuchet.go
Normal file
104
2023/day01_trebuchet.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
package _023
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Part1(input io.Reader) (int, error) {
|
||||
scanner := bufio.NewScanner(input)
|
||||
sum := 0
|
||||
|
||||
for scanner.Scan() {
|
||||
begValue, endValue := 0, 0
|
||||
line := scanner.Text()
|
||||
|
||||
for i := 0; i < len(line); i++ {
|
||||
beg := line[i]
|
||||
if val, err := strconv.Atoi(string(beg)); err == nil && begValue == 0 {
|
||||
begValue = val
|
||||
}
|
||||
|
||||
j := len(line) - 1 - i
|
||||
end := line[j]
|
||||
if val, err := strconv.Atoi(string(end)); err == nil && endValue == 0 {
|
||||
endValue = val
|
||||
}
|
||||
|
||||
if begValue > 0 && endValue > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
sum += begValue*10 + endValue
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func Part2(input io.Reader) (int, error) {
|
||||
reg, err := regexp.Compile(`[0-9]|one|two|three|four|five|six|seven|eight|nine`)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(input)
|
||||
sum := 0
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
begValue, endValue := 0, 0
|
||||
|
||||
for i := 0; i < len(line); i++ {
|
||||
beg := reg.FindString(line[:i])
|
||||
if value := translate(beg); value > 0 && begValue == 0 {
|
||||
begValue = value
|
||||
}
|
||||
|
||||
j := len(line) - i - 1
|
||||
end := reg.FindString(line[j:])
|
||||
if value := translate(end); value > 0 && endValue == 0 {
|
||||
endValue = value
|
||||
}
|
||||
|
||||
if begValue > 0 && endValue > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if begValue == 0 {
|
||||
begValue = endValue
|
||||
}
|
||||
|
||||
sum += begValue*10 + endValue
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func translate(digit string) int {
|
||||
switch digit {
|
||||
case "1", "one":
|
||||
return 1
|
||||
case "2", "two":
|
||||
return 2
|
||||
case "3", "three":
|
||||
return 3
|
||||
case "4", "four":
|
||||
return 4
|
||||
case "5", "five":
|
||||
return 5
|
||||
case "6", "six":
|
||||
return 6
|
||||
case "7", "seven":
|
||||
return 7
|
||||
case "8", "eight":
|
||||
return 8
|
||||
case "9", "nine":
|
||||
return 9
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
39
2023/day01_trebuchet_test.go
Normal file
39
2023/day01_trebuchet_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package _023
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
tests := []testCase{
|
||||
{"inputs/day01_test1", 142},
|
||||
{"inputs/day01", 53974},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.filename, check(test, Part1))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
tests := []testCase{
|
||||
{"inputs/day01_test2", 281},
|
||||
{"inputs/day01_test3", 277},
|
||||
{"inputs/day01", 52840},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.filename, check(test, Part2))
|
||||
}
|
||||
}
|
||||
|
||||
func check(test testCase, fn solveFunc) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
file, err := os.Open(test.filename)
|
||||
require.NoError(t, err)
|
||||
got, err := fn(file)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, test.want, got)
|
||||
}
|
||||
}
|
1000
2023/inputs/day01
Normal file
1000
2023/inputs/day01
Normal file
File diff suppressed because it is too large
Load diff
4
2023/inputs/day01_test1
Normal file
4
2023/inputs/day01_test1
Normal file
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
7
2023/inputs/day01_test2
Normal file
7
2023/inputs/day01_test2
Normal file
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
7
2023/inputs/day01_test3
Normal file
7
2023/inputs/day01_test3
Normal file
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrsteightwo
|
10
2023/types.go
Normal file
10
2023/types.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package _023
|
||||
|
||||
import "io"
|
||||
|
||||
type testCase struct {
|
||||
filename string
|
||||
want int
|
||||
}
|
||||
|
||||
type solveFunc func(reader io.Reader) (int, error)
|
11
go.mod
Normal file
11
go.mod
Normal file
|
@ -0,0 +1,11 @@
|
|||
module github.com/Crocmagnon/advent-of-code
|
||||
|
||||
go 1.21.4
|
||||
|
||||
require github.com/stretchr/testify v1.8.4
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
10
go.sum
Normal file
10
go.sum
Normal file
|
@ -0,0 +1,10 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
Loading…
Reference in a new issue