Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
e32a8489e3 | |||
8e95641aa4 | |||
15c31708d1 | |||
c17546495d |
10 changed files with 210 additions and 0 deletions
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# Usage
|
||||||
|
1. Change directory to chosen day
|
||||||
|
2. `go run dayXX.go < input.txt`
|
144
day02/day2.go
Normal file
144
day02/day2.go
Normal file
|
@ -0,0 +1,144 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Limitations struct {
|
||||||
|
red int
|
||||||
|
blue int
|
||||||
|
green int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Draw struct {
|
||||||
|
red int
|
||||||
|
blue int
|
||||||
|
green int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Game struct {
|
||||||
|
id int
|
||||||
|
draws []Draw
|
||||||
|
meta Meta
|
||||||
|
}
|
||||||
|
|
||||||
|
type Meta struct {
|
||||||
|
possible bool
|
||||||
|
red_min int
|
||||||
|
green_min int
|
||||||
|
blue_min int
|
||||||
|
}
|
||||||
|
|
||||||
|
func readInput() string {
|
||||||
|
stdin, err := io.ReadAll(os.Stdin)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
str := string(stdin)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func getColors(input string, color string) string {
|
||||||
|
rg := regexp.MustCompile(`[0-9]+ ` + regexp.QuoteMeta(color))
|
||||||
|
return rg.FindString(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseGameLine(line string, limits Limitations) Game {
|
||||||
|
splitted := strings.Split(line, ":")
|
||||||
|
draws_splitted := strings.Split(splitted[1], ";")
|
||||||
|
game_prefix := splitted[0]
|
||||||
|
|
||||||
|
id, _ := strconv.Atoi(strings.Split(game_prefix, " ")[1])
|
||||||
|
draws := []Draw{}
|
||||||
|
meta := Meta{}
|
||||||
|
red_min, green_min, blue_min := 0, 0, 0
|
||||||
|
meta.possible = true
|
||||||
|
for _, v := range draws_splitted {
|
||||||
|
red, _ := strconv.Atoi(strings.Split(getColors(v, "red"), " ")[0])
|
||||||
|
green, _ := strconv.Atoi(strings.Split(getColors(v, "green"), " ")[0])
|
||||||
|
blue, _ := strconv.Atoi(strings.Split(getColors(v, "blue"), " ")[0])
|
||||||
|
if (red > limits.red) || (green > limits.green) || (blue > limits.blue) {
|
||||||
|
meta.possible = false
|
||||||
|
}
|
||||||
|
if red_min < red {
|
||||||
|
red_min = red
|
||||||
|
}
|
||||||
|
if green_min < green {
|
||||||
|
green_min = green
|
||||||
|
}
|
||||||
|
if blue_min < blue {
|
||||||
|
blue_min = blue
|
||||||
|
}
|
||||||
|
draw := Draw{
|
||||||
|
red: red,
|
||||||
|
blue: blue,
|
||||||
|
green: green,
|
||||||
|
}
|
||||||
|
draws = append(draws, draw)
|
||||||
|
}
|
||||||
|
meta.blue_min = blue_min
|
||||||
|
meta.red_min = red_min
|
||||||
|
meta.green_min = green_min
|
||||||
|
game := Game{
|
||||||
|
id: id,
|
||||||
|
draws: draws,
|
||||||
|
meta: meta,
|
||||||
|
}
|
||||||
|
|
||||||
|
// fmt.Println(game)
|
||||||
|
return game
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1(lines []string) {
|
||||||
|
limits := Limitations{
|
||||||
|
red: 12,
|
||||||
|
green: 13,
|
||||||
|
blue: 14,
|
||||||
|
}
|
||||||
|
var games []Game
|
||||||
|
sum_ids := 0
|
||||||
|
for _, l := range lines {
|
||||||
|
if len(l) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
game := parseGameLine(l, limits)
|
||||||
|
if game.meta.possible {
|
||||||
|
games = append(games, game)
|
||||||
|
sum_ids += game.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Sum ids:", sum_ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(lines []string) {
|
||||||
|
var games []Game
|
||||||
|
|
||||||
|
for _, l := range lines {
|
||||||
|
if len(l) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
game := parseGameLine(l, Limitations{})
|
||||||
|
games = append(games, game)
|
||||||
|
}
|
||||||
|
sum := 0
|
||||||
|
for _, g := range games {
|
||||||
|
sum += (g.meta.red_min * g.meta.green_min * g.meta.blue_min)
|
||||||
|
}
|
||||||
|
fmt.Println(sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
input := readInput()
|
||||||
|
lines := strings.Split(input, "\n")
|
||||||
|
fmt.Println("---PART 1---")
|
||||||
|
part1(lines)
|
||||||
|
fmt.Println("---PART 2---")
|
||||||
|
part2(lines)
|
||||||
|
}
|
5
day02/example.txt
Normal file
5
day02/example.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||||
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||||
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||||
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||||
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
35
day_template/day.go
Normal file
35
day_template/day.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func readInput() string {
|
||||||
|
stdin, err := io.ReadAll(os.Stdin)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
str := string(stdin)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1(lines []string) {
|
||||||
|
fmt.Println("Part 1 not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(lines []string) {
|
||||||
|
fmt.Println("Part 2 not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
input := readInput()
|
||||||
|
lines := strings.Split(input, "\n")
|
||||||
|
fmt.Println("---PART 1---")
|
||||||
|
part1(lines)
|
||||||
|
fmt.Println("---PART 2---")
|
||||||
|
part2(lines)
|
||||||
|
}
|
1
go.work
Normal file
1
go.work
Normal file
|
@ -0,0 +1 @@
|
||||||
|
go 1.21.5
|
22
new_day.sh
Normal file
22
new_day.sh
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#!/bin/sh
|
||||||
|
# Code borrowed from https://github.com/Daste745/aoc-2023/blob/master/new_day.sh
|
||||||
|
if [ -z "$1" ]
|
||||||
|
then
|
||||||
|
cat << EOF
|
||||||
|
Usage: new_day.sh <day_number>
|
||||||
|
|
||||||
|
Example: new_day.sh 08
|
||||||
|
EOF
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
day_number=$1
|
||||||
|
if [ -d "day$day_number" ]
|
||||||
|
then
|
||||||
|
echo "Day$day_number already exists"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p ./day$day_number/
|
||||||
|
cp -v ./day_template/day.go ./day$day_number/day$day_number.go
|
||||||
|
|
Loading…
Add table
Reference in a new issue