diff --git a/client/client.go b/client/day.go similarity index 100% rename from client/client.go rename to client/day.go diff --git a/d1/p1.go b/d1/p1.go new file mode 100644 index 0000000..f753816 --- /dev/null +++ b/d1/p1.go @@ -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) +} diff --git a/d1/d1_test.go b/d1/p1_test.go similarity index 69% rename from d1/d1_test.go rename to d1/p1_test.go index e59682c..471f48a 100644 --- a/d1/d1_test.go +++ b/d1/p1_test.go @@ -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)) } diff --git a/d1/d1.go b/d1/p2.go similarity index 60% rename from d1/d1.go rename to d1/p2.go index c7e1311..5401aef 100644 --- a/d1/d1.go +++ b/d1/p2.go @@ -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 diff --git a/d2/d2_test.go b/d2/d2_test.go index 3564586..7fd7d69 100644 --- a/d2/d2_test.go +++ b/d2/d2_test.go @@ -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)) } diff --git a/d2/d2.go b/d2/p1.go similarity index 52% rename from d2/d2.go rename to d2/p1.go index 47aa4e1..c74f933 100644 --- a/d2/d2.go +++ b/d2/p1.go @@ -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 -} diff --git a/d2/p2.go b/d2/p2.go new file mode 100644 index 0000000..8a4a862 --- /dev/null +++ b/d2/p2.go @@ -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 +} diff --git a/d3/common.go b/d3/common.go new file mode 100644 index 0000000..aedc3bf --- /dev/null +++ b/d3/common.go @@ -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) +} diff --git a/d3/d3.go b/d3/d3.go deleted file mode 100644 index a0d94a7..0000000 --- a/d3/d3.go +++ /dev/null @@ -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 -} diff --git a/d3/d3_test.go b/d3/d3_test.go index 6358365..53a77f8 100644 --- a/d3/d3_test.go +++ b/d3/d3_test.go @@ -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)) } diff --git a/d3/p1.go b/d3/p1.go new file mode 100644 index 0000000..cf50211 --- /dev/null +++ b/d3/p1.go @@ -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 +} diff --git a/d3/p2.go b/d3/p2.go new file mode 100644 index 0000000..6968f15 --- /dev/null +++ b/d3/p2.go @@ -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 +} diff --git a/d4/d4.go b/d4/common.go similarity index 82% rename from d4/d4.go rename to d4/common.go index df19657..0eebd69 100644 --- a/d4/d4.go +++ b/d4/common.go @@ -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) -} diff --git a/d4/d4_test.go b/d4/d4_test.go index aaae8bb..95fe978 100644 --- a/d4/d4_test.go +++ b/d4/d4_test.go @@ -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)) } diff --git a/d4/p1.go b/d4/p1.go new file mode 100644 index 0000000..42ac77d --- /dev/null +++ b/d4/p1.go @@ -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) +} diff --git a/d4/p2.go b/d4/p2.go new file mode 100644 index 0000000..0d4a431 --- /dev/null +++ b/d4/p2.go @@ -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) +} diff --git a/d5/d5.go b/d5/common.go similarity index 81% rename from d5/d5.go rename to d5/common.go index 8d4feb8..2c38e26 100644 --- a/d5/d5.go +++ b/d5/common.go @@ -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) -} diff --git a/d5/d5_test.go b/d5/d5_test.go index 27a10dd..bc2d5fa 100644 --- a/d5/d5_test.go +++ b/d5/d5_test.go @@ -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)) } diff --git a/d5/p1.go b/d5/p1.go new file mode 100644 index 0000000..4648db6 --- /dev/null +++ b/d5/p1.go @@ -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) +} diff --git a/d5/p2.go b/d5/p2.go new file mode 100644 index 0000000..f6771cf --- /dev/null +++ b/d5/p2.go @@ -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) +} diff --git a/d6/common.go b/d6/common.go new file mode 100644 index 0000000..b235471 --- /dev/null +++ b/d6/common.go @@ -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 +} diff --git a/d6/d6.go b/d6/d6.go deleted file mode 100644 index 2c41c7b..0000000 --- a/d6/d6.go +++ /dev/null @@ -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 -} diff --git a/d6/d6_test.go b/d6/d6_test.go index e636cc2..6397da1 100644 --- a/d6/d6_test.go +++ b/d6/d6_test.go @@ -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)) } diff --git a/d6/p1.go b/d6/p1.go new file mode 100644 index 0000000..b9078b3 --- /dev/null +++ b/d6/p1.go @@ -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) +} diff --git a/d6/p2.go b/d6/p2.go new file mode 100644 index 0000000..4390e86 --- /dev/null +++ b/d6/p2.go @@ -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 +} diff --git a/d7/common.go b/d7/common.go new file mode 100644 index 0000000..05df3b0 --- /dev/null +++ b/d7/common.go @@ -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 +} diff --git a/d7/d7.go b/d7/d7.go deleted file mode 100644 index 08d743c..0000000 --- a/d7/d7.go +++ /dev/null @@ -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] -} diff --git a/d7/d7_test.go b/d7/d7_test.go index 7a45621..41c67ab 100644 --- a/d7/d7_test.go +++ b/d7/d7_test.go @@ -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")) } diff --git a/d7/p1.go b/d7/p1.go new file mode 100644 index 0000000..d23ebdd --- /dev/null +++ b/d7/p1.go @@ -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 +} diff --git a/d7/p2.go b/d7/p2.go new file mode 100644 index 0000000..5869319 --- /dev/null +++ b/d7/p2.go @@ -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] +} diff --git a/d8/d8_test.go b/d8/d8_test.go index 9fb850a..a982978 100644 --- a/d8/d8_test.go +++ b/d8/d8_test.go @@ -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)) } diff --git a/d8/p1.go b/d8/p1.go new file mode 100644 index 0000000..df6a3be --- /dev/null +++ b/d8/p1.go @@ -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 +} diff --git a/d8/d8.go b/d8/p2.go similarity index 93% rename from d8/d8.go rename to d8/p2.go index b8ec4ab..935c218 100644 --- a/d8/d8.go +++ b/d8/p2.go @@ -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 { diff --git a/d9/common.go b/d9/common.go new file mode 100644 index 0000000..e7ed252 --- /dev/null +++ b/d9/common.go @@ -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)...) +} diff --git a/d9/d9_test.go b/d9/d9_test.go index 5d1e391..8a11483 100644 --- a/d9/d9_test.go +++ b/d9/d9_test.go @@ -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)) } diff --git a/d9/p1.go b/d9/p1.go new file mode 100644 index 0000000..6a96540 --- /dev/null +++ b/d9/p1.go @@ -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 +} diff --git a/d9/d9.go b/d9/p2.go similarity index 53% rename from d9/d9.go rename to d9/p2.go index 0ce64fd..57088c1 100644 --- a/d9/d9.go +++ b/d9/p2.go @@ -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{} diff --git a/main.go b/main.go index c678ae0..efe73fd 100644 --- a/main.go +++ b/main.go @@ -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))) }