2022 Day 23 Complete
This commit is contained in:
2643
2022/day21/input
Normal file
2643
2022/day21/input
Normal file
File diff suppressed because it is too large
Load Diff
143
2022/day21/main.go
Normal file
143
2022/day21/main.go
Normal file
@@ -0,0 +1,143 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
102
2022/day21/problem
Normal file
102
2022/day21/problem
Normal file
@@ -0,0 +1,102 @@
|
||||
Advent of Code
|
||||
|
||||
• [About]
|
||||
• [Events]
|
||||
• [Shop]
|
||||
• [Settings]
|
||||
• [Log Out]
|
||||
|
||||
br0xen (AoC++) 35*
|
||||
|
||||
/*2022*/
|
||||
|
||||
• [Calendar]
|
||||
• [AoC++]
|
||||
• [Sponsors]
|
||||
• [Leaderboard]
|
||||
• [Stats]
|
||||
|
||||
Our sponsors help make Advent of Code possible:
|
||||
Teradyne - Do you like coding algorithms where milliseconds matter? What about nanoseconds?
|
||||
|
||||
--- Day 21: Monkey Math ---
|
||||
|
||||
The monkeys are back! You're worried they're going to try to steal your stuff again, but it seems like they're just
|
||||
holding their ground and making various monkey noises at you.
|
||||
|
||||
Eventually, one of the elephants realizes you don't speak monkey and comes over to interpret. As it turns out, they
|
||||
overheard you talking about trying to find the grove; they can show you a shortcut if you answer their riddle.
|
||||
|
||||
Each monkey is given a job: either to yell a specific number or to yell the result of a math operation. All of the
|
||||
number-yelling monkeys know their number from the start; however, the math operation monkeys need to wait for two other
|
||||
monkeys to yell a number, and those two other monkeys might also be waiting on other monkeys.
|
||||
|
||||
Your job is to work out the number the monkey named root will yell before the monkeys figure it out themselves.
|
||||
|
||||
For example:
|
||||
|
||||
root: pppw + sjmn
|
||||
dbpl: 5
|
||||
cczh: sllz + lgvd
|
||||
zczc: 2
|
||||
ptdq: humn - dvpt
|
||||
dvpt: 3
|
||||
lfqf: 4
|
||||
humn: 5
|
||||
ljgn: 2
|
||||
sjmn: drzm * dbpl
|
||||
sllz: 4
|
||||
pppw: cczh / lfqf
|
||||
lgvd: ljgn * ptdq
|
||||
drzm: hmdt - zczc
|
||||
hmdt: 32
|
||||
|
||||
Each line contains the name of a monkey, a colon, and then the job of that monkey:
|
||||
|
||||
• A lone number means the monkey's job is simply to yell that number.
|
||||
• A job like aaaa + bbbb means the monkey waits for monkeys aaaa and bbbb to yell each of their numbers; the monkey
|
||||
then yells the sum of those two numbers.
|
||||
• aaaa - bbbb means the monkey yells aaaa's number minus bbbb's number.
|
||||
• Job aaaa * bbbb will yell aaaa's number multiplied by bbbb's number.
|
||||
• Job aaaa / bbbb will yell aaaa's number divided by bbbb's number.
|
||||
|
||||
So, in the above example, monkey drzm has to wait for monkeys hmdt and zczc to yell their numbers. Fortunately, both
|
||||
hmdt and zczc have jobs that involve simply yelling a single number, so they do this immediately: 32 and 2. Monkey drzm
|
||||
can then yell its number by finding 32 minus 2: 30.
|
||||
|
||||
Then, monkey sjmn has one of its numbers (30, from monkey drzm), and already has its other number, 5, from dbpl. This
|
||||
allows it to yell its own number by finding 30 multiplied by 5: 150.
|
||||
|
||||
This process continues until root yells a number: 152.
|
||||
|
||||
However, your actual situation involves considerably more monkeys. What number will the monkey named root yell?
|
||||
|
||||
To begin, get your puzzle input.
|
||||
|
||||
Answer: _____________________ [ [Submit] ]
|
||||
|
||||
You can also [Shareon Twitter Mastodon] this puzzle.
|
||||
|
||||
References
|
||||
|
||||
Visible links
|
||||
. https://adventofcode.com/
|
||||
. https://adventofcode.com/2022/about
|
||||
. https://adventofcode.com/2022/events
|
||||
. https://teespring.com/stores/advent-of-code
|
||||
. https://adventofcode.com/2022/settings
|
||||
. https://adventofcode.com/2022/auth/logout
|
||||
. Advent of Code Supporter
|
||||
https://adventofcode.com/2022/support
|
||||
. https://adventofcode.com/2022
|
||||
. https://adventofcode.com/2022
|
||||
. https://adventofcode.com/2022/support
|
||||
. https://adventofcode.com/2022/sponsors
|
||||
. https://adventofcode.com/2022/leaderboard
|
||||
. https://adventofcode.com/2022/stats
|
||||
. https://adventofcode.com/2022/sponsors
|
||||
. https://jobs.teradyne.com/?utm_source=adventofcode&utm_medium=ad&utm_campaign=2022
|
||||
. https://adventofcode.com/2022/day/11
|
||||
. https://adventofcode.com/2022/day/21/input
|
||||
. https://twitter.com/intent/tweet?text=%22Monkey+Math%22+%2D+Day+21+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F21&related=ericwastl&hashtags=AdventOfCode
|
||||
. javascript:void(0);
|
15
2022/day21/testinput
Normal file
15
2022/day21/testinput
Normal file
@@ -0,0 +1,15 @@
|
||||
root: pppw + sjmn
|
||||
dbpl: 5
|
||||
cczh: sllz + lgvd
|
||||
zczc: 2
|
||||
ptdq: humn - dvpt
|
||||
dvpt: 3
|
||||
lfqf: 4
|
||||
humn: 5
|
||||
ljgn: 2
|
||||
sjmn: drzm * dbpl
|
||||
sllz: 4
|
||||
pppw: cczh / lfqf
|
||||
lgvd: ljgn * ptdq
|
||||
drzm: hmdt - zczc
|
||||
hmdt: 32
|
Reference in New Issue
Block a user