Browse Source

[2020-12-11] Add solutions for day11

master
Elis Hirwing 9 months ago
parent
commit
9e61d82146
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 10
      day11/example.txt
  3. 90
      day11/input.txt
  4. 94
      day11/lib.go
  5. 21
      day11/parse.go
  6. 21
      day11/solve1.go
  7. 14
      day11/solve2.go
  8. 4
      main.go

2
Makefile

@ -33,6 +33,8 @@ day10:
go run main.go day10 input
day11:
go run main.go day11 input
day12:
day13:
day14:

10
day11/example.txt

@ -0,0 +1,10 @@
L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL

90
day11/input.txt

@ -0,0 +1,90 @@
LLLLLLL.LLLLLLLLLLLL.LL.L.LLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLL.LLLL.LLLLL.LLLLLLLLLLLLL
LLLLLLL.LLLLLLLLLLLL.LLL..LLLLLLLLLLLLLLLLLLLLLLL..LLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLL
LLLLLLLLLLLLLL.LLLLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLL
LLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLL.LLLLLLLL.LLLL
LLLLLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLL
LLLLLLL.LLLLLL.L.LLLLLLLL.L.LLLLLL.LLLLLL.LL.LLLLL.LLLLLLLLLLLLLLLLL.LLLL.LLLLL.LLLLLLLLLLLLL
.L.....L...L.....LL..L...LLL.L.LL..LLL..LL.LLL...LLLL..L......L..........L...L..LL..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LLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLL.LLLLLLLLLLLLL
LLLLLLL.LLLL.L.LLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLL.LLLL.LLLLL.LLLL.LLLLLLLL
LLLLLLL.LLLLLLLLLLLL..LLL.LLLLLLLL..LLLLLLLLLLLLLL.LLLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLL.LLLLL

94
day11/lib.go

@ -0,0 +1,94 @@
package day11
type Coordinate struct {
X, Y int
}
func GetNearbySeatStatus(coord Coordinate, checkOnlyNearby bool) int {
occupied := 0
for x := -1; x <= 1; x++ {
for y := -1; y <= 1; y++ {
if x == 0 && y == 0 {
continue
}
isOccupied := CheckSeatDirection(
coord,
Coordinate{X: x, Y: y},
checkOnlyNearby,
)
if isOccupied {
occupied++
}
}
}
return occupied
}
func CheckSeatDirection(coord Coordinate, dir Coordinate, checkOnlyNearby bool) bool {
adjCoord := Coordinate{X: coord.X + dir.X, Y: coord.Y + dir.Y}
// If out of bounds, it's not occupied and we can't check furter
if rows[adjCoord] == "" {
return false
}
// If we only should check one step, just check that one step
if checkOnlyNearby {
return rows[adjCoord] == "#"
}
// If it's a floor tile, recurse further
if rows[adjCoord] == "." {
return CheckSeatDirection(adjCoord, dir, checkOnlyNearby)
}
return rows[adjCoord] == "#"
}
func MakeSeatChanges(tolerance int, checkOnlyNearby bool) int {
changes := make(map[Coordinate]string)
// Calculate changes
for coord, status := range rows {
// Ignore floor tiles
if status == "." {
continue
}
// Get nearby seat statuses
occupiedCount := GetNearbySeatStatus(coord, checkOnlyNearby)
// If current seat is free, check that there's nobody around
if status == "L" && occupiedCount == 0 {
changes[coord] = "#"
}
// If the current seat isn't free, check how many people there's around
if status == "#" && occupiedCount >= tolerance {
changes[coord] = "L"
}
}
// Apply changes
for coord, change := range changes {
rows[coord] = change
}
return len(changes)
}
func CountOccupiedSeats() int {
counter := 0
for _, status := range rows {
if status == "#" {
counter++
}
}
return counter
}

21
day11/parse.go

@ -0,0 +1,21 @@
package day11
import (
"git.elis.nu/etu/aoc2020/utils"
)
var rows map[Coordinate]string
func ParseFile(input string) {
rows = make(map[Coordinate]string)
x := 0
// Parse file
for _, line := range utils.GetLinesFromFile("day11/" + input + ".txt") {
for y := 0; y < len(line); y++ {
rows[Coordinate{x, y}] = string(line[y])
}
x++
}
}

21
day11/solve1.go

@ -0,0 +1,21 @@
package day11
import (
"log"
)
func Solve1() {
changes := 1
for changes > 0 {
changes = MakeSeatChanges(4, true)
}
log.Printf("2020-12-11.01: Answer: %d\n", CountOccupiedSeats())
// Reset input
for coord, status := range rows {
if status != "." {
rows[coord] = "L"
}
}
}

14
day11/solve2.go

@ -0,0 +1,14 @@
package day11
import (
"log"
)
func Solve2() {
changes := 1
for changes > 0 {
changes = MakeSeatChanges(5, false)
}
log.Printf("2020-12-11.02: Answer: %d\n", CountOccupiedSeats())
}

4
main.go

@ -14,6 +14,7 @@ import (
"git.elis.nu/etu/aoc2020/day08"
"git.elis.nu/etu/aoc2020/day09"
"git.elis.nu/etu/aoc2020/day10"
"git.elis.nu/etu/aoc2020/day11"
"git.elis.nu/etu/aoc2020/utils"
)
@ -52,5 +53,8 @@ func main() {
case "day10":
utils.Perf("2020-12-10", day10.ParseFile, day10.Solve1, day10.Solve2)
case "day11":
utils.Perf("2020-12-11", day11.ParseFile, day11.Solve1, day11.Solve2)
}
}

Loading…
Cancel
Save