Skip to content

Commit

Permalink
Implementation for 5A
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 5, 2018
1 parent 639a5c6 commit c5fdcb6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aoc2018.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"time"

"github.com/nlowe/aoc2018/day5"

"github.com/nlowe/aoc2018/day4"

"github.com/nlowe/aoc2018/day3"
Expand Down Expand Up @@ -43,6 +45,7 @@ func main() {
day2.A, day2.B,
day3.A, day3.B,
day4.A, day4.B,
day5.A,
)

flags := rootCmd.PersistentFlags()
Expand Down
52 changes: 52 additions & 0 deletions day5/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package day5

import (
"fmt"
"strings"

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

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

const delta = 'a' - 'A'

func intAbs(a int32) int32 {
return (a + (a >> 31)) ^ (a >> 31)
}

func a(input *util.ChallengeInput) int {
polymer := <-input.Lines()

for {
buff := strings.Builder{}
reduced := false

for i := 0; i < len(polymer)-1; i++ {
if intAbs(rune(polymer[i])-rune(polymer[i+1])) == delta {
i++
reduced = true
} else {
buff.WriteRune(rune(polymer[i]))

if i == len(polymer)-2 {
buff.WriteRune(rune(polymer[i+1]))
}
}
}

polymer = buff.String()
if !reduced {
break
}
}

return len(polymer)
}
14 changes: 14 additions & 0 deletions day5/a_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package day5

import (
"testing"

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

func TestA(t *testing.T) {
input := util.TestInput(`dabAcCaCBAcCcaDA`)

require.Equal(t, 10, a(input))
}
1 change: 1 addition & 0 deletions day5/input.txt

Large diffs are not rendered by default.

0 comments on commit c5fdcb6

Please sign in to comment.