This commit is contained in:
David 2023-12-02 15:57:50 -05:00
parent 94a11e24c5
commit 725e453168
3 changed files with 129 additions and 5 deletions

120
02/main.go Normal file
View File

@ -0,0 +1,120 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
func mustAtoi(line string) int {
i, _ := strconv.Atoi(line)
return i
}
func main() {
start := time.Now()
partOne()
duration := time.Since(start)
partTwo()
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
}
func makeScanner(test bool) *bufio.Scanner {
var f *os.File
if test {
f, _ = os.Open("inputs/testinput")
} else {
f, _ = os.Open("inputs/input")
}
reader := bufio.NewReader(f)
return bufio.NewScanner(reader)
}
type game struct {
red int
green int
blue int
}
func partOne() {
scanner := makeScanner(false)
sum := 0
round := 0
max := game{
red: 12,
green: 13,
blue: 14,
}
for scanner.Scan() {
line := scanner.Text()
ok := true
round++
_, draws, _ := strings.Cut(line, ": ")
for _, draws := range strings.Split(draws, "; ") {
iter := strings.Split(draws, " ")
g := game{}
for i := 0; i < len(iter); i++ {
seen := mustAtoi(iter[i])
i++
switch iter[i][0] {
case 'r':
g.red = seen
case 'g':
g.green = seen
case 'b':
g.blue = seen
}
}
if g.red > max.red || g.blue > max.blue || g.green > max.green {
ok = false
}
}
if ok {
sum += round
}
}
fmt.Println(sum)
}
func partTwo() {
scanner := makeScanner(false)
sum := 0
for scanner.Scan() {
line := scanner.Text()
_, draws, _ := strings.Cut(line, ": ")
max := game{}
for _, draws := range strings.Split(draws, "; ") {
iter := strings.Split(draws, " ")
g := game{}
for i := 0; i < len(iter); i++ {
seen := mustAtoi(iter[i])
i++
switch iter[i][0] {
case 'r':
g.red = seen
case 'g':
g.green = seen
case 'b':
g.blue = seen
}
}
if g.red > max.red {
max.red = g.red
}
if g.blue > max.blue {
max.blue = g.blue
}
if g.green > max.green {
max.green = g.green
}
}
sum += max.blue * max.green * max.red
}
fmt.Println(sum)
}

View File

@ -3,14 +3,18 @@
(chicken format)
)
(define print-line
(lambda (line)
(printf "~A~%" line)))
(call-with-input-file "inputs/input"
(lambda (port)
(map
(lambda (line) (printf "~A~%" line))
print-line
(read-lines port))))
(call-with-input-file "inputs/input"
(lambda (port)
(map
(lambda (line) (printf "~A~%" line))
print-line
(read-lines port))))

6
new.sh
View File

@ -14,8 +14,8 @@ if [ "${1}" != "" ]; then
if [ ! -f "${padded}/main.go" ]; then
cp -n main.go.tmpl ${padded}/main.go
fi
if [ ! -f "${padded}/main.scm" ]; then
cp -n main.scm.tmpl ${padded}/main.scm
fi
# if [ ! -f "${padded}/main.scm" ]; then
# cp -n main.scm.tmpl ${padded}/main.scm
# fi
echo "https://adventofcode.com/${__year}/day/${1}"
fi