Skip to content

Commit

Permalink
2024 day 24
Browse files Browse the repository at this point in the history
  • Loading branch information
dbut2 committed Dec 24, 2024
1 parent 929da85 commit a3f73ec
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
79 changes: 79 additions & 0 deletions 2024/24/1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"strings"

"github.com/dbut2/advent-of-code/pkg/harness"
"github.com/dbut2/advent-of-code/pkg/math"
"github.com/dbut2/advent-of-code/pkg/space"
"github.com/dbut2/advent-of-code/pkg/sti"
)

type grid = space.Grid[byte]

func solve(input [2][]string) int {
total := 0

keys := []string{}

values := make(map[string]int)

for _, line := range input[0] {
parts := strings.Split(line, ": ")
values[parts[0]] = sti.Int(parts[1])
keys = append(keys, parts[0])
}

type calc struct {
a, b string
op string
}

calcs := map[string]calc{}
for _, line := range input[1] {
parts := strings.Split(line, " ")
calcs[parts[4]] = calc{a: parts[0], b: parts[2], op: parts[1]}
keys = append(keys, parts[4])
}

var calculate func(key string) int
calculate = func(key string) int {
if v, ok := values[key]; ok {
return v
}

c := calcs[key]
a := calculate(c.a)
b := calculate(c.b)

var out int
switch c.op {
case "AND":
out = a & b
case "OR":
out = a | b
case "XOR":
out = a ^ b
default:
panic("unknown op " + c.op)
}

values[key] = out
return out
}

for _, key := range keys {
if strings.HasPrefix(key, "z") {
n := strings.TrimPrefix(key, "z")
total += calculate(key) * math.Pow(2, sti.Int(n))
}
}

return total
}

func main() {
h := harness.New(solve)
h.Expect(1, 2024)
h.Run()
}
90 changes: 90 additions & 0 deletions 2024/24/2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"slices"
"strings"

"github.com/dbut2/advent-of-code/pkg/harness"
"github.com/dbut2/advent-of-code/pkg/lists"
)

func solve(input [2][]string) string {
type calc struct {
name string
a, b string
op string
}
var calcs []calc
find := func(a, b string, op string) (calc, bool) {
if a > b {
a, b = b, a
}
return lists.Find(calcs, func(calc calc) bool {
return calc.op == op && calc.a == a && calc.b == b
})
}

for _, gate := range input[1] {
parts := strings.Split(gate, " ")
a, b := parts[0], parts[2]
if b < a {
a, b = b, a
}
calcs = append(calcs, calc{
name: parts[4],
a: a,
b: b,
op: parts[1],
})
}

var swapped []string

var carry calc
for i := range 45 {
n := fmt.Sprintf("%02d", i)
xor, _ := find("x"+n, "y"+n, "XOR")
and, _ := find("x"+n, "y"+n, "AND")

if carry.name == "" {
carry = and
continue
}

gate1, ok := find(carry.name, xor.name, "AND")
if !ok {
xor, and = and, xor
swapped = append(swapped, and.name, xor.name)
gate1, _ = find(carry.name, xor.name, "AND")
}

z, ok := find(carry.name, xor.name, "XOR")
if strings.HasPrefix(xor.name, "z") {
xor, z = z, xor
swapped = append(swapped, xor.name, z.name)
}
if strings.HasPrefix(and.name, "z") {
and, z = z, and
swapped = append(swapped, and.name, z.name)
}
if strings.HasPrefix(gate1.name, "z") {
gate1, z = z, gate1
swapped = append(swapped, gate1.name, z.name)
}

carry, _ = find(gate1.name, and.name, "OR")
if strings.HasPrefix(carry.name, "z") {
carry, z = z, carry
swapped = append(swapped, carry.name, z.name)
}
}

slices.Sort(swapped)
return strings.Join(swapped, ",")
}

func main() {
h := harness.New(solve)
h.Run()
}
47 changes: 47 additions & 0 deletions 2024/24/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
x00: 1
x01: 0
x02: 1
x03: 1
x04: 0
y00: 1
y01: 1
y02: 1
y03: 1
y04: 1

ntg XOR fgs -> mjb
y02 OR x01 -> tnw
kwq OR kpj -> z05
x00 OR x03 -> fst
tgd XOR rvg -> z01
vdt OR tnw -> bfw
bfw AND frj -> z10
ffh OR nrd -> bqk
y00 AND y03 -> djm
y03 OR y00 -> psh
bqk OR frj -> z08
tnw OR fst -> frj
gnj AND tgd -> z11
bfw XOR mjb -> z00
x03 OR x00 -> vdt
gnj AND wpb -> z02
x04 AND y00 -> kjc
djm OR pbm -> qhw
nrd AND vdt -> hwm
kjc AND fst -> rvg
y04 OR y02 -> fgs
y01 AND x02 -> pbm
ntg OR kjc -> kwq
psh XOR fgs -> tgd
qhw XOR tgd -> z09
pbm OR djm -> kpj
x03 XOR y03 -> ffh
x00 XOR y04 -> ntg
bfw OR bqk -> z06
nrd XOR fgs -> wpb
frj XOR qhw -> z04
bqk OR frj -> z07
y03 OR x01 -> nrd
hwm AND bqk -> z03
tgd XOR rvg -> z12
tnw OR pbm -> gnj
Empty file added 2024/24/test2.txt
Empty file.

0 comments on commit a3f73ec

Please sign in to comment.