diff --git a/aoclib/aoclib.go b/aoclib/aoclib.go index 392b05e..71dcf31 100644 --- a/aoclib/aoclib.go +++ b/aoclib/aoclib.go @@ -56,6 +56,25 @@ func (p *Puzzle) GetInputArrayInt() []int { return p.RawInputInt } +func (p *Puzzle) GetInput2DArrayInt() [][]int { + var ret [][]int + for _, v := range p.GetInputArray() { + var thisIntLine []int + for _, c := range v { + value, err := strconv.Atoi(string(c)) + if err != nil { + log.Fatalf("Cannot convert string to int: (%c in %s)", c, v) + } else { + thisIntLine = append(thisIntLine, value) + } + } + + ret = append(ret, thisIntLine) + } + + return ret +} + func (p Puzzle) Solve(partFunction func(p Puzzle) interface{}, day int, part int, timeit bool, timeitNumber int) { var result interface{} if !timeit { diff --git a/day10/day10.go b/day10/day10.go new file mode 100644 index 0000000..f554dc8 --- /dev/null +++ b/day10/day10.go @@ -0,0 +1,43 @@ +package day10 + +import ( + "aoc2015/aoclib" +) + +func lookAndSay(look []int) (say []int) { + for index := 0; index < len(look); { + firstChar := look[index] + nextIndex := index + 1 + if nextIndex == len(look) { + say = append(say, nextIndex-index, firstChar) + } else { + for len(look) > nextIndex && look[nextIndex] == firstChar { + nextIndex++ + } + say = append(say, nextIndex-index, firstChar) + } + index = nextIndex + } + + return say +} + +func Part1(puzzle aoclib.Puzzle) interface{} { + look := puzzle.GetInput2DArrayInt()[0] + + for i := 0; i < 40; i++ { + look = lookAndSay(look) + } + + return len(look) +} + +func Part2(puzzle aoclib.Puzzle) interface{} { + look := puzzle.GetInput2DArrayInt()[0] + + for i := 0; i < 50; i++ { + look = lookAndSay(look) + } + + return len(look) +} diff --git a/inputs/10 b/inputs/10 new file mode 100644 index 0000000..d402c24 --- /dev/null +++ b/inputs/10 @@ -0,0 +1 @@ +3113322113 \ No newline at end of file diff --git a/inputs/10_test b/inputs/10_test new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/inputs/10_test @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/main.go b/main.go index 1764c2d..740ca61 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "aoc2015/day07" "aoc2015/day08" "aoc2015/day09" + "aoc2015/day10" "flag" "fmt" "os" @@ -54,6 +55,9 @@ func initDayFunctions() { dayFunctions[9] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2) dayFunctions[9][1] = day09.Part1 dayFunctions[9][2] = day09.Part2 + dayFunctions[10] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2) + dayFunctions[10][1] = day10.Part1 + dayFunctions[10][2] = day10.Part2 } func execute(thisDay int) {