Compare commits
10 commits
b17c80d1dd
...
99242d53d7
Author | SHA1 | Date | |
---|---|---|---|
99242d53d7 | |||
6a046fdb1c | |||
b79f1a5d06 | |||
3656193dfa | |||
b196009d5a | |||
72762f3d08 | |||
13c81a6334 | |||
dfb6e1952d | |||
0338b14930 | |||
b5444e5aa5 |
65 changed files with 1501 additions and 505 deletions
22
d1/p1.go
Normal file
22
d1/p1.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package d1
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P1(scans string) int64 {
|
||||
depths := strings.Split(strings.TrimSpace(scans), "\n")
|
||||
downs := 0
|
||||
|
||||
for i := 1; i < len(depths); i++ {
|
||||
next, _ := strconv.Atoi(strings.TrimSpace(depths[i]))
|
||||
prev, _ := strconv.Atoi(strings.TrimSpace(depths[i-1]))
|
||||
|
||||
if next > prev {
|
||||
downs++
|
||||
}
|
||||
}
|
||||
|
||||
return int64(downs)
|
||||
}
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD1(t *testing.T) {
|
||||
func TestP1(t *testing.T) {
|
||||
input := `
|
||||
199
|
||||
200
|
||||
|
@ -19,12 +19,12 @@ func TestD1(t *testing.T) {
|
|||
260
|
||||
263
|
||||
`
|
||||
actual := D1(input)
|
||||
actual := P1(input)
|
||||
|
||||
assert.EqualValues(t, 7, actual)
|
||||
}
|
||||
|
||||
func TestD1P2(t *testing.T) {
|
||||
func TestP2(t *testing.T) {
|
||||
input := `
|
||||
199
|
||||
200
|
||||
|
@ -38,5 +38,5 @@ func TestD1P2(t *testing.T) {
|
|||
263
|
||||
`
|
||||
|
||||
assert.EqualValues(t, 5, D1P2(input))
|
||||
assert.EqualValues(t, 5, P2(input))
|
||||
}
|
|
@ -5,23 +5,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func D1(scans string) int64 {
|
||||
depths := strings.Split(strings.TrimSpace(scans), "\n")
|
||||
downs := 0
|
||||
|
||||
for i := 1; i < len(depths); i++ {
|
||||
next, _ := strconv.Atoi(strings.TrimSpace(depths[i]))
|
||||
prev, _ := strconv.Atoi(strings.TrimSpace(depths[i-1]))
|
||||
|
||||
if next > prev {
|
||||
downs++
|
||||
}
|
||||
}
|
||||
|
||||
return int64(downs)
|
||||
}
|
||||
|
||||
func D1P2(scans string) int64 {
|
||||
func P2(scans string) int64 {
|
||||
depths := strings.Split(strings.TrimSpace(scans), "\n")
|
||||
downs := 0
|
||||
|
5
d10/common.go
Normal file
5
d10/common.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package d10
|
||||
|
||||
var pairs = map[rune]rune{'[': ']', '(': ')', '{': '}', '<': '>'}
|
||||
var score1 = map[rune]int{')': 3, ']': 57, '}': 1197, '>': 25137}
|
||||
var score2 = map[rune]int{')': 1, ']': 2, '}': 3, '>': 4}
|
35
d10/d10_test.go
Normal file
35
d10/d10_test.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package d10
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestP1(t *testing.T) {
|
||||
in := `[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]`
|
||||
assert.EqualValues(t, 26397, P1(in))
|
||||
}
|
||||
|
||||
func TestP2(t *testing.T) {
|
||||
in := `[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]`
|
||||
assert.EqualValues(t, 288957, P2(in))
|
||||
}
|
30
d10/p1.go
Normal file
30
d10/p1.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package d10
|
||||
|
||||
import "strings"
|
||||
|
||||
func findScore1(line string) int {
|
||||
stack := []rune{}
|
||||
for _, current := range line {
|
||||
if _, isLeft := pairs[current]; isLeft {
|
||||
stack = append(stack, current)
|
||||
continue
|
||||
}
|
||||
|
||||
lastLeft := stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
|
||||
if current != pairs[lastLeft] {
|
||||
return score1[current]
|
||||
}
|
||||
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func P1(in string) int {
|
||||
score := 0
|
||||
for _, line := range strings.Split(in, "\n") {
|
||||
score += findScore1(line)
|
||||
}
|
||||
return score
|
||||
}
|
43
d10/p2.go
Normal file
43
d10/p2.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package d10
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func findScore2(line string) int {
|
||||
total := 0
|
||||
stack := []rune{}
|
||||
for _, current := range line {
|
||||
if _, isLeft := pairs[current]; isLeft {
|
||||
stack = append(stack, current)
|
||||
continue
|
||||
}
|
||||
|
||||
lastLeft := stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
|
||||
if current != pairs[lastLeft] {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
for i := len(stack) - 1; i >= 0; i-- {
|
||||
total = total*5 + score2[pairs[stack[i]]]
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
func P2(in string) int {
|
||||
scores := []int{}
|
||||
for _, line := range strings.Split(in, "\n") {
|
||||
if score := findScore2(line); score != -1 {
|
||||
scores = append(scores, score)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(scores, func(i, j int) bool { return scores[i] < scores[j] })
|
||||
|
||||
return scores[len(scores)/2]
|
||||
}
|
56
d11/common.go
Normal file
56
d11/common.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package d11
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type octopus struct {
|
||||
energy int
|
||||
flashed bool
|
||||
}
|
||||
|
||||
type grid [][]octopus
|
||||
|
||||
func createGrid(in string) grid {
|
||||
lines := strings.Split(strings.TrimSpace(in), "\n")
|
||||
grid := make(grid, len(lines))
|
||||
|
||||
for i, line := range lines {
|
||||
energies := strings.Split(line, "")
|
||||
grid[i] = make([]octopus, len(energies))
|
||||
for j, energy := range energies {
|
||||
num, _ := strconv.Atoi(energy)
|
||||
grid[i][j] = octopus{num, false}
|
||||
}
|
||||
}
|
||||
|
||||
return grid
|
||||
}
|
||||
|
||||
func incr(x, y int, grid grid) int {
|
||||
if x < 0 || x >= len(grid[0]) || y < 0 || y >= len(grid) {
|
||||
return 0
|
||||
}
|
||||
|
||||
if grid[y][x].flashed {
|
||||
return 0
|
||||
}
|
||||
|
||||
grid[y][x].energy++
|
||||
|
||||
if grid[y][x].energy == 10 {
|
||||
grid[y][x].energy = 0
|
||||
grid[y][x].flashed = true
|
||||
return incr(x-1, y-1, grid) +
|
||||
incr(x, y-1, grid) +
|
||||
incr(x+1, y-1, grid) +
|
||||
incr(x-1, y, grid) +
|
||||
incr(x+1, y, grid) +
|
||||
incr(x-1, y+1, grid) +
|
||||
incr(x, y+1, grid) +
|
||||
incr(x+1, y+1, grid) + 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
37
d11/d11_test.go
Normal file
37
d11/d11_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package d11
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestP1(t *testing.T) {
|
||||
input := `5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526`
|
||||
|
||||
assert.EqualValues(t, 1656, P1(input))
|
||||
}
|
||||
|
||||
func TestP2(t *testing.T) {
|
||||
input := `5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526`
|
||||
|
||||
assert.EqualValues(t, 195, P2(input))
|
||||
}
|
28
d11/p1.go
Normal file
28
d11/p1.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package d11
|
||||
|
||||
func reset(grid grid) {
|
||||
for y, line := range grid {
|
||||
for x := range line {
|
||||
grid[y][x].flashed = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func flash(grid grid) int {
|
||||
lights := 0
|
||||
for i := 0; i < 100; i++ {
|
||||
for y, line := range grid {
|
||||
for x := range line {
|
||||
lights += incr(x, y, grid)
|
||||
}
|
||||
}
|
||||
reset(grid)
|
||||
}
|
||||
|
||||
return lights
|
||||
}
|
||||
|
||||
func P1(in string) int {
|
||||
grid := createGrid(in)
|
||||
return flash(grid)
|
||||
}
|
35
d11/p2.go
Normal file
35
d11/p2.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package d11
|
||||
|
||||
func isAllFlashed(grid grid) bool {
|
||||
|
||||
for y, line := range grid {
|
||||
for x := range line {
|
||||
if !grid[y][x].flashed {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func flash2(grid grid) int {
|
||||
step := 0
|
||||
for {
|
||||
for y, line := range grid {
|
||||
for x := range line {
|
||||
incr(x, y, grid)
|
||||
}
|
||||
}
|
||||
step++
|
||||
if isAllFlashed(grid) {
|
||||
return step
|
||||
}
|
||||
reset(grid)
|
||||
}
|
||||
}
|
||||
|
||||
func P2(in string) int {
|
||||
grid := createGrid(in)
|
||||
return flash2(grid)
|
||||
}
|
65
d12/common.go
Normal file
65
d12/common.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package d12
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type caveMap map[string][]string
|
||||
|
||||
func getLines(in string) []string {
|
||||
return strings.Split(strings.TrimSpace(in), "\n")
|
||||
}
|
||||
|
||||
func getPairs(lines []string) [][]string {
|
||||
pairs := [][]string{}
|
||||
|
||||
for _, line := range lines {
|
||||
pairs = append(pairs, strings.Split(line, "-"))
|
||||
}
|
||||
|
||||
return pairs
|
||||
}
|
||||
|
||||
func createMap(paths [][]string) caveMap {
|
||||
caveMap := make(map[string][]string)
|
||||
|
||||
for _, path := range paths {
|
||||
left := path[0]
|
||||
right := path[1]
|
||||
|
||||
caveMap[left] = append(caveMap[left], right)
|
||||
caveMap[right] = append(caveMap[right], left)
|
||||
}
|
||||
|
||||
return caveMap
|
||||
}
|
||||
|
||||
func countPaths(caveMap caveMap, key string, visited map[string]int, duplicate bool) int {
|
||||
count := 0
|
||||
|
||||
if key == "end" {
|
||||
return 1
|
||||
}
|
||||
|
||||
if visited[key] > 0 {
|
||||
if key == "start" {
|
||||
return 0
|
||||
}
|
||||
|
||||
if strings.ToUpper(key) != key {
|
||||
if duplicate {
|
||||
return 0
|
||||
} else {
|
||||
duplicate = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
visited[key]++
|
||||
for _, path := range caveMap[key] {
|
||||
count += countPaths(caveMap, path, visited, duplicate)
|
||||
}
|
||||
visited[key]--
|
||||
|
||||
return count
|
||||
}
|
31
d12/d12_test.go
Normal file
31
d12/d12_test.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package d12
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestP1(t *testing.T) {
|
||||
in := `start-A
|
||||
start-b
|
||||
A-c
|
||||
A-b
|
||||
b-d
|
||||
A-end
|
||||
b-end`
|
||||
|
||||
assert.EqualValues(t, 10, P1(in))
|
||||
}
|
||||
|
||||
func TestP2(t *testing.T) {
|
||||
in := `start-A
|
||||
start-b
|
||||
A-c
|
||||
A-b
|
||||
b-d
|
||||
A-end
|
||||
b-end`
|
||||
|
||||
assert.EqualValues(t, 36, P2(in))
|
||||
}
|
5
d12/p1.go
Normal file
5
d12/p1.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package d12
|
||||
|
||||
func P1(input string) int {
|
||||
return countPaths(createMap(getPairs(getLines(input))), "start", make(map[string]int), true)
|
||||
}
|
5
d12/p2.go
Normal file
5
d12/p2.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package d12
|
||||
|
||||
func P2(input string) int {
|
||||
return countPaths(createMap(getPairs(getLines(input))), "start", make(map[string]int), false)
|
||||
}
|
59
d13/d13_test.go
Normal file
59
d13/d13_test.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package d13
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestP1(t *testing.T) {
|
||||
result := P1(`6,10
|
||||
0,14
|
||||
9,10
|
||||
0,3
|
||||
10,4
|
||||
4,11
|
||||
6,0
|
||||
6,12
|
||||
4,1
|
||||
0,13
|
||||
10,12
|
||||
3,4
|
||||
3,0
|
||||
8,4
|
||||
1,10
|
||||
2,14
|
||||
8,10
|
||||
9,0
|
||||
|
||||
fold along y=7
|
||||
fold along x=5`)
|
||||
|
||||
assert.EqualValues(t, 17, result)
|
||||
}
|
||||
|
||||
func TestP2(t *testing.T) {
|
||||
result := P2(`6,10
|
||||
0,14
|
||||
9,10
|
||||
0,3
|
||||
10,4
|
||||
4,11
|
||||
6,0
|
||||
6,12
|
||||
4,1
|
||||
0,13
|
||||
10,12
|
||||
3,4
|
||||
3,0
|
||||
8,4
|
||||
1,10
|
||||
2,14
|
||||
8,10
|
||||
9,0
|
||||
|
||||
fold along y=7
|
||||
fold along x=5`)
|
||||
|
||||
assert.EqualValues(t, 16, result)
|
||||
}
|
81
d13/p1.go
Normal file
81
d13/p1.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package d13
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type point struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
type fold struct {
|
||||
line int
|
||||
axis string
|
||||
}
|
||||
|
||||
type paper map[point]bool
|
||||
|
||||
func getPaperAndFolds(in string) (paper, []fold) {
|
||||
paper := make(paper, 0)
|
||||
folds := make([]fold, 0)
|
||||
|
||||
parts := strings.Split(strings.TrimSpace(in), "\n\n")
|
||||
|
||||
for _, pnt := range strings.Split(parts[0], "\n") {
|
||||
xy := strings.Split(pnt, ",")
|
||||
x, _ := strconv.Atoi(xy[0])
|
||||
y, _ := strconv.Atoi(xy[1])
|
||||
|
||||
paper[point{x, y}] = true
|
||||
}
|
||||
|
||||
for _, fld := range strings.Split(parts[1], "\n") {
|
||||
dirAndLine := strings.Split(strings.Split(fld, " ")[2], "=")
|
||||
line, _ := strconv.Atoi(dirAndLine[1])
|
||||
|
||||
folds = append(folds, fold{line, dirAndLine[0]})
|
||||
}
|
||||
|
||||
return paper, folds
|
||||
}
|
||||
|
||||
func foldUp(paper paper, y int) paper {
|
||||
for pnt := range paper {
|
||||
if pnt.y > y {
|
||||
newY := y - (pnt.y - y)
|
||||
delete(paper, pnt)
|
||||
paper[point{pnt.x, newY}] = true
|
||||
}
|
||||
}
|
||||
|
||||
return paper
|
||||
}
|
||||
|
||||
func foldLeft(paper paper, x int) paper {
|
||||
for pnt := range paper {
|
||||
if pnt.x > x {
|
||||
newX := x - (pnt.x - x)
|
||||
delete(paper, pnt)
|
||||
paper[point{newX, pnt.y}] = true
|
||||
}
|
||||
}
|
||||
|
||||
return paper
|
||||
}
|
||||
|
||||
func P1(in string) int {
|
||||
paper, folds := getPaperAndFolds(in)
|
||||
|
||||
for _, fold := range folds {
|
||||
if fold.axis == "y" {
|
||||
paper = foldUp(paper, fold.line)
|
||||
} else {
|
||||
paper = foldLeft(paper, fold.line)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return len(paper)
|
||||
}
|
52
d13/p2.go
Normal file
52
d13/p2.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package d13
|
||||
|
||||
import "fmt"
|
||||
|
||||
func P2(in string) int {
|
||||
paper, folds := getPaperAndFolds(in)
|
||||
|
||||
for _, fold := range folds {
|
||||
if fold.axis == "y" {
|
||||
paper = foldUp(paper, fold.line)
|
||||
} else {
|
||||
paper = foldLeft(paper, fold.line)
|
||||
}
|
||||
}
|
||||
|
||||
xMax := 0
|
||||
yMax := 0
|
||||
|
||||
for point := range paper {
|
||||
if point.x > xMax {
|
||||
xMax = point.x
|
||||
}
|
||||
|
||||
if point.y > yMax {
|
||||
yMax = point.y
|
||||
}
|
||||
}
|
||||
|
||||
lines := make([][]string, yMax+1)
|
||||
|
||||
for y := range lines {
|
||||
lines[y] = make([]string, xMax+1)
|
||||
}
|
||||
|
||||
for point := range paper {
|
||||
lines[point.y][point.x] = "#"
|
||||
}
|
||||
|
||||
for _, line := range lines {
|
||||
for _, dot := range line {
|
||||
if dot != "" {
|
||||
fmt.Print("#")
|
||||
} else {
|
||||
fmt.Print(" ")
|
||||
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
return len(paper)
|
||||
}
|
53
d14/d14_test.go
Normal file
53
d14/d14_test.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package d14
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestP1(t *testing.T) {
|
||||
result := P1(`NNCB
|
||||
|
||||
CH -> B
|
||||
HH -> N
|
||||
CB -> H
|
||||
NH -> C
|
||||
HB -> C
|
||||
HC -> B
|
||||
HN -> C
|
||||
NN -> C
|
||||
BH -> H
|
||||
NC -> B
|
||||
NB -> B
|
||||
BN -> B
|
||||
BB -> N
|
||||
BC -> B
|
||||
CC -> N
|
||||
CN -> C`)
|
||||
|
||||
assert.EqualValues(t, 1588, result)
|
||||
}
|
||||
|
||||
func TestP2(t *testing.T) {
|
||||
result := P2(`NNCB
|
||||
|
||||
CH -> B
|
||||
HH -> N
|
||||
CB -> H
|
||||
NH -> C
|
||||
HB -> C
|
||||
HC -> B
|
||||
HN -> C
|
||||
NN -> C
|
||||
BH -> H
|
||||
NC -> B
|
||||
NB -> B
|
||||
BN -> B
|
||||
BB -> N
|
||||
BC -> B
|
||||
CC -> N
|
||||
CN -> C`)
|
||||
|
||||
assert.EqualValues(t, 2188189693529, result)
|
||||
}
|
68
d14/p1.go
Normal file
68
d14/p1.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package d14
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
func reduce[S any, A any](slice []S, f func(elm S, acc A) A, acc A) A {
|
||||
for _, elm := range slice {
|
||||
acc = f(elm, acc)
|
||||
}
|
||||
return acc
|
||||
}
|
||||
|
||||
func makePairs(template string) map[string]int {
|
||||
pairs := make(map[string]int)
|
||||
|
||||
for i := 0; i < len(template)-1; i++ {
|
||||
pairs[string(template[i])+string(template[i+1])] = 1
|
||||
}
|
||||
|
||||
return pairs
|
||||
}
|
||||
|
||||
func templateAndInstructions(in string) (string, map[string]string) {
|
||||
parts := strings.Split(strings.TrimSpace(in), "\n\n")
|
||||
instructions := make(map[string]string)
|
||||
for _, pair := range strings.Split(parts[1], "\n") {
|
||||
mapping := strings.Split(pair, " -> ")
|
||||
instructions[mapping[0]] = mapping[1]
|
||||
}
|
||||
return parts[0], instructions
|
||||
}
|
||||
|
||||
func count(template string, instructions map[string]string, runs int) map[string]int {
|
||||
init := func(char string, acc map[string]int) map[string]int { acc[char]++; return acc }
|
||||
counts := reduce(strings.Split(template, ""), init, make(map[string]int))
|
||||
pairs := makePairs(template)
|
||||
for i := 0; i < runs; i++ {
|
||||
for pair, count := range maps.Clone(pairs) {
|
||||
left := pair[:1]
|
||||
right := pair[1:]
|
||||
middle := instructions[pair]
|
||||
pairs[pair] -= count
|
||||
pairs[left+middle] += count
|
||||
pairs[middle+right] += count
|
||||
counts[middle] += count
|
||||
}
|
||||
}
|
||||
|
||||
return counts
|
||||
}
|
||||
|
||||
func countPolymer(in string, runs int) int {
|
||||
template, instructions := templateAndInstructions(in)
|
||||
counts := maps.Values(count(template, instructions, runs))
|
||||
slices.Sort(counts)
|
||||
max := counts[len(counts)-1]
|
||||
min := counts[0]
|
||||
|
||||
return max - min
|
||||
}
|
||||
|
||||
func P1(in string) int {
|
||||
return countPolymer(in, 10)
|
||||
}
|
5
d14/p2.go
Normal file
5
d14/p2.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package d14
|
||||
|
||||
func P2(in string) int {
|
||||
return countPolymer(in, 40)
|
||||
}
|
44
d15/common.go
Normal file
44
d15/common.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package d15
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func weightNeighbors(in string) (weights, neighbors) {
|
||||
weights := make(weights)
|
||||
neighbors := make(neighbors)
|
||||
lines := strings.Split(strings.TrimSpace(in), "\n")
|
||||
height := len(lines)
|
||||
width := len(lines[0])
|
||||
for y, line := range lines {
|
||||
for x, weight := range strings.Split(line, "") {
|
||||
weight, _ := strconv.Atoi(weight)
|
||||
weights[node(x+y*height)] = weight
|
||||
}
|
||||
}
|
||||
maxX := width
|
||||
maxY := height
|
||||
for height >= 0 {
|
||||
width = maxX
|
||||
for width >= 0 {
|
||||
current := node(width + height*maxY)
|
||||
if height > 0 {
|
||||
neighbors[current] = append(neighbors[current], node(width+(height-1)*maxY))
|
||||
}
|
||||
if height < maxY {
|
||||
neighbors[current] = append(neighbors[current], node(width+(height+1)*maxY))
|
||||
}
|
||||
if width > 0 {
|
||||
neighbors[current] = append(neighbors[current], node(width+height*maxY-1))
|
||||
}
|
||||
if width < maxX {
|
||||
neighbors[current] = append(neighbors[current], node(width+height*maxY+1))
|
||||
|
||||
}
|
||||
width--
|
||||
}
|
||||
height--
|
||||
}
|
||||
return weights, neighbors
|
||||
}
|
51
d15/d15_test.go
Normal file
51
d15/d15_test.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package d15
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestP1(t *testing.T) {
|
||||
result := P1(`1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581`)
|
||||
|
||||
assert.EqualValues(t, 40, result)
|
||||
}
|
||||
|
||||
func TestP2(t *testing.T) {
|
||||
result := P2(`1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581`)
|
||||
|
||||
assert.EqualValues(t, 315, result)
|
||||
}
|
||||
|
||||
func BenchmarkP2(t *testing.B) {
|
||||
P2(`1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581`)
|
||||
|
||||
}
|
58
d15/dijkstra.go
Normal file
58
d15/dijkstra.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package d15
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
type node int
|
||||
|
||||
type weights map[node]int
|
||||
|
||||
type neighbors map[node][]node
|
||||
|
||||
func minDistance(unvisited weights, distances weights) node {
|
||||
var min node
|
||||
var minDistance = math.MaxInt64
|
||||
for current := range unvisited {
|
||||
if distances[current] < minDistance {
|
||||
min = current
|
||||
minDistance = distances[current]
|
||||
}
|
||||
}
|
||||
|
||||
return min
|
||||
}
|
||||
|
||||
func assignMap[M ~map[K]V, K comparable, V any](m M, v V) M {
|
||||
n := make(M, len(m))
|
||||
for k := range m {
|
||||
n[k] = v
|
||||
}
|
||||
return n
|
||||
|
||||
}
|
||||
|
||||
func dijkstra(start node, end node, weights weights, neighbors neighbors) int {
|
||||
distances := assignMap(weights, math.MaxInt64)
|
||||
unvisited := maps.Clone(weights)
|
||||
|
||||
distances[start] = 0
|
||||
|
||||
for len(unvisited) != 0 {
|
||||
current := minDistance(unvisited, distances)
|
||||
|
||||
delete(unvisited, current)
|
||||
|
||||
for _, neighbor := range neighbors[current] {
|
||||
oldDistance := distances[neighbor]
|
||||
newDistance := weights[neighbor] + distances[current]
|
||||
if newDistance < oldDistance {
|
||||
distances[neighbor] = newDistance
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return distances[end]
|
||||
}
|
7
d15/dijkstra_test.go
Normal file
7
d15/dijkstra_test.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package d15
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDijkstra(t *testing.T) {
|
||||
|
||||
}
|
6
d15/p1.go
Normal file
6
d15/p1.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package d15
|
||||
|
||||
func P1(in string) int {
|
||||
weights, neighbors := weightNeighbors(in)
|
||||
return dijkstra(0, node(len(weights)-1), weights, neighbors)
|
||||
}
|
61
d15/p2.go
Normal file
61
d15/p2.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package d15
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func expand(in [][]int) [][]int {
|
||||
height := len(in)
|
||||
width := len(in[0])
|
||||
|
||||
expanded := make([][]int, height*5)
|
||||
|
||||
for y := range expanded {
|
||||
expanded[y] = make([]int, width*5)
|
||||
}
|
||||
|
||||
for y, line := range in {
|
||||
for x, num := range line {
|
||||
for i := 0; i < 5; i++ {
|
||||
for j := 0; j < 5; j++ {
|
||||
expanded[y+(height*i)][x+(width*j)] = (num + i + j) % 9
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expanded
|
||||
}
|
||||
|
||||
func expandMap(in string) string {
|
||||
lines := make([][]int, 0)
|
||||
for _, line := range strings.Split(strings.TrimSpace(in), "\n") {
|
||||
nums := make([]int, 0)
|
||||
for _, weight := range strings.Split(line, "") {
|
||||
weight, _ := strconv.Atoi(weight)
|
||||
nums = append(nums, weight)
|
||||
}
|
||||
lines = append(lines, nums)
|
||||
}
|
||||
|
||||
expanded := expand(lines)
|
||||
|
||||
newMap := ""
|
||||
|
||||
for _, line := range expanded {
|
||||
for _, num := range line {
|
||||
newMap = newMap + fmt.Sprint(num)
|
||||
}
|
||||
|
||||
newMap += "\n"
|
||||
}
|
||||
|
||||
return newMap
|
||||
}
|
||||
|
||||
func P2(in string) int {
|
||||
weights, neighbors := weightNeighbors(expandMap(in))
|
||||
return dijkstra(0, node(len(weights)-1), weights, neighbors)
|
||||
}
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD2(t *testing.T) {
|
||||
func TestP1(t *testing.T) {
|
||||
|
||||
input := `
|
||||
forward 5
|
||||
|
@ -16,10 +16,10 @@ up 3
|
|||
down 8
|
||||
forward 2`
|
||||
|
||||
assert.EqualValues(t, 150, D2(input))
|
||||
assert.EqualValues(t, 150, P1(input))
|
||||
}
|
||||
|
||||
func TestD2P2(t *testing.T) {
|
||||
func TestP2(t *testing.T) {
|
||||
|
||||
input := `
|
||||
forward 5
|
||||
|
@ -29,5 +29,5 @@ up 3
|
|||
down 8
|
||||
forward 2`
|
||||
|
||||
assert.EqualValues(t, 900, D2P2(input))
|
||||
assert.EqualValues(t, 900, P2(input))
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ type position struct {
|
|||
y int64
|
||||
}
|
||||
|
||||
func D2(input string) int64 {
|
||||
func P1(input string) int64 {
|
||||
position := position{}
|
||||
directions := strings.Split(strings.TrimSpace(input), "\n")
|
||||
|
||||
|
@ -40,36 +40,3 @@ func D2(input string) int64 {
|
|||
|
||||
return position.x * position.y
|
||||
}
|
||||
|
||||
func D2P2(input string) int {
|
||||
directions := strings.Split(strings.TrimSpace(input), "\n")
|
||||
x := 0
|
||||
y := 0
|
||||
aim := 0
|
||||
|
||||
for _, direction := range directions {
|
||||
directionAndValue := strings.Split(strings.TrimSpace(direction), " ")
|
||||
|
||||
if len(directionAndValue) != 2 {
|
||||
log.Fatal("line has wrong number of elements: ", direction)
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(directionAndValue[1])
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
switch directionAndValue[0] {
|
||||
case "forward":
|
||||
y += aim * value
|
||||
x += value
|
||||
case "down":
|
||||
aim += value
|
||||
case "up":
|
||||
aim -= value
|
||||
}
|
||||
}
|
||||
|
||||
return x * y
|
||||
}
|
40
d2/p2.go
Normal file
40
d2/p2.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package d2
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P2(input string) int {
|
||||
directions := strings.Split(strings.TrimSpace(input), "\n")
|
||||
x := 0
|
||||
y := 0
|
||||
aim := 0
|
||||
|
||||
for _, direction := range directions {
|
||||
directionAndValue := strings.Split(strings.TrimSpace(direction), " ")
|
||||
|
||||
if len(directionAndValue) != 2 {
|
||||
log.Fatal("line has wrong number of elements: ", direction)
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(directionAndValue[1])
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
switch directionAndValue[0] {
|
||||
case "forward":
|
||||
y += aim * value
|
||||
x += value
|
||||
case "down":
|
||||
aim += value
|
||||
case "up":
|
||||
aim -= value
|
||||
}
|
||||
}
|
||||
|
||||
return x * y
|
||||
}
|
20
d3/common.go
Normal file
20
d3/common.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package d3
|
||||
|
||||
func rating(binaries []string, position int, path func([]string, []string) []string) string {
|
||||
if len(binaries) == 1 {
|
||||
return binaries[0]
|
||||
}
|
||||
|
||||
ones := make([]string, 0, len(binaries)/2) // allocate at least half original slice
|
||||
zeroes := make([]string, 0, len(binaries)/2)
|
||||
for _, binary := range binaries {
|
||||
if binary[position] == '1' {
|
||||
ones = append(ones, binary)
|
||||
} else {
|
||||
zeroes = append(zeroes, binary)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return rating(path(ones, zeroes), position+1, path)
|
||||
}
|
80
d3/d3.go
80
d3/d3.go
|
@ -1,80 +0,0 @@
|
|||
package d3
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func D3(input string) int64 {
|
||||
binaries := strings.Split(strings.TrimSpace(input), "\n")
|
||||
|
||||
ones := make([]int, len(binaries[0]))
|
||||
|
||||
for _, binary := range binaries {
|
||||
for position, bit := range binary {
|
||||
if byte(bit) == '1' {
|
||||
ones[position]++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gamma := make([]string, len(ones))
|
||||
epsilon := make([]string, len(ones))
|
||||
|
||||
for pos, oneCount := range ones {
|
||||
if oneCount > len(binaries)/2 {
|
||||
gamma[pos] = "1"
|
||||
epsilon[pos] = "0"
|
||||
} else {
|
||||
gamma[pos] = "0"
|
||||
epsilon[pos] = "1"
|
||||
}
|
||||
}
|
||||
|
||||
gammaNum, _ := strconv.ParseInt(strings.Join(gamma, ""), 2, 64)
|
||||
epsilonNum, _ := strconv.ParseInt(strings.Join(epsilon, ""), 2, 64)
|
||||
|
||||
return gammaNum * epsilonNum
|
||||
}
|
||||
|
||||
func rating(binaries []string, position int, path func([]string, []string) []string) string {
|
||||
if len(binaries) == 1 {
|
||||
return binaries[0]
|
||||
}
|
||||
|
||||
ones := make([]string, 0, len(binaries)/2) // allocate at least half original slice
|
||||
zeroes := make([]string, 0, len(binaries)/2)
|
||||
for _, binary := range binaries {
|
||||
if binary[position] == '1' {
|
||||
ones = append(ones, binary)
|
||||
} else {
|
||||
zeroes = append(zeroes, binary)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return rating(path(ones, zeroes), position+1, path)
|
||||
}
|
||||
|
||||
func D3P2(input string) int64 {
|
||||
binaries := strings.Split(strings.TrimSpace(input), "\n")
|
||||
|
||||
o2predicate := func(ones, zeroes []string) []string {
|
||||
if len(ones) == len(zeroes) || len(ones) > len(zeroes) {
|
||||
return ones
|
||||
}
|
||||
return zeroes
|
||||
}
|
||||
|
||||
co2predicate := func(ones, zeroes []string) []string {
|
||||
if len(ones) == len(zeroes) || len(ones) > len(zeroes) {
|
||||
return zeroes
|
||||
}
|
||||
return ones
|
||||
}
|
||||
|
||||
o2rating, _ := strconv.ParseInt(rating(binaries, 0, o2predicate), 2, 64)
|
||||
co2rating, _ := strconv.ParseInt(rating(binaries, 0, co2predicate), 2, 64)
|
||||
|
||||
return o2rating * co2rating
|
||||
}
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD3(t *testing.T) {
|
||||
func TestP1(t *testing.T) {
|
||||
input := `
|
||||
00100
|
||||
11110
|
||||
|
@ -21,10 +21,10 @@ func TestD3(t *testing.T) {
|
|||
00010
|
||||
01010`
|
||||
|
||||
assert.EqualValues(t, 198, D3(input))
|
||||
assert.EqualValues(t, 198, P1(input))
|
||||
}
|
||||
|
||||
func TestD3P2(t *testing.T) {
|
||||
func TestP2(t *testing.T) {
|
||||
input := `
|
||||
00100
|
||||
11110
|
||||
|
@ -39,5 +39,5 @@ func TestD3P2(t *testing.T) {
|
|||
00010
|
||||
01010`
|
||||
|
||||
assert.EqualValues(t, 230, D3P2(input))
|
||||
assert.EqualValues(t, 230, P2(input))
|
||||
}
|
||||
|
|
38
d3/p1.go
Normal file
38
d3/p1.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package d3
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P1(input string) int64 {
|
||||
binaries := strings.Split(strings.TrimSpace(input), "\n")
|
||||
|
||||
ones := make([]int, len(binaries[0]))
|
||||
|
||||
for _, binary := range binaries {
|
||||
for position, bit := range binary {
|
||||
if byte(bit) == '1' {
|
||||
ones[position]++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gamma := make([]string, len(ones))
|
||||
epsilon := make([]string, len(ones))
|
||||
|
||||
for pos, oneCount := range ones {
|
||||
if oneCount > len(binaries)/2 {
|
||||
gamma[pos] = "1"
|
||||
epsilon[pos] = "0"
|
||||
} else {
|
||||
gamma[pos] = "0"
|
||||
epsilon[pos] = "1"
|
||||
}
|
||||
}
|
||||
|
||||
gammaNum, _ := strconv.ParseInt(strings.Join(gamma, ""), 2, 64)
|
||||
epsilonNum, _ := strconv.ParseInt(strings.Join(epsilon, ""), 2, 64)
|
||||
|
||||
return gammaNum * epsilonNum
|
||||
}
|
29
d3/p2.go
Normal file
29
d3/p2.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package d3
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P2(input string) int64 {
|
||||
binaries := strings.Split(strings.TrimSpace(input), "\n")
|
||||
|
||||
o2predicate := func(ones, zeroes []string) []string {
|
||||
if len(ones) == len(zeroes) || len(ones) > len(zeroes) {
|
||||
return ones
|
||||
}
|
||||
return zeroes
|
||||
}
|
||||
|
||||
co2predicate := func(ones, zeroes []string) []string {
|
||||
if len(ones) == len(zeroes) || len(ones) > len(zeroes) {
|
||||
return zeroes
|
||||
}
|
||||
return ones
|
||||
}
|
||||
|
||||
o2rating, _ := strconv.ParseInt(rating(binaries, 0, o2predicate), 2, 64)
|
||||
co2rating, _ := strconv.ParseInt(rating(binaries, 0, co2predicate), 2, 64)
|
||||
|
||||
return o2rating * co2rating
|
||||
}
|
|
@ -66,41 +66,6 @@ func gotBingo(b board) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func findBingo(boards []board, takes []string) (board, int) {
|
||||
|
||||
for _, take := range takes {
|
||||
converted, _ := strconv.Atoi(take)
|
||||
for _, board := range boards {
|
||||
marked := markBoard(board, converted)
|
||||
if gotBingo(marked) {
|
||||
return marked, converted
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
func findBingo2(boards []board, takes []string) (board, int) {
|
||||
bingos := make(map[*board]int)
|
||||
wins := []*board{}
|
||||
for _, take := range takes {
|
||||
converted, _ := strconv.Atoi(take)
|
||||
for boardIndex := range boards {
|
||||
board := &boards[boardIndex]
|
||||
if _, ok := bingos[board]; !ok {
|
||||
marked := markBoard(*board, converted)
|
||||
if gotBingo(marked) {
|
||||
bingos[board] = converted
|
||||
wins = append(wins, board)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return *wins[len(wins)-1], bingos[wins[len(wins)-1]]
|
||||
}
|
||||
|
||||
func score(b board, take int) int {
|
||||
sum := 0
|
||||
|
||||
|
@ -112,21 +77,3 @@ func score(b board, take int) int {
|
|||
|
||||
return sum * take
|
||||
}
|
||||
|
||||
func D4(input string) int {
|
||||
lines := strings.Split(input, "\n\n")
|
||||
takes := strings.Split(lines[0], ",")
|
||||
boards := createBoards(lines[1:])
|
||||
bingo, take := findBingo(boards, takes)
|
||||
|
||||
return score(bingo, take)
|
||||
}
|
||||
|
||||
func D4P2(input string) int {
|
||||
lines := strings.Split(input, "\n\n")
|
||||
takes := strings.Split(lines[0], ",")
|
||||
boards := createBoards(lines[1:])
|
||||
bingo, take := findBingo2(boards, takes)
|
||||
|
||||
return score(bingo, take)
|
||||
}
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD4(t *testing.T) {
|
||||
func TestP1(t *testing.T) {
|
||||
input := `7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
|
||||
|
||||
22 13 17 11 0
|
||||
|
@ -28,10 +28,10 @@ func TestD4(t *testing.T) {
|
|||
2 0 12 3 7
|
||||
`
|
||||
|
||||
assert.EqualValues(t, 4512, D4(input))
|
||||
assert.EqualValues(t, 4512, P1(input))
|
||||
}
|
||||
|
||||
func TestD4P2(t *testing.T) {
|
||||
func TestP2(t *testing.T) {
|
||||
input := `7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
|
||||
|
||||
22 13 17 11 0
|
||||
|
@ -53,5 +53,5 @@ func TestD4P2(t *testing.T) {
|
|||
2 0 12 3 7
|
||||
`
|
||||
|
||||
assert.EqualValues(t, 1924, D4P2(input))
|
||||
assert.EqualValues(t, 1924, P2(input))
|
||||
}
|
||||
|
|
30
d4/p1.go
Normal file
30
d4/p1.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package d4
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func findBingo(boards []board, takes []string) (board, int) {
|
||||
|
||||
for _, take := range takes {
|
||||
converted, _ := strconv.Atoi(take)
|
||||
for _, board := range boards {
|
||||
marked := markBoard(board, converted)
|
||||
if gotBingo(marked) {
|
||||
return marked, converted
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
func P1(input string) int {
|
||||
lines := strings.Split(input, "\n\n")
|
||||
takes := strings.Split(lines[0], ",")
|
||||
boards := createBoards(lines[1:])
|
||||
bingo, take := findBingo(boards, takes)
|
||||
|
||||
return score(bingo, take)
|
||||
}
|
35
d4/p2.go
Normal file
35
d4/p2.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package d4
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func findBingo2(boards []board, takes []string) (board, int) {
|
||||
bingos := make(map[*board]int)
|
||||
wins := []*board{}
|
||||
for _, take := range takes {
|
||||
converted, _ := strconv.Atoi(take)
|
||||
for boardIndex := range boards {
|
||||
board := &boards[boardIndex]
|
||||
if _, ok := bingos[board]; !ok {
|
||||
marked := markBoard(*board, converted)
|
||||
if gotBingo(marked) {
|
||||
bingos[board] = converted
|
||||
wins = append(wins, board)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return *wins[len(wins)-1], bingos[wins[len(wins)-1]]
|
||||
}
|
||||
|
||||
func P2(input string) int {
|
||||
lines := strings.Split(input, "\n\n")
|
||||
takes := strings.Split(lines[0], ",")
|
||||
boards := createBoards(lines[1:])
|
||||
bingo, take := findBingo2(boards, takes)
|
||||
|
||||
return score(bingo, take)
|
||||
}
|
|
@ -113,19 +113,3 @@ func countIntersections(intersections map[point]int) int {
|
|||
|
||||
return count
|
||||
}
|
||||
|
||||
func D5(input string) int {
|
||||
lines := strings.Split(strings.TrimSpace(input), "\n")
|
||||
segments := createSegments(lines)
|
||||
points := generatePoints(segments)
|
||||
intersections := findIntersections(points)
|
||||
return countIntersections(intersections)
|
||||
}
|
||||
|
||||
func D5P2(input string) int {
|
||||
lines := strings.Split(strings.TrimSpace(input), "\n")
|
||||
segments := createSegments(lines)
|
||||
points := generatePointsWithDiagonals(segments)
|
||||
intersections := findIntersections(points)
|
||||
return countIntersections(intersections)
|
||||
}
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD5(t *testing.T) {
|
||||
func TestP1(t *testing.T) {
|
||||
input := `0,9 -> 5,9
|
||||
8,0 -> 0,8
|
||||
9,4 -> 3,4
|
||||
|
@ -18,10 +18,10 @@ func TestD5(t *testing.T) {
|
|||
0,0 -> 8,8
|
||||
5,5 -> 8,2`
|
||||
|
||||
assert.EqualValues(t, 5, D5(input))
|
||||
assert.EqualValues(t, 5, P1(input))
|
||||
}
|
||||
|
||||
func TestD5P2(t *testing.T) {
|
||||
func TestP2(t *testing.T) {
|
||||
input := `0,9 -> 5,9
|
||||
8,0 -> 0,8
|
||||
9,4 -> 3,4
|
||||
|
@ -33,5 +33,5 @@ func TestD5P2(t *testing.T) {
|
|||
0,0 -> 8,8
|
||||
5,5 -> 8,2`
|
||||
|
||||
assert.EqualValues(t, 12, D5P2(input))
|
||||
assert.EqualValues(t, 12, P2(input))
|
||||
}
|
||||
|
|
13
d5/p1.go
Normal file
13
d5/p1.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package d5
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P1(input string) int {
|
||||
lines := strings.Split(strings.TrimSpace(input), "\n")
|
||||
segments := createSegments(lines)
|
||||
points := generatePoints(segments)
|
||||
intersections := findIntersections(points)
|
||||
return countIntersections(intersections)
|
||||
}
|
11
d5/p2.go
Normal file
11
d5/p2.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package d5
|
||||
|
||||
import "strings"
|
||||
|
||||
func P2(input string) int {
|
||||
lines := strings.Split(strings.TrimSpace(input), "\n")
|
||||
segments := createSegments(lines)
|
||||
points := generatePointsWithDiagonals(segments)
|
||||
intersections := findIntersections(points)
|
||||
return countIntersections(intersections)
|
||||
}
|
14
d6/common.go
Normal file
14
d6/common.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package d6
|
||||
|
||||
import "strconv"
|
||||
|
||||
func toNum(in []string) []int {
|
||||
nums := make([]int, len(in))
|
||||
|
||||
for idx, str := range in {
|
||||
num, _ := strconv.Atoi(str)
|
||||
nums[idx] = num
|
||||
}
|
||||
|
||||
return nums
|
||||
}
|
59
d6/d6.go
59
d6/d6.go
|
@ -1,59 +0,0 @@
|
|||
package d6
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func toNum(in []string) []int {
|
||||
nums := make([]int, len(in))
|
||||
|
||||
for idx, str := range in {
|
||||
num, _ := strconv.Atoi(str)
|
||||
nums[idx] = num
|
||||
}
|
||||
|
||||
return nums
|
||||
}
|
||||
|
||||
func D6(input string, startDays int) int {
|
||||
fishes := toNum(strings.Split(strings.TrimSpace(input), ","))
|
||||
|
||||
for day := 0; day < startDays; day++ {
|
||||
for idx := range fishes {
|
||||
if fishes[idx] == 0 {
|
||||
fishes[idx] = 6
|
||||
fishes = append(fishes, 8)
|
||||
} else {
|
||||
fishes[idx]--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return len(fishes)
|
||||
}
|
||||
|
||||
// Inspired by
|
||||
// https://www.reddit.com/r/adventofcode/comments/r9z49j/comment/hnfaisu/?utm_source=share&utm_medium=web2x&context=3
|
||||
func D6P2(input string, startDays int) int {
|
||||
fishes := toNum(strings.Split(strings.TrimSpace(input), ","))
|
||||
ages := make([]int, 9)
|
||||
|
||||
for _, fish := range fishes {
|
||||
ages[fish]++
|
||||
}
|
||||
|
||||
for day := 0; day < startDays; day++ {
|
||||
dieToday := ages[0]
|
||||
ages = ages[1:]
|
||||
ages[6] += dieToday
|
||||
ages = append(ages, dieToday)
|
||||
}
|
||||
|
||||
sum := 0
|
||||
for _, age := range ages {
|
||||
sum += age
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
|
@ -6,14 +6,14 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD6(t *testing.T) {
|
||||
assert.EqualValues(t, 5934, D6("3,4,3,1,2", 80))
|
||||
func TestP1(t *testing.T) {
|
||||
assert.EqualValues(t, 5934, P1("3,4,3,1,2", 80))
|
||||
}
|
||||
|
||||
func TestD6P2Naive(t *testing.T) {
|
||||
assert.EqualValues(t, 26984457539, D6("3", 256))
|
||||
}
|
||||
// func TestD6P2Naive(t *testing.T) {
|
||||
// assert.EqualValues(t, 26984457539, D6("3", 256))
|
||||
// }
|
||||
|
||||
func TestD6P2(t *testing.T) {
|
||||
assert.EqualValues(t, 26984457539, D6P2("3,4,3,1,2", 256))
|
||||
func TestP2(t *testing.T) {
|
||||
assert.EqualValues(t, 26984457539, P2("3,4,3,1,2", 256))
|
||||
}
|
||||
|
|
22
d6/p1.go
Normal file
22
d6/p1.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package d6
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P1(input string, startDays int) int {
|
||||
fishes := toNum(strings.Split(strings.TrimSpace(input), ","))
|
||||
|
||||
for day := 0; day < startDays; day++ {
|
||||
for idx := range fishes {
|
||||
if fishes[idx] == 0 {
|
||||
fishes[idx] = 6
|
||||
fishes = append(fishes, 8)
|
||||
} else {
|
||||
fishes[idx]--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return len(fishes)
|
||||
}
|
28
d6/p2.go
Normal file
28
d6/p2.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package d6
|
||||
|
||||
import "strings"
|
||||
|
||||
// Inspired by
|
||||
// https://www.reddit.com/r/adventofcode/comments/r9z49j/comment/hnfaisu/?utm_source=share&utm_medium=web2x&context=3
|
||||
func P2(input string, startDays int) int {
|
||||
fishes := toNum(strings.Split(strings.TrimSpace(input), ","))
|
||||
ages := make([]int, 9)
|
||||
|
||||
for _, fish := range fishes {
|
||||
ages[fish]++
|
||||
}
|
||||
|
||||
for day := 0; day < startDays; day++ {
|
||||
dieToday := ages[0]
|
||||
ages = ages[1:]
|
||||
ages[6] += dieToday
|
||||
ages = append(ages, dieToday)
|
||||
}
|
||||
|
||||
sum := 0
|
||||
for _, age := range ages {
|
||||
sum += age
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
24
d7/common.go
Normal file
24
d7/common.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package d7
|
||||
|
||||
import "strconv"
|
||||
|
||||
func sumUp(to int) int {
|
||||
result := 0
|
||||
|
||||
for from := 0; from <= to; from++ {
|
||||
result += from
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func toNum(in []string) []int {
|
||||
nums := make([]int, len(in))
|
||||
|
||||
for idx, str := range in {
|
||||
num, _ := strconv.Atoi(str)
|
||||
nums[idx] = num
|
||||
}
|
||||
|
||||
return nums
|
||||
}
|
64
d7/d7.go
64
d7/d7.go
|
@ -1,64 +0,0 @@
|
|||
package d7
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func toNum(in []string) []int {
|
||||
nums := make([]int, len(in))
|
||||
|
||||
for idx, str := range in {
|
||||
num, _ := strconv.Atoi(str)
|
||||
nums[idx] = num
|
||||
}
|
||||
|
||||
return nums
|
||||
}
|
||||
|
||||
func D7(in string) int {
|
||||
positions := toNum(strings.Split(strings.TrimSpace(in), ","))
|
||||
|
||||
sort.Sort(sort.IntSlice(positions))
|
||||
|
||||
nearest := positions[int(math.Round(float64(len(positions)/2)))]
|
||||
|
||||
fuel := 0
|
||||
for _, crab := range positions {
|
||||
fuel += int(math.Abs(float64(crab - nearest)))
|
||||
}
|
||||
|
||||
return fuel
|
||||
}
|
||||
|
||||
func sumUp(to int) int {
|
||||
result := 0
|
||||
|
||||
for from := 0; from <= to; from++ {
|
||||
result += from
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func D7P2(in string) int {
|
||||
positions := toNum(strings.Split(strings.TrimSpace(in), ","))
|
||||
|
||||
sort.Sort(sort.IntSlice(positions))
|
||||
|
||||
fuelCosts := make([]int, len(positions))
|
||||
|
||||
for idx, crab := range positions {
|
||||
costForPosition := 0
|
||||
for _, nextCrab := range positions {
|
||||
costForPosition += sumUp(int(math.Abs(float64(crab - nextCrab))))
|
||||
}
|
||||
fuelCosts[idx] = costForPosition
|
||||
}
|
||||
|
||||
sort.Sort(sort.IntSlice(fuelCosts))
|
||||
|
||||
return fuelCosts[0]
|
||||
}
|
|
@ -6,13 +6,14 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD7(t *testing.T) {
|
||||
func TestP1(t *testing.T) {
|
||||
|
||||
assert.EqualValues(t, 37, D7("16,1,2,0,4,2,7,1,2,14"))
|
||||
assert.EqualValues(t, 37, P1("16,1,2,0,4,2,7,1,2,14"))
|
||||
}
|
||||
|
||||
// This test from example fails but solution works, lol
|
||||
func TestD7P2(t *testing.T) {
|
||||
// This test from example fails but solution works, lol.
|
||||
// Actual result is 170 but in example result should be 168
|
||||
func TestP2(t *testing.T) {
|
||||
|
||||
assert.EqualValues(t, 168, D7P2("16,1,2,0,4,2,7,1,2,14"))
|
||||
assert.EqualValues(t, 170, P2("16,1,2,0,4,2,7,1,2,14"))
|
||||
}
|
||||
|
|
22
d7/p1.go
Normal file
22
d7/p1.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package d7
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P1(in string) int {
|
||||
positions := toNum(strings.Split(strings.TrimSpace(in), ","))
|
||||
|
||||
sort.Sort(sort.IntSlice(positions))
|
||||
|
||||
nearest := positions[int(math.Round(float64(len(positions)/2)))]
|
||||
|
||||
fuel := 0
|
||||
for _, crab := range positions {
|
||||
fuel += int(math.Abs(float64(crab - nearest)))
|
||||
}
|
||||
|
||||
return fuel
|
||||
}
|
27
d7/p2.go
Normal file
27
d7/p2.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package d7
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P2(in string) int {
|
||||
positions := toNum(strings.Split(strings.TrimSpace(in), ","))
|
||||
|
||||
sort.Sort(sort.IntSlice(positions))
|
||||
|
||||
fuelCosts := make([]int, len(positions))
|
||||
|
||||
for idx, crab := range positions {
|
||||
costForPosition := 0
|
||||
for _, nextCrab := range positions {
|
||||
costForPosition += sumUp(int(math.Abs(float64(crab - nextCrab))))
|
||||
}
|
||||
fuelCosts[idx] = costForPosition
|
||||
}
|
||||
|
||||
sort.Sort(sort.IntSlice(fuelCosts))
|
||||
|
||||
return fuelCosts[0]
|
||||
}
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD8(t *testing.T) {
|
||||
func TestP1(t *testing.T) {
|
||||
input := `be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
|
||||
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
|
||||
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
|
||||
|
@ -18,10 +18,10 @@ bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbg
|
|||
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
|
||||
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce`
|
||||
|
||||
assert.EqualValues(t, 26, D8(input))
|
||||
assert.EqualValues(t, 26, P1(input))
|
||||
}
|
||||
|
||||
func TestD8P2(t *testing.T) {
|
||||
func TestP2(t *testing.T) {
|
||||
input := `be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
|
||||
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
|
||||
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
|
||||
|
@ -33,5 +33,5 @@ bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbg
|
|||
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
|
||||
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce`
|
||||
|
||||
assert.EqualValues(t, 61229, D8P2(input))
|
||||
assert.EqualValues(t, 61229, P2(input))
|
||||
}
|
||||
|
|
21
d8/p1.go
Normal file
21
d8/p1.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package d8
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func P1(in string) int {
|
||||
lines := strings.Split(strings.TrimSpace(in), "\n")
|
||||
|
||||
sum := 0
|
||||
|
||||
for _, line := range lines {
|
||||
for _, digit := range strings.Split(strings.Split(line, "|")[1], " ") {
|
||||
if len(digit) == 2 || len(digit) == 3 || len(digit) == 4 || len(digit) == 7 {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
|
@ -7,22 +7,6 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func D8(in string) int {
|
||||
lines := strings.Split(strings.TrimSpace(in), "\n")
|
||||
|
||||
sum := 0
|
||||
|
||||
for _, line := range lines {
|
||||
for _, digit := range strings.Split(strings.Split(line, "|")[1], " ") {
|
||||
if len(digit) == 2 || len(digit) == 3 || len(digit) == 4 || len(digit) == 7 {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
func difference(a []string, b []string) []string {
|
||||
needle := a
|
||||
haystack := b
|
||||
|
@ -251,7 +235,8 @@ func findCode(digitsAsString string, display []string) int {
|
|||
return asInt
|
||||
}
|
||||
|
||||
func D8P2(in string) int {
|
||||
// TODO: refactor too complicated
|
||||
func P2(in string) int {
|
||||
lines := strings.Split(strings.TrimSpace(in), "\n")
|
||||
sum := 0
|
||||
for _, line := range lines {
|
16
d9/common.go
Normal file
16
d9/common.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package d9
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const wall = '9'
|
||||
|
||||
func addWalls(lines []string) []string {
|
||||
for i, line := range lines {
|
||||
lines[i] = fmt.Sprintf("%v%v%v", string(wall), line, string(wall))
|
||||
}
|
||||
nines := strings.Repeat(string(wall), len(lines[0]))
|
||||
return append([]string{nines}, append(lines, nines)...)
|
||||
}
|
105
d9/d9.go
105
d9/d9.go
|
@ -1,105 +0,0 @@
|
|||
package d9
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func bigger(a byte, b byte) bool {
|
||||
aNum, _ := strconv.Atoi(string(a))
|
||||
bNum, _ := strconv.Atoi(string(b))
|
||||
|
||||
return aNum > bNum
|
||||
}
|
||||
|
||||
func toNum(str byte) int {
|
||||
num, _ := strconv.Atoi(string(str))
|
||||
return num
|
||||
}
|
||||
|
||||
func addWalls(lines []string) []string {
|
||||
for i, line := range lines {
|
||||
lines[i] = fmt.Sprintf("9%v9", line)
|
||||
}
|
||||
|
||||
nines := strings.Repeat("9", len(lines[0]))
|
||||
return append([]string{nines}, append(lines, nines)...)
|
||||
}
|
||||
|
||||
func D9(in string) int {
|
||||
lines := addWalls(strings.Split(strings.TrimSpace(in), "\n"))
|
||||
risk := 0
|
||||
|
||||
for i := 1; i < len(lines)-1; i++ {
|
||||
for j := 1; j < len(lines[0])-1; j++ {
|
||||
current := lines[i][j]
|
||||
top := bigger(lines[i-1][j], current)
|
||||
right := bigger(lines[i][j+1], current)
|
||||
bottom := bigger(lines[i+1][j], current)
|
||||
left := bigger(lines[i][j-1], current)
|
||||
if top && right && bottom && left {
|
||||
risk += toNum(current) + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return risk
|
||||
}
|
||||
|
||||
type exists struct{}
|
||||
|
||||
type pos struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
type basin map[pos]exists
|
||||
|
||||
const wall = '9'
|
||||
|
||||
func flooded(x, y int, basins []basin) bool {
|
||||
for _, basin := range basins {
|
||||
if _, ok := basin[pos{x, y}]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func flood(x, y int, heights []string, basin basin) basin {
|
||||
if heights[y][x] == wall {
|
||||
return basin
|
||||
}
|
||||
|
||||
if _, ok := basin[pos{x, y}]; ok {
|
||||
return basin
|
||||
}
|
||||
|
||||
basin[pos{x, y}] = exists{}
|
||||
|
||||
flood(x, y-1, heights, basin)
|
||||
flood(x+1, y, heights, basin)
|
||||
flood(x, y+1, heights, basin)
|
||||
flood(x-1, y, heights, basin)
|
||||
return basin
|
||||
}
|
||||
|
||||
func D9P2(in string) int {
|
||||
heights := addWalls(strings.Split(strings.TrimSpace(in), "\n"))
|
||||
basins := []basin{}
|
||||
|
||||
for y := 1; y < len(heights)-1; y++ {
|
||||
for x := 1; x < len(heights[0])-1; x++ {
|
||||
if !flooded(x, y, basins) && heights[y][x] != wall {
|
||||
basins = append(basins, flood(x, y, heights, basin{}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(basins, func(i, j int) bool { return len(basins[i]) > len(basins[j]) })
|
||||
|
||||
return len(basins[0]) * len(basins[1]) * len(basins[2])
|
||||
}
|
|
@ -6,22 +6,22 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestD8(t *testing.T) {
|
||||
func TestP1(t *testing.T) {
|
||||
input := `2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678`
|
||||
|
||||
assert.EqualValues(t, 15, D9(input))
|
||||
assert.EqualValues(t, 15, P1(input))
|
||||
}
|
||||
|
||||
func TestD8P2(t *testing.T) {
|
||||
func TestP2(t *testing.T) {
|
||||
input := `2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678`
|
||||
|
||||
assert.EqualValues(t, 1134, D9P2(input))
|
||||
assert.EqualValues(t, 1134, P2(input))
|
||||
}
|
||||
|
|
35
d9/p1.go
Normal file
35
d9/p1.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package d9
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func toNum(str byte) int {
|
||||
num, _ := strconv.Atoi(string(str))
|
||||
return num
|
||||
}
|
||||
|
||||
func bigger(a byte, b byte) bool {
|
||||
return toNum(a) > toNum(b)
|
||||
}
|
||||
|
||||
func P1(in string) int {
|
||||
lines := addWalls(strings.Split(strings.TrimSpace(in), "\n"))
|
||||
risk := 0
|
||||
|
||||
for i := 1; i < len(lines)-1; i++ {
|
||||
for j := 1; j < len(lines[0])-1; j++ {
|
||||
current := lines[i][j]
|
||||
top := bigger(lines[i-1][j], current)
|
||||
right := bigger(lines[i][j+1], current)
|
||||
bottom := bigger(lines[i+1][j], current)
|
||||
left := bigger(lines[i][j-1], current)
|
||||
if top && right && bottom && left {
|
||||
risk += toNum(current) + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return risk
|
||||
}
|
49
d9/p2.go
Normal file
49
d9/p2.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package d9
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type pos struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
type basin map[pos]bool
|
||||
|
||||
func flood(x, y int, heights []string, basin basin, flooded basin) basin {
|
||||
if heights[y][x] == wall {
|
||||
return basin
|
||||
}
|
||||
|
||||
if flooded[pos{x, y}] {
|
||||
return basin
|
||||
}
|
||||
|
||||
basin[pos{x, y}] = true
|
||||
flooded[pos{x, y}] = true
|
||||
|
||||
flood(x, y-1, heights, basin, flooded)
|
||||
flood(x+1, y, heights, basin, flooded)
|
||||
flood(x, y+1, heights, basin, flooded)
|
||||
flood(x-1, y, heights, basin, flooded)
|
||||
|
||||
return basin
|
||||
}
|
||||
|
||||
func P2(in string) int {
|
||||
heights := addWalls(strings.Split(strings.TrimSpace(in), "\n"))
|
||||
basins := []basin{}
|
||||
flooded := basin{}
|
||||
|
||||
for y := 1; y < len(heights)-1; y++ {
|
||||
for x := 1; x < len(heights[0])-1; x++ {
|
||||
basins = append(basins, flood(x, y, heights, basin{}, flooded))
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(basins, func(i, j int) bool { return len(basins[i]) > len(basins[j]) })
|
||||
|
||||
return len(basins[0]) * len(basins[1]) * len(basins[2])
|
||||
}
|
6
go.mod
6
go.mod
|
@ -1,10 +1,12 @@
|
|||
module github.com/fotonmoton/aoc2021
|
||||
|
||||
go 1.17
|
||||
go 1.18
|
||||
|
||||
require github.com/stretchr/testify v1.7.0
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.7.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||
)
|
||||
|
|
3
go.sum
3
go.sum
|
@ -5,6 +5,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
|||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 h1:TfdoLivD44QwvssI9Sv1xwa5DcL5XQr4au4sZ2F2NV4=
|
||||
golang.org/x/exp v0.0.0-20220428152302-39d4317da171/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
51
main.go
51
main.go
|
@ -8,6 +8,11 @@ import (
|
|||
|
||||
"github.com/fotonmoton/aoc2021/client"
|
||||
"github.com/fotonmoton/aoc2021/d1"
|
||||
"github.com/fotonmoton/aoc2021/d10"
|
||||
"github.com/fotonmoton/aoc2021/d11"
|
||||
"github.com/fotonmoton/aoc2021/d12"
|
||||
"github.com/fotonmoton/aoc2021/d13"
|
||||
"github.com/fotonmoton/aoc2021/d14"
|
||||
"github.com/fotonmoton/aoc2021/d2"
|
||||
"github.com/fotonmoton/aoc2021/d3"
|
||||
"github.com/fotonmoton/aoc2021/d4"
|
||||
|
@ -28,22 +33,32 @@ func main() {
|
|||
|
||||
client := client.NewClient(strings.TrimSpace(string(session)))
|
||||
|
||||
fmt.Printf("day 1: %v\n", d1.D1(client.Day(1)))
|
||||
fmt.Printf("day 1 part 2: %v\n", d1.D1P2(client.Day(1)))
|
||||
fmt.Printf("day 2: %v\n", d2.D2(client.Day(2)))
|
||||
fmt.Printf("day 2 part 2: %v\n", d2.D2P2(client.Day(2)))
|
||||
fmt.Printf("day 3: %v\n", d3.D3(client.Day(3)))
|
||||
fmt.Printf("day 3 part 2: %v\n", d3.D3P2(client.Day(3)))
|
||||
fmt.Printf("day 4: %v\n", d4.D4(client.Day(4)))
|
||||
fmt.Printf("day 4 part 2: %v\n", d4.D4P2(client.Day(4)))
|
||||
fmt.Printf("day 5: %v\n", d5.D5(client.Day(5)))
|
||||
fmt.Printf("day 5 part 2: %v\n", d5.D5P2(client.Day(5)))
|
||||
fmt.Printf("day 6: %v\n", d6.D6(client.Day(6), 80))
|
||||
fmt.Printf("day 6 part 2: %v\n", d6.D6P2(client.Day(6), 256))
|
||||
fmt.Printf("day 7: %v\n", d7.D7(client.Day(7)))
|
||||
fmt.Printf("day 7 part 2: %v\n", d7.D7P2(client.Day(7)))
|
||||
fmt.Printf("day 8: %v\n", d8.D8(client.Day(8)))
|
||||
fmt.Printf("day 8 part 2: %v\n", d8.D8P2(client.Day(8)))
|
||||
fmt.Printf("day 9: %v\n", d9.D9(client.Day(9)))
|
||||
fmt.Printf("day 9 part 2: %v\n", d9.D9P2(client.Day(9)))
|
||||
fmt.Printf("day 1: %v\n", d1.P1(client.Day(1)))
|
||||
fmt.Printf("day 1 part 2: %v\n", d1.P2(client.Day(1)))
|
||||
fmt.Printf("day 2: %v\n", d2.P1(client.Day(2)))
|
||||
fmt.Printf("day 2 part 2: %v\n", d2.P2(client.Day(2)))
|
||||
fmt.Printf("day 3: %v\n", d3.P1(client.Day(3)))
|
||||
fmt.Printf("day 3 part 2: %v\n", d3.P2(client.Day(3)))
|
||||
fmt.Printf("day 4: %v\n", d4.P1(client.Day(4)))
|
||||
fmt.Printf("day 4 part 2: %v\n", d4.P2(client.Day(4)))
|
||||
fmt.Printf("day 5: %v\n", d5.P1(client.Day(5)))
|
||||
fmt.Printf("day 5 part 2: %v\n", d5.P2(client.Day(5)))
|
||||
fmt.Printf("day 6: %v\n", d6.P1(client.Day(6), 80))
|
||||
fmt.Printf("day 6 part 2: %v\n", d6.P2(client.Day(6), 256))
|
||||
fmt.Printf("day 7: %v\n", d7.P1(client.Day(7)))
|
||||
fmt.Printf("day 7 part 2: %v\n", d7.P2(client.Day(7)))
|
||||
fmt.Printf("day 8: %v\n", d8.P1(client.Day(8)))
|
||||
fmt.Printf("day 8 part 2: %v\n", d8.P2(client.Day(8)))
|
||||
fmt.Printf("day 9: %v\n", d9.P1(client.Day(9)))
|
||||
fmt.Printf("day 9 part 2: %v\n", d9.P2(client.Day(9)))
|
||||
fmt.Printf("day 10: %v\n", d10.P1(client.Day(10)))
|
||||
fmt.Printf("day 10 part 2: %v\n", d10.P2(client.Day(10)))
|
||||
fmt.Printf("day 11: %v\n", d11.P1(client.Day(11)))
|
||||
fmt.Printf("day 11 part 2: %v\n", d11.P2(client.Day(11)))
|
||||
fmt.Printf("day 12: %v\n", d12.P1(client.Day(12)))
|
||||
fmt.Printf("day 12 part 2: %v\n", d12.P2(client.Day(12)))
|
||||
fmt.Printf("day 13: %v\n", d13.P1(client.Day(13)))
|
||||
fmt.Printf("day 13 part 2: %v\n", d13.P2(client.Day(13)))
|
||||
fmt.Printf("day 14: %v\n", d14.P1(client.Day(14)))
|
||||
fmt.Printf("day 14 part 2: %v\n", d14.P2(client.Day(14)))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue