2023 day 4 but 3x faster

This commit is contained in:
Tristan 2023-12-20 19:31:01 +00:00
parent a9fc4c9e60
commit 8cbfa778c2
9 changed files with 3168 additions and 5 deletions

1000
2023/01/cast.txt Normal file

File diff suppressed because it is too large Load diff

1000
2023/01/filtered.txt Normal file

File diff suppressed because it is too large Load diff

1000
2023/01/input.txt Normal file

File diff suppressed because it is too large Load diff

15
2023/01/part1.py Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env python3
import os
path = os.path.dirname(os.path.abspath(__file__))
nums = []
with open(path+'/filtered.txt', 'r') as input:
for text in input.readlines():
a = int(text[0])
b = int(text[-2])
nums.append((10*a) + b)
print(nums)
print(sum(nums))

58
2023/01/part2.py Normal file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env python3
# 52746 too low
import os
import re
path = os.path.dirname(os.path.abspath(__file__))
nums = []
def toInt(txt: str):
try:
num = int(txt)
except:
if txt == 'one':
num = 1
elif txt == 'two':
num = 2
elif txt == 'three':
num = 3
elif txt == 'four':
num = 4
elif txt == 'five':
num = 5
elif txt == 'six':
num = 6
elif txt == 'seven':
num = 7
elif txt == 'eight':
num = 8
elif txt == 'nine':
num = 9
finally:
return num
with open(path+'/input.txt', 'r') as input:
for text in input.readlines():
text = text.strip()
print('original:',text)
left = re.findall(r"(?=(one|two|three|four|five|six|seven|eight|nine|1|2|3|4|5|6|7|8|9))", text)
print('search:',left)
a = toInt(left[0])
b = toInt(left[-1])
print(a, b)
total = (10*a) + b
print('total:',total)
nums.append(total)
print(sum(nums))

View file

@ -0,0 +1,86 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
lines := []string{}
for text, err := reader.ReadString('\n'); err == nil; text, err = reader.ReadString('\n') {
lines = append(lines, text)
}
size := len(lines)
fmt.Printf("%v lines\n", size)
linepoints := make([]int, size)
for i, text := range lines {
amt := score(text)
fmt.Printf("%v %v\n", text, amt)
linepoints[i] = amt
}
const maxPoints = 10
newTot := 0;
mults := make([]int, size)
for line, points := range linepoints {
mul := 1
for i := line - maxPoints; i < line; i++ {
if i < 0 {
continue
}
end := i + linepoints[i]
if end >= line {
mul += mults[i]
}
}
mults[line] = mul
newTot += mul
fmt.Printf("%v | +%v = %v \n", points, mul, newTot)
}
fmt.Printf("total cards: %v", newTot)
}
func score(text string) int {
amt := 0
text = strings.Trim(text, "\n")
gameStr := strings.ReplaceAll(strings.Split(text, ": ")[1], " ", " ")
nums := strings.Split(gameStr, " | ")
winners := strings.Split(nums[0], " ")
draw := strings.Split(nums[1], " ")
for _, n := range draw {
match := false
for _, w := range winners {
match = n == w
if match {
break
}
}
if match {
amt++
}
}
return amt
}

View file

@ -19,7 +19,7 @@ func main() {
text = strings.Trim(text, "\n") text = strings.Trim(text, "\n")
points := score(text); points := score(text);
fmt.Printf("%v: %v\n", text, points); // fmt.Printf("%v: %v\n", text, points);
newMultipliers := []int{}; newMultipliers := []int{};
for i := range multipliers { for i := range multipliers {
@ -36,7 +36,7 @@ func main() {
} }
total += multiplier; total += multiplier;
fmt.Printf("+%v = %v\n", multiplier, total); fmt.Printf("%v | +%v = %v\n", points, multiplier, total);
} }
fmt.Printf("total cards: %v", total); fmt.Printf("total cards: %v", total);

View file

@ -1,3 +1,3 @@
module tristans.cloud/aoc module tristans.cloud/aoc/2023/06
go 1.20 go 1.20

View file

@ -1,7 +1,11 @@
go 1.21.4 go 1.21.4
use ( use (
2023/10 2023/04
2023/09 2023/05
2023/06
2023/07
2023/08 2023/08
2023/09
2023/10
) )