chore: setup folders for aoc 2024
This commit is contained in:
4
aoc2023/day01/example1.txt
Normal file
4
aoc2023/day01/example1.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
7
aoc2023/day01/example2.txt
Normal file
7
aoc2023/day01/example2.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
1000
aoc2023/day01/input.txt
Normal file
1000
aoc2023/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
102
aoc2023/day01/main.go
Normal file
102
aoc2023/day01/main.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
aoc "go.sour.is/advent-of-code"
|
||||
)
|
||||
|
||||
func main() { aoc.MustResult(aoc.Runner(run)) }
|
||||
|
||||
type result struct {
|
||||
sum int
|
||||
sum2 int
|
||||
}
|
||||
|
||||
func (r result) String() string {
|
||||
return fmt.Sprintln("result pt1:", r.sum, "\nresult pt2:", r.sum2)
|
||||
}
|
||||
|
||||
var numbers = []string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
|
||||
|
||||
func run(scan *bufio.Scanner) (*result, error) {
|
||||
result := &result{}
|
||||
|
||||
for scan.Scan() {
|
||||
var first, last int
|
||||
var first2, last2 int
|
||||
|
||||
text := scan.Text()
|
||||
|
||||
slice := make([]rune, 5)
|
||||
for i := range text {
|
||||
copy(slice, []rune(text[i:]))
|
||||
|
||||
switch {
|
||||
case slice[0] >= '0' && slice[0] <= '9':
|
||||
if first == 0 {
|
||||
first = int(slice[0] - '0')
|
||||
}
|
||||
if first2 == 0 {
|
||||
first2 = int(slice[0] - '0')
|
||||
}
|
||||
default:
|
||||
if first2 != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for i, s := range numbers {
|
||||
if strings.HasPrefix(string(slice), s) {
|
||||
first2 = i
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if first != 0 && first2 != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
text = string(aoc.Reverse([]rune(text)))
|
||||
|
||||
for i := range text {
|
||||
copy(slice, []rune(text[i:]))
|
||||
slice = aoc.Reverse(slice)
|
||||
|
||||
switch {
|
||||
case slice[4] >= '0' && slice[4] <= '9':
|
||||
if last == 0 {
|
||||
last = int(slice[4] - '0')
|
||||
}
|
||||
if last2 == 0 {
|
||||
last2 = int(slice[4] - '0')
|
||||
}
|
||||
default:
|
||||
if last2 != 0 {
|
||||
continue
|
||||
}
|
||||
for i, s := range numbers {
|
||||
if strings.HasSuffix(string(slice), s) {
|
||||
last2 = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if last != 0 && last2 != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
result.sum += first*10 + last
|
||||
result.sum2 += first2*10 + last2
|
||||
}
|
||||
if err := scan.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
53
aoc2023/day01/main_test.go
Normal file
53
aoc2023/day01/main_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/matryer/is"
|
||||
)
|
||||
|
||||
//go:embed example1.txt
|
||||
var example1 []byte
|
||||
|
||||
//go:embed example2.txt
|
||||
var example2 []byte
|
||||
|
||||
//go:embed input.txt
|
||||
var input []byte
|
||||
|
||||
func TestExample1(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(example1))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.sum, 142)
|
||||
}
|
||||
|
||||
func TestExample2(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(example2))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.sum2, 281)
|
||||
}
|
||||
func TestInput(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(input))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.sum, 54573)
|
||||
is.Equal(result.sum2, 54591)
|
||||
}
|
||||
Reference in New Issue
Block a user