Day 6
This commit is contained in:
@@ -1,61 +0,0 @@
|
|||||||
package day01
|
|
||||||
|
|
||||||
import (
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Part1(input string) int {
|
|
||||||
total := 0
|
|
||||||
lines := strings.Split(input, "\r\n")
|
|
||||||
for _, line := range lines {
|
|
||||||
regex := regexp.MustCompile("[a-zA-Z]")
|
|
||||||
numbers := regex.ReplaceAllString(line, "")
|
|
||||||
number := string(numbers[0]) + string(numbers[len(numbers)-1])
|
|
||||||
i, _ := strconv.Atoi(number)
|
|
||||||
total += i
|
|
||||||
}
|
|
||||||
|
|
||||||
return total
|
|
||||||
}
|
|
||||||
|
|
||||||
func Part2(input string) int {
|
|
||||||
values := map[string]string{
|
|
||||||
"one": "1",
|
|
||||||
"two": "2",
|
|
||||||
"three": "3",
|
|
||||||
"four": "4",
|
|
||||||
"five": "5",
|
|
||||||
"six": "6",
|
|
||||||
"seven": "7",
|
|
||||||
"eight": "8",
|
|
||||||
"nine": "9",
|
|
||||||
}
|
|
||||||
lines := strings.Split(input, "\r\n")
|
|
||||||
s := ""
|
|
||||||
|
|
||||||
total := 0
|
|
||||||
for _, line := range lines {
|
|
||||||
numbers := []string{}
|
|
||||||
for _, c := range line {
|
|
||||||
s = s + string(c)
|
|
||||||
if num, err := strconv.Atoi(string(c)); err == nil {
|
|
||||||
numbers = append(numbers, strconv.Itoa(num))
|
|
||||||
s = ""
|
|
||||||
}
|
|
||||||
for key, value := range values {
|
|
||||||
if strings.Contains(s, key) {
|
|
||||||
numbers = append(numbers, value)
|
|
||||||
buffer := s[len(s)-1]
|
|
||||||
s = "" + string(buffer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
number := numbers[0] + numbers[len(numbers)-1]
|
|
||||||
i, _ := strconv.Atoi(number)
|
|
||||||
total += i
|
|
||||||
s = ""
|
|
||||||
}
|
|
||||||
return total
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package day01
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPart1(t *testing.T) {
|
|
||||||
r := Part1(`1abc2
|
|
||||||
pqr3stu8vwx
|
|
||||||
a1b2c3d4e5f
|
|
||||||
treb7uchet`)
|
|
||||||
assert.Equal(t, 142, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPart2(t *testing.T) {
|
|
||||||
r := Part2(`two1nine
|
|
||||||
eightwothree
|
|
||||||
abcone2threexyz
|
|
||||||
xtwone3four
|
|
||||||
4nineeightseven2
|
|
||||||
zoneight234
|
|
||||||
7pqrstsixteen`)
|
|
||||||
assert.Equal(t, 281, r)
|
|
||||||
}
|
|
||||||
40
2023/gareth/day06/day06.go
Normal file
40
2023/gareth/day06/day06.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package day06
|
||||||
|
|
||||||
|
type Race struct {
|
||||||
|
Time int
|
||||||
|
Distance int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(input string) int {
|
||||||
|
var Races []Race
|
||||||
|
Races = append(Races, Race{56, 546})
|
||||||
|
Races = append(Races, Race{97, 1927})
|
||||||
|
Races = append(Races, Race{78, 1131})
|
||||||
|
Races = append(Races, Race{75, 1139})
|
||||||
|
|
||||||
|
total := 1
|
||||||
|
for _, r := range Races {
|
||||||
|
halfTime := r.Time / 2
|
||||||
|
for i := 0; i <= halfTime; i++ {
|
||||||
|
raceDis := i * (r.Time - i)
|
||||||
|
if raceDis > r.Distance {
|
||||||
|
total = total * (r.Time - (i * 2) + 1)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(input string) int {
|
||||||
|
r := Race{56977875, 546192711311139}
|
||||||
|
halfTime := r.Time / 2
|
||||||
|
for i := 0; i <= halfTime; i++ {
|
||||||
|
raceDis := i * (r.Time - i)
|
||||||
|
if raceDis > r.Distance {
|
||||||
|
return r.Time - (i * 2) + 1
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
16
2023/gareth/day06/day06_test.go
Normal file
16
2023/gareth/day06/day06_test.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package day06
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPart1(t *testing.T) {
|
||||||
|
r := Part1(``)
|
||||||
|
assert.Equal(t, 1624896, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPart2(t *testing.T) {
|
||||||
|
r := Part2(``)
|
||||||
|
assert.Equal(t, 32583852, r)
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user