-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"embed" | ||
_ "embed" | ||
"fmt" | ||
"github.com/dbut2/advent-of-code/pkg/benchmark" | ||
"github.com/dbut2/advent-of-code/pkg/test" | ||
"github.com/dbut2/advent-of-code/pkg/utils" | ||
"time" | ||
) | ||
|
||
//go:embed input.txt | ||
var input string | ||
|
||
//go:embed test*.txt | ||
var tests embed.FS | ||
|
||
func main() { | ||
t := test.Register(tests, solve) | ||
t.Expect(1, 8) | ||
fmt.Println(solve(input)) | ||
benchmark.Run(func() { | ||
solve(input) | ||
}, benchmark.Time(time.Second)) | ||
} | ||
|
||
func solve(input string) int { | ||
s := utils.ParseInput(input) | ||
|
||
total := 0 | ||
var bufferCount int | ||
var i, j int | ||
var line string | ||
|
||
for i, line = range s { | ||
for j = range line { | ||
if line[j] >= '0' && line[j] <= '9' { | ||
bufferCount *= 10 | ||
bufferCount += int(line[j] - '0') | ||
continue | ||
} | ||
if line[j] == ' ' { | ||
continue | ||
} | ||
switch line[j] { | ||
case 'r': | ||
if bufferCount > 12 { | ||
goto exit | ||
} | ||
case 'g': | ||
if bufferCount > 13 { | ||
goto exit | ||
} | ||
case 'b': | ||
if bufferCount > 14 { | ||
goto exit | ||
} | ||
} | ||
|
||
bufferCount = 0 | ||
} | ||
total += i + 1 | ||
exit: | ||
} | ||
|
||
return total | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"embed" | ||
_ "embed" | ||
"fmt" | ||
"github.com/dbut2/advent-of-code/pkg/benchmark" | ||
"github.com/dbut2/advent-of-code/pkg/test" | ||
"github.com/dbut2/advent-of-code/pkg/utils" | ||
) | ||
|
||
//go:embed input.txt | ||
var input string | ||
|
||
//go:embed test*.txt | ||
var tests embed.FS | ||
|
||
func main() { | ||
t := test.Register(tests, solve) | ||
t.Expect(2, 2286) | ||
fmt.Println(solve(input)) | ||
benchmark.Run(func() { | ||
solve(input) | ||
}, benchmark.Count(1000)) | ||
} | ||
|
||
func solve(input string) int { | ||
s := utils.ParseInput(input) | ||
|
||
total := 0 | ||
var bufferCount int | ||
var j int | ||
var line string | ||
|
||
var redCount, greenCount, blueCount int | ||
|
||
for _, line = range s { | ||
redCount, greenCount, blueCount = 0, 0, 0 | ||
|
||
for j = range line { | ||
if line[j] >= '0' && line[j] <= '9' { | ||
bufferCount *= 10 | ||
bufferCount += int(line[j] - '0') | ||
continue | ||
} | ||
if line[j] == ' ' { | ||
continue | ||
} | ||
switch line[j] { | ||
case 'r': | ||
redCount = max(redCount, bufferCount) | ||
case 'g': | ||
greenCount = max(greenCount, bufferCount) | ||
case 'b': | ||
blueCount = max(blueCount, bufferCount) | ||
} | ||
|
||
bufferCount = 0 | ||
} | ||
|
||
total += redCount * greenCount * blueCount | ||
} | ||
|
||
return total | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters