Skip to content

Commit

Permalink
Implementation for 3b
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 3, 2018
1 parent 38cf35f commit cf9bf4d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aoc2018.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
rootCmd.AddCommand(
day1.A, day1.B,
day2.A, day2.B,
day3.A,
day3.A, day3.B,
)

flags := rootCmd.PersistentFlags()
Expand Down
7 changes: 7 additions & 0 deletions day3/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func intMin(a, b int) int {
}

type claim struct {
id int
top int
left int
width int
Expand All @@ -50,6 +51,12 @@ func parseClaim(line string) *claim {

result := &claim{}

if id, err := strconv.Atoi(strings.TrimLeft(parts[0], "#")); err != nil {
panic(err)
} else {
result.id = id
}

if l, err := strconv.Atoi(tl[0]); err != nil {
panic(err)
} else {
Expand Down
42 changes: 42 additions & 0 deletions day3/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package day3

import (
"fmt"

"github.com/spf13/cobra"

"github.com/nlowe/aoc2018/util"
)

var B = &cobra.Command{
Use: "3b",
Short: "Day 3, Problem B",
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("Answer: %d\n", b(util.ReadInput()))
},
}

func b(input *util.ChallengeInput) int {
var claims []*claim

for line := range input.Lines() {
claims = append(claims, parseClaim(line))
}

search:
for i := 0; i < len(claims); i++ {
for j := 0; j < len(claims); j++ {
if i == j {
continue
}

if claims[i].intersection(claims[j]) != nil {
continue search
}
}

return claims[i].id
}

panic("All claims overlap with at least one other claim")
}
16 changes: 16 additions & 0 deletions day3/b_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package day3

import (
"testing"

"github.com/nlowe/aoc2018/util"
"github.com/stretchr/testify/require"
)

func TestB(t *testing.T) {
input := util.TestInput(`#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2`)

require.Equal(t, 3, b(input))
}

0 comments on commit cf9bf4d

Please sign in to comment.