This commit is contained in:
2022-12-15 14:56:28 +01:00
parent 108980344b
commit cc58326f8c
20 changed files with 145 additions and 145 deletions

View File

@@ -1,9 +1,9 @@
package common package common
import ( import (
"bufio" "bufio"
"strconv" "strconv"
"strings" "strings"
) )
type Assignment struct { type Assignment struct {

View File

@@ -1,9 +1,9 @@
package common package common
import ( import (
"bufio" "bufio"
"strconv" "strconv"
"strings" "strings"
) )
type Stack []byte type Stack []byte

View File

@@ -1,7 +1,7 @@
package common package common
import ( import (
"bufio" "bufio"
) )
type Signal string type Signal string

View File

@@ -1,9 +1,9 @@
package common package common
import ( import (
"bufio" "bufio"
"strconv" "strconv"
"strings" "strings"
) )
type FileBase interface { type FileBase interface {

View File

@@ -1,7 +1,7 @@
package common package common
import ( import (
"bufio" "bufio"
) )
type Tree int type Tree int

View File

@@ -1,10 +1,10 @@
package main package main
import ( import (
"aoc2022/day10/common" "aoc2022/day10/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
) )
func main() { func main() {

View File

@@ -1,9 +1,9 @@
package common package common
import ( import (
"bufio" "bufio"
"strconv" "strconv"
"strings" "strings"
) )

View File

@@ -1,11 +1,11 @@
package main package main
import ( import (
"aoc2022/day11/common" "aoc2022/day11/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"sort" "sort"
) )
func main() { func main() {

View File

@@ -1,11 +1,11 @@
package main package main
import ( import (
"aoc2022/day11/common" "aoc2022/day11/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"sort" "sort"
) )
func main() { func main() {

View File

@@ -1,8 +1,8 @@
package common package common
import ( import (
"bufio" "bufio"
"math" "math"
) )

View File

@@ -1,10 +1,10 @@
package main package main
import ( import (
"aoc2022/day12/common" "aoc2022/day12/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
) )
func main() { func main() {

View File

@@ -1,10 +1,10 @@
package main package main
import ( import (
"aoc2022/day12/common" "aoc2022/day12/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
) )
func main() { func main() {

View File

@@ -1,21 +1,21 @@
package main package main
import ( import (
"aoc2022/day13/common" "aoc2022/day13/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
) )
func main() { func main() {
pairs := common.ParsePairs(*bufio.NewScanner(os.Stdin)) pairs := common.ParsePairs(*bufio.NewScanner(os.Stdin))
sum := 0 sum := 0
for i, pair := range pairs { for i, pair := range pairs {
if pair.CheckOrder() { if pair.CheckOrder() {
sum += (i + 1) sum += (i + 1)
} }
} }
fmt.Println(sum) fmt.Println(sum)
} }

View File

@@ -1,26 +1,26 @@
package main package main
import ( import (
"aoc2022/day13/common" "aoc2022/day13/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"sort" "sort"
) )
func main() { func main() {
packets := common.ParsePackets(*bufio.NewScanner(os.Stdin), []string{"[[2]]", "[[6]]"}) packets := common.ParsePackets(*bufio.NewScanner(os.Stdin), []string{"[[2]]", "[[6]]"})
sort.Slice(packets, func(i int, j int) bool { sort.Slice(packets, func(i int, j int) bool {
return packets[i].Compare(packets[j]) < 0 return packets[i].Compare(packets[j]) < 0
}) })
result := 1 result := 1
for i := 0; i < len(packets); i++ { for i := 0; i < len(packets); i++ {
if packets[i].IsSeparator() { if packets[i].IsSeparator() {
result *= (i+1) result *= (i+1)
} }
} }
fmt.Println(result) fmt.Println(result)
} }

View File

@@ -78,8 +78,8 @@ func (tileMap *Map) DropSand() bool {
func (tileMap *Map) AddFloor() { func (tileMap *Map) AddFloor() {
currFloor := 0 currFloor := 0
for i := 0; i < len(*tileMap); i++ { for i := 0; i < len(*tileMap); i++ {
for j := 0; j < len((*tileMap)[i]); j++ { for j := 0; j < len((*tileMap)[i]); j++ {
if (*tileMap)[i][j] == Rock && i > currFloor { if (*tileMap)[i][j] == Rock && i > currFloor {
currFloor = i currFloor = i
} }
@@ -97,19 +97,19 @@ func (tileMap *Map) IsSourceBlocked() bool {
} }
func (tileMap *Map) Print(xMin, xMax, yMin, yMax int) { func (tileMap *Map) Print(xMin, xMax, yMin, yMax int) {
for i := yMin; i <= yMax; i++ { for i := yMin; i <= yMax; i++ {
for j := xMin; j <= xMax; j++ { for j := xMin; j <= xMax; j++ {
switch (*tileMap)[i][j] { switch (*tileMap)[i][j] {
case Air: case Air:
fmt.Print(".") fmt.Print(".")
case Rock: case Rock:
fmt.Print("#") fmt.Print("#")
case Sand: case Sand:
fmt.Print("o") fmt.Print("o")
} }
} }
fmt.Println() fmt.Println()
} }
} }
type RockStructure struct { type RockStructure struct {

View File

@@ -9,16 +9,16 @@ import (
func main() { func main() {
rockStructures := common.Parse(*bufio.NewScanner(os.Stdin)) rockStructures := common.Parse(*bufio.NewScanner(os.Stdin))
tileMap := common.NewMap() tileMap := common.NewMap()
for _, rockStructure := range rockStructures { for _, rockStructure := range rockStructures {
tileMap.AddRockStructure(rockStructure) tileMap.AddRockStructure(rockStructure)
} }
units := 0 units := 0
for tileMap.DropSand() { for tileMap.DropSand() {
units++ units++
} }
fmt.Println(units) fmt.Println(units)
} }

View File

@@ -9,18 +9,18 @@ import (
func main() { func main() {
rockStructures := common.Parse(*bufio.NewScanner(os.Stdin)) rockStructures := common.Parse(*bufio.NewScanner(os.Stdin))
tileMap := common.NewMap() tileMap := common.NewMap()
for _, rockStructure := range rockStructures { for _, rockStructure := range rockStructures {
tileMap.AddRockStructure(rockStructure) tileMap.AddRockStructure(rockStructure)
} }
tileMap.AddFloor() tileMap.AddFloor()
units := 0 units := 0
for !tileMap.IsSourceBlocked() { for !tileMap.IsSourceBlocked() {
tileMap.DropSand() tileMap.DropSand()
units++ units++
} }
fmt.Println(units) fmt.Println(units)
} }

View File

@@ -1,10 +1,10 @@
package common package common
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
) )
@@ -185,21 +185,21 @@ func (tileMap *Map) GetCoverageYInXInterval(xMin, xMax, y int) int {
} }
func (tileMap *Map) Print(xMin, xMax, yMin, yMax int) { func (tileMap *Map) Print(xMin, xMax, yMin, yMax int) {
for i := yMin; i <= yMax; i++ { for i := yMin; i <= yMax; i++ {
for j := xMin; j <= xMax; j++ { for j := xMin; j <= xMax; j++ {
switch tileMap.GetTile(j, i) { switch tileMap.GetTile(j, i) {
case Beacon: case Beacon:
fmt.Print("B") fmt.Print("B")
case Sensor: case Sensor:
fmt.Print("S") fmt.Print("S")
case Covered: case Covered:
fmt.Print("#") fmt.Print("#")
case Free: case Free:
fmt.Print(".") fmt.Print(".")
} }
} }
fmt.Println() fmt.Println()
} }
} }

View File

@@ -1,19 +1,19 @@
package main package main
import ( import (
"aoc2022/day15/common" "aoc2022/day15/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
) )
func main() { func main() {
sensors := common.Parse(*bufio.NewScanner(os.Stdin)) sensors := common.Parse(*bufio.NewScanner(os.Stdin))
tileMap := common.NewMap() tileMap := common.NewMap()
for _, sensor := range sensors { for _, sensor := range sensors {
tileMap.SetSensor(sensor) tileMap.SetSensor(sensor)
} }
fmt.Println(tileMap.GetCoverageY(2000000)) fmt.Println(tileMap.GetCoverageY(2000000))
} }

View File

@@ -1,33 +1,33 @@
package main package main
import ( import (
"aoc2022/day15/common" "aoc2022/day15/common"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
) )
func main() { func main() {
sensors := common.Parse(*bufio.NewScanner(os.Stdin)) sensors := common.Parse(*bufio.NewScanner(os.Stdin))
tileMap := common.NewMap() tileMap := common.NewMap()
for _, sensor := range sensors { for _, sensor := range sensors {
tileMap.SetSensor(sensor) tileMap.SetSensor(sensor)
} }
var frequency int var frequency int
found := false found := false
for i := 0; i <= 4000000 && !found; i++ { for i := 0; i <= 4000000 && !found; i++ {
coverage := tileMap.GetCoverageYInXInterval(0, 4000000, i) coverage := tileMap.GetCoverageYInXInterval(0, 4000000, i)
if coverage <= 4000000 { if coverage <= 4000000 {
for j := 0; j < 4000000 && !found; j++ { for j := 0; j < 4000000 && !found; j++ {
if tileMap.GetTile(j, i) == common.Free { if tileMap.GetTile(j, i) == common.Free {
frequency = j * 4000000 + i frequency = j * 4000000 + i
found = true found = true
} }
} }
} }
} }
fmt.Println(frequency) fmt.Println(frequency)
} }