Skip to content

Commit

Permalink
Implementation for 21b
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 30, 2019
1 parent d1a5e97 commit 91bede8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
61 changes: 61 additions & 0 deletions challenge/day21/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package day21

import (
"fmt"

"github.com/nlowe/aoc2019/intcode"
"github.com/nlowe/aoc2019/intcode/input"
"github.com/nlowe/aoc2019/intcode/output"

"github.com/nlowe/aoc2019/challenge"
"github.com/spf13/cobra"
)

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

const cmdRun = "RUN"

func b(challenge *challenge.Input) int {
in := make(chan string)
cpu, out := intcode.NewCPUForProgram(<-challenge.Lines(), input.ASCIIWrapper(in))

go cpu.Run()

dmg := 0
wg := output.Each(out, func(i int) {
if i < 255 {
fmt.Print(string(rune(i)))
}
dmg = i
})

// The springdroid jumps 4 spaces at t time, so there are only 3 cases to check
// We need to initiate a jump when
// * the very next tile is a hole (!A)
// * AND All Of
// * There is a hole 3 tiles out (jump as early as possible, !C) AND
// * If we jump now we won't fall (D) AND
// * If we jump *again* immediately afterwards, we won't fall (H)
// * OR Both
// * The 2nd tile out is a hole that we couldn't make last turn (so !B) AND
// * The 4th tile is solid (so we have something to land on, so D)
// !A || (!C && D && H) || (!B && D)
in <- "NOT C J"
in <- "AND D J"
in <- "AND H J"
in <- "NOT B T"
in <- "AND D T"
in <- "OR T J"
in <- "NOT A T"
in <- "OR T J"
in <- cmdRun

wg.Wait()
return dmg
}
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"time"

"github.com/nlowe/aoc2019/challenge/day21"

"github.com/nlowe/aoc2019/challenge/day1"
"github.com/nlowe/aoc2019/challenge/day10"
"github.com/nlowe/aoc2019/challenge/day11"
Expand All @@ -19,6 +17,7 @@ import (
"github.com/nlowe/aoc2019/challenge/day19"
"github.com/nlowe/aoc2019/challenge/day2"
"github.com/nlowe/aoc2019/challenge/day20"
"github.com/nlowe/aoc2019/challenge/day21"
"github.com/nlowe/aoc2019/challenge/day3"
"github.com/nlowe/aoc2019/challenge/day4"
"github.com/nlowe/aoc2019/challenge/day5"
Expand Down Expand Up @@ -83,7 +82,7 @@ func init() {
day18.A, day18.B,
day19.A, day19.B,
day20.A, day20.B,
day21.A,
day21.A, day21.B,
)

flags := rootCmd.PersistentFlags()
Expand Down

0 comments on commit 91bede8

Please sign in to comment.