refactor directory structure

This commit is contained in:
Gregory 2022-02-21 22:28:30 +02:00
parent b17c80d1dd
commit b5444e5aa5
38 changed files with 470 additions and 407 deletions

22
d1/p1.go Normal file
View 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)
}

View file

@ -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))
}

View file

@ -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

View file

@ -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))
}

View file

@ -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
View 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
View 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)
}

View file

@ -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
}

View file

@ -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
View 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
View 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
}

View file

@ -112,21 +112,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)
}

View file

@ -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))
}

14
d4/p1.go Normal file
View file

@ -0,0 +1,14 @@
package d4
import (
"strings"
)
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)
}

12
d4/p2.go Normal file
View file

@ -0,0 +1,12 @@
package d4
import "strings"
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)
}

View file

@ -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)
}

View file

@ -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
View 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
View 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
View 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
}

View file

@ -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
}

View file

@ -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
View 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
View 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
View 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
}

View file

@ -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]
}

View file

@ -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
View 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
View 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]
}

View file

@ -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
View 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
}

View file

@ -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,7 @@ func findCode(digitsAsString string, display []string) int {
return asInt
}
func D8P2(in string) int {
func P2(in string) int {
lines := strings.Split(strings.TrimSpace(in), "\n")
sum := 0
for _, line := range lines {

15
d9/common.go Normal file
View file

@ -0,0 +1,15 @@
package d9
import (
"fmt"
"strings"
)
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)...)
}

View file

@ -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
View 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
}

View file

@ -1,53 +1,10 @@
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 {
@ -87,7 +44,7 @@ func flood(x, y int, heights []string, basin basin) basin {
return basin
}
func D9P2(in string) int {
func P2(in string) int {
heights := addWalls(strings.Split(strings.TrimSpace(in), "\n"))
basins := []basin{}

36
main.go
View file

@ -28,22 +28,22 @@ 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)))
}