Skip to content

Commit

Permalink
finished day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
devries committed Dec 2, 2023
1 parent 7de5301 commit 8b100a0
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Advent of Code 2023

[![Tests](https://github.com/devries/advent_of_code_2023/actions/workflows/main.yml/badge.svg)](https://github.com/devries/advent_of_code_2023/actions/workflows/main.yml)
[![Stars: 2](https://img.shields.io/badge/⭐_Stars-2-yellow)](https://adventofcode.com/2023)
[![Stars: 4](https://img.shields.io/badge/⭐_Stars-4-yellow)](https://adventofcode.com/2023)

## Plan for This Year

Expand Down Expand Up @@ -37,3 +37,11 @@ opportunity to experiment a bit with code generation.
matches starting at the next character after your previous search and I did
have some indexing issues which I had to debug. I later realized I could use
`strings.LastIndex` to make this much easier.

- [Day 2:Cube Conundrum ](https://adventofcode.com/2023/day/2) - [part 1](day02p1/solution.go), [part 2](day02p2/solution.go)

Most of this problem was parsing. I just did a lot of splitting on substrings.
First I split on ": " to separate the game id from the draws, then I split on
"; " to separate the individual draws, then on ", " to split to the colors.
After that it was straightforward. I missed an opportunity to use the debugger
to check my parsing and instead put in a `Println`.
66 changes: 66 additions & 0 deletions day02p1/solution.go
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
}
37 changes: 37 additions & 0 deletions day02p1/solution_test.go
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)
}
}
}
65 changes: 65 additions & 0 deletions day02p2/solution.go
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
}
37 changes: 37 additions & 0 deletions day02p2/solution_test.go
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)
}
}
}
2 changes: 1 addition & 1 deletion inputs

0 comments on commit 8b100a0

Please sign in to comment.