mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
remove internal/util
move cmpIncrease, cmpDecrease into parseLines
This commit is contained in:
parent
aeb57b5cca
commit
a6295375aa
@ -4,8 +4,6 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
|
||||
)
|
||||
|
||||
type comparator func([]int, int) bool
|
||||
@ -21,13 +19,23 @@ func Solve(buf []byte) (int, error) {
|
||||
}
|
||||
|
||||
func parseLines(r io.Reader) (int, error) {
|
||||
var (
|
||||
cmpIncrease = func(nums []int, i int) bool {
|
||||
return nums[i-1] >= nums[i]
|
||||
}
|
||||
|
||||
cmpDecrease = func(nums []int, i int) bool {
|
||||
return nums[i-1] <= nums[i]
|
||||
}
|
||||
)
|
||||
|
||||
var sum int
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
nums := util.IntsFromString(scanner.Text())
|
||||
nums := intsFromString(scanner.Text())
|
||||
|
||||
if check(nums, util.CmpIncrease) || check(nums, util.CmpDecrease) {
|
||||
if check(nums, cmpIncrease) || check(nums, cmpDecrease) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
22
day-02/internal/one/util.go
Normal file
22
day-02/internal/one/util.go
Normal file
@ -0,0 +1,22 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func mustConv(s string) int {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func intsFromString(s string) []int {
|
||||
nums := []int{}
|
||||
for _, r := range strings.Fields(s) {
|
||||
nums = append(nums, mustConv(r))
|
||||
}
|
||||
return nums
|
||||
}
|
@ -4,8 +4,6 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
|
||||
)
|
||||
|
||||
type comparator func([]int, int) bool
|
||||
@ -22,13 +20,23 @@ func Solve(buf []byte) (int, error) {
|
||||
}
|
||||
|
||||
func parseLines(r io.Reader) (int, error) {
|
||||
var (
|
||||
cmpIncrease = func(nums []int, i int) bool {
|
||||
return nums[i-1] >= nums[i]
|
||||
}
|
||||
|
||||
cmpDecrease = func(nums []int, i int) bool {
|
||||
return nums[i-1] <= nums[i]
|
||||
}
|
||||
)
|
||||
|
||||
var sum int
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
nums := util.IntsFromString(scanner.Text())
|
||||
nums := intsFromString(scanner.Text())
|
||||
|
||||
if check(withDampener, nums, util.CmpIncrease) || check(withDampener, nums, util.CmpDecrease) {
|
||||
if check(withDampener, nums, cmpIncrease) || check(withDampener, nums, cmpDecrease) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
22
day-02/internal/two/util.go
Normal file
22
day-02/internal/two/util.go
Normal file
@ -0,0 +1,22 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func mustConv(s string) int {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func intsFromString(s string) []int {
|
||||
nums := []int{}
|
||||
for _, r := range strings.Fields(s) {
|
||||
nums = append(nums, mustConv(r))
|
||||
}
|
||||
return nums
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func MustConv(s string) int {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func IntsFromString(s string) []int {
|
||||
nums := []int{}
|
||||
for _, r := range strings.Fields(s) {
|
||||
nums = append(nums, MustConv(r))
|
||||
}
|
||||
return nums
|
||||
}
|
||||
|
||||
var CmpIncrease = func(nums []int, i int) bool {
|
||||
return nums[i-1] >= nums[i]
|
||||
}
|
||||
|
||||
var CmpDecrease = func(nums []int, i int) bool {
|
||||
return nums[i-1] <= nums[i]
|
||||
}
|
Loading…
Reference in New Issue
Block a user