File tree 5 files changed +101
-3
lines changed
5 files changed +101
-3
lines changed Original file line number Diff line number Diff line change @@ -24,18 +24,18 @@ I am using `embed` and `generics` to solve problems. So at least go `1.18` is ne
24
24
25
25
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.
26
26
27
- ## Progress - 76 .5 of 200
27
+ ## Progress - 77 .5 of 200
28
28
29
29
| Day | [ 2015] ( year15 ) | [ 2016] ( year16 ) | [ 2017] ( year17 ) | [ 2018] ( year18 ) | [ 2019] ( year19 ) | [ 2020] ( year20 ) | [ 2021] ( year21 ) | [ 2022] ( year22 ) |
30
30
| :---:| :-:| :-:| :-:| :-:| :-:| :-:| :-:| :-:|
31
- | :star : | 37 | 26 | 15 | 11 | 12 | 18 | 17 | 15 |
31
+ | :star : | 37 | 26 | 15 | 11 | 12 | 18 | 19 | 15 |
32
32
| 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 : |
33
33
| 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 : |
34
34
| 3| :1st_place_medal : | :1st_place_medal : | | :1st_place_medal : | :1st_place_medal : | :1st_place_medal : | :1st_place_medal : | :1st_place_medal : |
35
35
| 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 : |
36
36
| 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 : |
37
37
| 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 : |
39
39
| 8| :1st_place_medal : | | :1st_place_medal : | | | :1st_place_medal : | | |
40
40
| 9| :1st_place_medal : | | :1st_place_medal : | | | :1st_place_medal : | | |
41
41
| 10| :1st_place_medal : | | | | | | :1st_place_medal : | |
Original file line number Diff line number Diff line change 12
12
| 4 | [ Giant Squid] ( https://adventofcode.com/2021/day/4 ) | :dizzy_face : :dizzy_face : | [ year21/day04] ( /year21/day04 ) | :octopus : :slot_machine : |
13
13
| 5 | [ Hydrothermal Venture] ( https://adventofcode.com/2021/day/5 ) | :vomiting_face : | [ year21/day05] ( /year21/day05 ) | :straight_ruler : :triangular_ruler : |
14
14
| 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 : |
15
16
| 10 | [ Syntax Scoring] ( https://adventofcode.com/2021/day/10 ) | :star2 : | [ year21/day10] ( /year21/day10 ) | :interrobang : |
16
17
| 11 | [ Dumbo Octopus] ( https://adventofcode.com/2021/day/11 ) | :star2 : | [ year21/day11] ( /year21/day11 ) | :octopus : :flashlight : |
17
18
| 25 | [ Sea Cucumber] ( https://adventofcode.com/2021/day/25 ) | :star2 : :star2 : | [ year21/day25] ( /year21/day25 ) | :cucumber : :ocean : |
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 8
8
"github.com/code-shoily/aocgo/year21/day04"
9
9
"github.com/code-shoily/aocgo/year21/day05"
10
10
"github.com/code-shoily/aocgo/year21/day06"
11
+ "github.com/code-shoily/aocgo/year21/day07"
11
12
"github.com/code-shoily/aocgo/year21/day10"
12
13
"github.com/code-shoily/aocgo/year21/day11"
13
14
"github.com/code-shoily/aocgo/year21/day25"
@@ -27,6 +28,8 @@ func SolveForDay(day int) {
27
28
day05 .Run ()
28
29
case 6 :
29
30
day06 .Run ()
31
+ case 7 :
32
+ day07 .Run ()
30
33
case 10 :
31
34
day10 .Run ()
32
35
case 11 :
You can’t perform that action at this time.
0 commit comments