Skip to content

Commit 5c87752

Browse files
authored
Merge pull request #2 from oleg-balunenko/1-day-2dpart
Solve 1 day 2nd part
2 parents 72eb4d8 + 9b1b535 commit 5c87752

File tree

5 files changed

+106
-69
lines changed

5 files changed

+106
-69
lines changed

1-day/spec.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

1-day/fuel.go renamed to puzzles/1-day/fuel.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@ import (
99
)
1010

1111
type module struct {
12-
mass string
12+
mass int
1313
}
1414

15-
func (m module) fuel() (int, error) {
16-
mass, err := strconv.Atoi(m.mass)
17-
if err != nil {
18-
return 0, err
19-
}
15+
func (m module) fuel() int {
16+
mass := m.mass
2017

2118
diff := mass % 3
2219
if diff != 0 {
@@ -25,21 +22,31 @@ func (m module) fuel() (int, error) {
2522

2623
f := (mass / 3) - 2
2724

28-
return f, nil
25+
return f
2926
}
3027

31-
func calc(in chan input, res chan result) {
28+
func calc(in chan input, res chan result, done chan struct{}) {
3229
for i := range in {
33-
go fuelForModule(i.m, res)
30+
go fuelForModule(i.m, res, done)
3431
}
3532
}
3633

37-
func fuelForModule(m module, res chan result) {
38-
f, err := m.fuel()
39-
res <- result{
40-
fuel: f,
41-
err: err,
34+
func fuelForModule(m module, res chan result, done chan struct{}) {
35+
var isDone bool
36+
for !isDone {
37+
f := m.fuel()
38+
res <- result{
39+
fuel: f,
40+
}
41+
42+
if f/3 > 1 {
43+
m.mass = f
44+
} else {
45+
isDone = true
46+
}
4247
}
48+
49+
done <- struct{}{}
4350
}
4451

4552
type input struct {
@@ -63,17 +70,26 @@ func calculate(filepath string) (int, error) {
6370

6471
in := make(chan input)
6572
res := make(chan result)
73+
done := make(chan struct{})
6674

67-
go calc(in, res)
75+
go calc(in, res, done)
6876

69-
var lines int
77+
var (
78+
lines int
79+
mass int
80+
)
7081

7182
scanner := bufio.NewScanner(file)
7283

7384
for scanner.Scan() {
85+
mass, err = strconv.Atoi(scanner.Text())
86+
if err != nil {
87+
return 0, err
88+
}
89+
7490
in <- input{
7591
module{
76-
mass: scanner.Text(),
92+
mass: mass,
7793
},
7894
}
7995
lines++
@@ -86,14 +102,16 @@ func calculate(filepath string) (int, error) {
86102
}
87103

88104
for lines > 0 {
89-
r := <-res
90-
if r.err != nil {
91-
return 0, err
105+
select {
106+
case r := <-res:
107+
if r.err != nil {
108+
return 0, err
109+
}
110+
111+
sum += r.fuel
112+
case <-done:
113+
lines--
92114
}
93-
94-
sum += r.fuel
95-
96-
lines--
97115
}
98116

99117
close(res)
Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,41 @@ import (
1010

1111
func Test_module_fuel(t *testing.T) {
1212
type fields struct {
13-
mass string
13+
mass int
1414
}
1515

1616
tests := []struct {
17-
name string
18-
fields fields
19-
want int
20-
wantErr bool
17+
name string
18+
fields fields
19+
want int
2120
}{
2221
{
2322
name: "mass 12",
2423
fields: fields{
25-
mass: "12",
24+
mass: 12,
2625
},
27-
want: 2,
28-
wantErr: false,
26+
want: 2,
2927
},
3028
{
3129
name: "mass 14",
3230
fields: fields{
33-
mass: "14",
31+
mass: 14,
3432
},
35-
want: 2,
36-
wantErr: false,
33+
want: 2,
3734
},
3835
{
3936
name: "mass 1969",
4037
fields: fields{
41-
mass: "1969",
38+
mass: 1969,
4239
},
43-
want: 654,
44-
wantErr: false,
40+
want: 654,
4541
},
4642
{
4743
name: "mass 100756",
4844
fields: fields{
49-
mass: "100756",
45+
mass: 100756,
5046
},
51-
want: 33583,
52-
wantErr: false,
47+
want: 33583,
5348
},
5449
}
5550

@@ -61,12 +56,7 @@ func Test_module_fuel(t *testing.T) {
6156
mass: tt.fields.mass,
6257
}
6358

64-
got, err := m.fuel()
65-
if tt.wantErr {
66-
require.Error(t, err)
67-
}
68-
69-
require.NoError(t, err)
59+
got := m.fuel()
7060
assert.Equal(t, tt.want, got)
7161
})
7262
}
@@ -88,7 +78,7 @@ func Test_calculate(t *testing.T) {
8878
args: args{
8979
filepath: filepath.Join("testdata", "input.txt"),
9080
},
91-
want: 3464458,
81+
want: 5193796,
9282
wantErr: false,
9383
},
9484
}

puzzles/1-day/spec.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Day 1: The Tyranny of the Rocket Equation
2+
3+
## Part One
4+
5+
Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.
6+
7+
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
8+
9+
The Elves quickly load you into a spacecraft and prepare to launch.
10+
11+
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.
12+
13+
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
14+
15+
For example:
16+
17+
```text
18+
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
19+
20+
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
21+
22+
For a mass of 1969, the fuel required is 654.
23+
24+
For a mass of 100756, the fuel required is 33583.
25+
```
26+
27+
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
28+
29+
What is the sum of the fuel requirements for all of the modules on your spacecraft?
30+
31+
## Part Two
32+
33+
During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.
34+
35+
Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
36+
37+
So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative.
38+
39+
For example:
40+
41+
```text
42+
A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0,
43+
which would call for a negative fuel), so the total fuel required is still just 2.
44+
45+
At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
46+
47+
The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
48+
```
49+
50+
What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)

0 commit comments

Comments
 (0)