@@ -25,19 +25,19 @@ func (m module) fuel() int {
2525 return f
2626}
2727
28- func calc (in chan input , res chan result , done chan struct {}) {
28+ func calc (in chan module , res chan int , done chan struct {}) {
2929 for i := range in {
30- go fuelForModule (i . m , res , done )
30+ go fuelForModule (i , res , done )
3131 }
3232}
3333
34- func fuelForModule (m module , res chan result , done chan struct {}) {
34+ func fuelForModule (m module , res chan int , done chan struct {}) {
3535 var isDone bool
36+
3637 for ! isDone {
3738 f := m .fuel ()
38- res <- result {
39- fuel : f ,
40- }
39+
40+ res <- f
4141
4242 if f / 3 > 1 {
4343 m .mass = f
@@ -49,15 +49,6 @@ func fuelForModule(m module, res chan result, done chan struct{}) {
4949 done <- struct {}{}
5050}
5151
52- type input struct {
53- m module
54- }
55-
56- type result struct {
57- fuel int
58- err error
59- }
60-
6152func calculate (filepath string ) (int , error ) {
6253 file , err := os .Open (filepath )
6354 if err != nil {
@@ -68,8 +59,8 @@ func calculate(filepath string) (int, error) {
6859
6960 var sum int
7061
71- in := make (chan input )
72- res := make (chan result )
62+ in := make (chan module )
63+ res := make (chan int )
7364 done := make (chan struct {})
7465
7566 go calc (in , res , done )
@@ -87,10 +78,8 @@ func calculate(filepath string) (int, error) {
8778 return 0 , err
8879 }
8980
90- in <- input {
91- module {
92- mass : mass ,
93- },
81+ in <- module {
82+ mass : mass ,
9483 }
9584 lines ++
9685 }
@@ -104,11 +93,7 @@ func calculate(filepath string) (int, error) {
10493 for lines > 0 {
10594 select {
10695 case r := <- res :
107- if r .err != nil {
108- return 0 , err
109- }
110-
111- sum += r .fuel
96+ sum += r
11297 case <- done :
11398 lines --
11499 }
0 commit comments