package main import ( "fmt" "strings" h "git.bullercodeworks.com/brian/adventofcode/helpers" ) var output []string func main() { inp := h.StdinToStringSlice() part1(inp) part2(inp) } func showLog() { fmt.Println(h.CLEAR_SCREEN) fmt.Println(strings.Join(output, "\n")) } func setLog(v ...string) { output = []string{} log(v...) } func log(v ...string) { output = append(output, v...) } func loadMonkeys(inp []string) map[string]*Monkey { monkeys := make(map[string]*Monkey) for _, v := range inp { if len(v) == 0 { break } m := NewMonkey(strings.Split(v, ": ")...) monkeys[m.name] = m } for _, v := range monkeys { v.init(monkeys, 1) } return monkeys } func part1(inp []string) { monkeys := loadMonkeys(inp) log("# Part 1", fmt.Sprintf("%d", monkeys["root"].Value())) showLog() } func part2(inp []string) { fmt.Println() monkeys := loadMonkeys(inp) m1 := monkeys["root"].inp1 // <- Human is in here m2 := monkeys["root"].inp2 // <- We need to make it match this monkeys["humn"].Value = func() int { return 0 } fmt.Println(m1.Value(), m2.Value()) monkeys["humn"].Value = func() int { return 1000 } fmt.Println(m1.Value(), m2.Value()) //v := monkeys["root"].Value() /* i := 0 var lastV int for { monkeys["humn"].Value = func() int { return i } v := monkeys["root"].Value() if v == 0 { output = append(output[:1], []string{"# Part 2", fmt.Sprintf("Found: %d", i)}...) showLog() return } output = append(output[:3], []string{ "# Part 2", fmt.Sprintf("Scanning... [ humn: %d ]", i), fmt.Sprintf("CURR: %d =? 0", v), fmt.Sprintf("LAST: %d", lastV), }...) setLog(output...) showLog() } */ } type Operation int const ( opAdd = iota opSub opMlt opDiv ) type Monkey struct { name string op Operation inp1, inp2 *Monkey rawValue string Value func() int } func NewMonkey(inp ...string) *Monkey { return &Monkey{name: inp[0], rawValue: inp[1]} } func (m *Monkey) init(monkeys map[string]*Monkey, part int) { pts := strings.Fields(m.rawValue) if len(pts) == 1 { m.Value = func() int { return h.Atoi(pts[0]) } } else { if part == 2 && m.name == "root" { pts[1] = "-" } m.inp1, m.inp2 = monkeys[pts[0]], monkeys[pts[2]] switch pts[1] { case "+": m.Value = func() int { return m.inp1.Value() + m.inp2.Value() } case "-": m.Value = func() int { return m.inp1.Value() - m.inp2.Value() } case "*": m.Value = func() int { return m.inp1.Value() * m.inp2.Value() } case "/": m.Value = func() int { return m.inp1.Value() / m.inp2.Value() } case "=": m.Value = func() int { if m.inp1.Value() == m.inp2.Value() { return 1 } else { return 0 } } } } }