Skip to content

Commit d1e1378

Browse files
committed
Solve year 21/7
1 parent f0043d1 commit d1e1378

File tree

5 files changed

+101
-3
lines changed

5 files changed

+101
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ I am using `embed` and `generics` to solve problems. So at least go `1.18` is ne
2424

2525
I have not shared my inputs as it is [discouraged to share inputs](https://www.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/) on public repository and I respect that. I will purge my past shares soon as I didn't know of info.
2626

27-
## Progress - 76.5 of 200
27+
## Progress - 77.5 of 200
2828

2929
| Day | [2015](year15) | [2016](year16) | [2017](year17) | [2018](year18) | [2019](year19) | [2020](year20) | [2021](year21) | [2022](year22) |
3030
|:---:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
31-
| :star: | 37 | 26 | 15 | 11 | 12 | 18 | 17 | 15 |
31+
| :star: | 37 | 26 | 15 | 11 | 12 | 18 | 19 | 15 |
3232
|1| :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal:| :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
3333
|2| :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
3434
|3| :1st_place_medal: | :1st_place_medal: | | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
3535
|4| :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
3636
|5| :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
3737
|6| :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
38-
|7| :1st_place_medal: | :1st_place_medal: | | :2nd_place_medal: | | :1st_place_medal: | | :1st_place_medal: |
38+
|7| :1st_place_medal: | :1st_place_medal: | | :2nd_place_medal: | | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
3939
|8| :1st_place_medal: | | :1st_place_medal: | | | :1st_place_medal: | | |
4040
|9| :1st_place_medal: | | :1st_place_medal: | | | :1st_place_medal: | | |
4141
|10| :1st_place_medal: | | | | | | :1st_place_medal: | |

year21/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
| 4 | [Giant Squid](https://adventofcode.com/2021/day/4) | :dizzy_face: :dizzy_face: | [year21/day04](/year21/day04) | :octopus: :slot_machine: |
1313
| 5 | [Hydrothermal Venture](https://adventofcode.com/2021/day/5) | :vomiting_face: | [year21/day05](/year21/day05) | :straight_ruler: :triangular_ruler: |
1414
| 6 | [Lanternfish](https://adventofcode.com/2021/day/6) | :star2: | [year21/day06](/year21/day06) | :bucket: :lantern: :heavy_multiplication_x: :fish: |
15+
| 7 | [The Treachery of Whales](https://adventofcode.com/2021/day/7) | :star2: | [year21/day07](/year21/day07) | :crab: :fuelpump: :arrow_left: :arrow_right: |
1516
| 10 | [Syntax Scoring](https://adventofcode.com/2021/day/10) | :star2: | [year21/day10](/year21/day10) | :interrobang: |
1617
| 11 | [Dumbo Octopus](https://adventofcode.com/2021/day/11) | :star2: | [year21/day11](/year21/day11) | :octopus: :flashlight: |
1718
| 25 | [Sea Cucumber](https://adventofcode.com/2021/day/25) | :star2: :star2: | [year21/day25](/year21/day25) | :cucumber: :ocean: |

year21/day07/main.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Package day07 - Solution for Advent of Code 2021/07
2+
// Problem Link: http://adventofcode.com/2021/day/07
3+
package day07
4+
5+
import (
6+
_ "embed"
7+
"fmt"
8+
"github.com/code-shoily/aocgo/algo"
9+
"math"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
//go:embed input.txt
15+
var input string
16+
17+
// Run prints out the result of the solution.
18+
func Run() {
19+
fmt.Println(solve(input))
20+
}
21+
22+
func solve(input string) (int, int) {
23+
data := parse(input)
24+
return solvePart1(data), solvePart2(data)
25+
}
26+
27+
func solvePart1(positions []int) (fuelCost int) {
28+
median := algo.Median(positions)
29+
30+
for _, pos := range positions {
31+
fuelCost += int(math.Abs(float64(pos) - median))
32+
}
33+
34+
return fuelCost
35+
}
36+
37+
func solvePart2(positions []int) (fuelCost int) {
38+
mean := math.Round(algo.Mean(positions))
39+
40+
for _, pos := range positions {
41+
diff := math.Abs(float64(pos)-mean) + 1
42+
fuelCost += int(diff * (diff - 1) / 2)
43+
}
44+
45+
return fuelCost
46+
}
47+
48+
func parse(input string) (positions []int) {
49+
for _, token := range strings.Split(input, ",") {
50+
if position, err := strconv.Atoi(token); err == nil {
51+
positions = append(positions, position)
52+
}
53+
}
54+
55+
return positions
56+
}

year21/day07/main_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package day07
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSolve(t *testing.T) {
8+
solve1, solve2 := solve(input)
9+
expected1, expected2 := 344_138, 94_862_124
10+
11+
if solve1 != expected1 {
12+
t.Errorf("Fail - part 1. Expected %v, got %v", expected1, solve1)
13+
}
14+
15+
if solve2 != expected2 {
16+
t.Errorf("Fail - part 2. Expected %v, got %v", expected2, solve2)
17+
}
18+
}
19+
20+
func BenchmarkSolve(b *testing.B) {
21+
for i := 0; i < b.N; i++ {
22+
solve(input)
23+
}
24+
}
25+
26+
func BenchmarkSolvePart1(b *testing.B) {
27+
data := parse(input)
28+
for i := 0; i < b.N; i++ {
29+
solvePart1(data)
30+
}
31+
}
32+
33+
func BenchmarkSolvePart2(b *testing.B) {
34+
data := parse(input)
35+
for i := 0; i < b.N; i++ {
36+
solvePart2(data)
37+
}
38+
}

year21/runner.go

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/code-shoily/aocgo/year21/day04"
99
"github.com/code-shoily/aocgo/year21/day05"
1010
"github.com/code-shoily/aocgo/year21/day06"
11+
"github.com/code-shoily/aocgo/year21/day07"
1112
"github.com/code-shoily/aocgo/year21/day10"
1213
"github.com/code-shoily/aocgo/year21/day11"
1314
"github.com/code-shoily/aocgo/year21/day25"
@@ -27,6 +28,8 @@ func SolveForDay(day int) {
2728
day05.Run()
2829
case 6:
2930
day06.Run()
31+
case 7:
32+
day07.Run()
3033
case 10:
3134
day10.Run()
3235
case 11:

0 commit comments

Comments
 (0)