generated from devries/aoc_template
-
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
6 changed files
with
215 additions
and
2 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,66 @@ | ||
package day02p1 | ||
|
||
import ( | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var maxred = 12 | ||
var maxgreen = 13 | ||
var maxblue = 14 | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
sum := 0 | ||
|
||
outer: | ||
for _, ln := range lines { | ||
game := parseGame(ln) | ||
|
||
for _, draw := range game.Draws { | ||
if draw["red"] > maxred || draw["green"] > maxgreen || draw["blue"] > maxblue { | ||
continue outer | ||
} | ||
} | ||
sum += game.Id | ||
} | ||
|
||
return sum | ||
} | ||
|
||
type Game struct { | ||
Id int | ||
Draws []map[string]int | ||
} | ||
|
||
func parseGame(line string) Game { | ||
firstSplit := strings.Split(line, ": ") | ||
|
||
idstrings := strings.Fields(firstSplit[0]) | ||
|
||
result := Game{} | ||
|
||
var err error | ||
result.Id, err = strconv.Atoi(idstrings[1]) | ||
utils.Check(err, "Unable to convert %s to integer", idstrings[1]) | ||
|
||
draws := strings.Split(firstSplit[1], "; ") | ||
|
||
for _, draw := range draws { | ||
drawmap := make(map[string]int) | ||
colorsets := strings.Split(draw, ", ") | ||
for _, colorset := range colorsets { | ||
parts := strings.Fields(colorset) | ||
n, err := strconv.Atoi(parts[0]) | ||
utils.Check(err, "Unable to convert %s to integer", parts[0]) | ||
drawmap[parts[1]] = n | ||
} | ||
result.Draws = append(result.Draws, drawmap) | ||
} | ||
|
||
return result | ||
} |
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,37 @@ | ||
package day02p1 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green | ||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue | ||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red | ||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red | ||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer int | ||
}{ | ||
{testInput, 8}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(int) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
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 day02p2 | ||
|
||
import ( | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
sum := 0 | ||
|
||
for _, ln := range lines { | ||
game := parseGame(ln) | ||
|
||
maximums := make(map[string]int) | ||
for _, draw := range game.Draws { | ||
for k, v := range draw { | ||
if v > maximums[k] { | ||
maximums[k] = v | ||
} | ||
} | ||
} | ||
|
||
sum += maximums["red"] * maximums["green"] * maximums["blue"] | ||
} | ||
|
||
return sum | ||
} | ||
|
||
type Game struct { | ||
Id int | ||
Draws []map[string]int | ||
} | ||
|
||
func parseGame(line string) Game { | ||
firstSplit := strings.Split(line, ": ") | ||
|
||
idstrings := strings.Fields(firstSplit[0]) | ||
|
||
result := Game{} | ||
|
||
var err error | ||
result.Id, err = strconv.Atoi(idstrings[1]) | ||
utils.Check(err, "Unable to convert %s to integer", idstrings[1]) | ||
|
||
draws := strings.Split(firstSplit[1], "; ") | ||
|
||
for _, draw := range draws { | ||
drawmap := make(map[string]int) | ||
colorsets := strings.Split(draw, ", ") | ||
for _, colorset := range colorsets { | ||
parts := strings.Fields(colorset) | ||
n, err := strconv.Atoi(parts[0]) | ||
utils.Check(err, "Unable to convert %s to integer", parts[0]) | ||
drawmap[parts[1]] = n | ||
} | ||
result.Draws = append(result.Draws, drawmap) | ||
} | ||
|
||
return result | ||
} |
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,37 @@ | ||
package day02p2 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green | ||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue | ||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red | ||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red | ||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer int | ||
}{ | ||
{testInput, 2286}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(int) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
Submodule inputs
updated
from 3d7675 to 7b4edb