Skip to content

Commit

Permalink
operator sucks
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Jan 3, 2025
1 parent 3da93c0 commit 7c5c017
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
21 changes: 11 additions & 10 deletions aoc_wim/aoc2015/q16.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
--- Day 16: Aunt Sue ---
https://adventofcode.com/2015/day/16
"""
from collections import defaultdict
from operator import eq
from operator import gt
from operator import lt

from aocd import data


Expand Down Expand Up @@ -34,13 +29,19 @@


def find_sue(**kwargs):
opmap = defaultdict(lambda: eq)
opmap.update(kwargs)
for sue, stats in sues.items():
common_keys = d_message.keys() & stats.keys()
if all(opmap[k](stats[k], d_message[k]) for k in common_keys):
if all(kwargs.get(k, int.__eq__)(stats[k], d_message[k]) for k in common_keys):
return int(sue.split()[1])


print("answer_a:", find_sue())
print("answer_b:", find_sue(cats=gt, trees=gt, pomeranians=lt, goldfish=lt))
a = find_sue()
b = find_sue(
cats=int.__gt__,
trees=int.__gt__,
pomeranians=int.__lt__,
goldfish=int.__lt__,
)

print("answer_a:", a)
print("answer_b:", b)
11 changes: 5 additions & 6 deletions aoc_wim/aoc2022/q21.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
--- Day 21: Monkey Math ---
https://adventofcode.com/2022/day/21
"""
import operator as op

from aocd import data


ops = {
"+": op.add,
"*": op.mul,
"-": op.sub,
"/": op.truediv,
"+": complex.__add__,
"*": complex.__mul__,
"-": complex.__sub__,
"/": complex.__truediv__,
}


Expand Down
4 changes: 2 additions & 2 deletions aoc_wim/aoc2023/q22.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
--- Day 22: Sand Slabs ---
https://adventofcode.com/2023/day/22
"""
import operator as op
from dataclasses import dataclass
from operator import attrgetter

from aocd import data

Expand Down Expand Up @@ -57,7 +57,7 @@ def fall(self):
# data = tdata

bricks = [Brick.fromline(x) for x in data.splitlines()]
bricks.sort(key=op.attrgetter("z0"))
bricks.sort(key=attrgetter("z0"))
space = {}
for i, brick in enumerate(bricks):
brick.id = i
Expand Down
5 changes: 2 additions & 3 deletions aoc_wim/aoc2024/q07.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
https://adventofcode.com/2024/day/7
"""
import itertools as it
import operator as op

from aocd import data

Expand All @@ -15,7 +14,7 @@ def concat(n1, n2):
a = b = 0
for line in data.replace(":", "").splitlines():
result, *ns = map(int, line.split())
for ops in it.product((op.add, op.mul), repeat=len(ns)-1):
for ops in it.product((int.__add__, int.__mul__), repeat=len(ns)-1):
val = ns[0]
for i, f in enumerate(ops, 1):
val = f(val, ns[i])
Expand All @@ -24,7 +23,7 @@ def concat(n1, n2):
b += result
break
else:
for ops in it.product((op.add, op.mul, concat), repeat=len(ns) - 1):
for ops in it.product((int.__add__, int.__mul__, concat), repeat=len(ns) - 1):
val = ns[0]
for i, f in enumerate(ops, 1):
val = f(val, ns[i])
Expand Down

0 comments on commit 7c5c017

Please sign in to comment.