Skip to content

Commit 5b341d3

Browse files
committed
feat: 2024 day 24
1 parent 5ce48df commit 5b341d3

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

2024/24a.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from aoc import *
2+
3+
4+
def main(infi: str):
5+
inp = filerstrip(infi)
6+
inputs, rules = inp.split('\n\n')
7+
values = {}
8+
for i in inputs.split('\n'):
9+
name, val = i.split(': ')
10+
values[name] = bool(int(val))
11+
rul = []
12+
for r in rules.split('\n'):
13+
match = re.fullmatch(r'^(.*?) (.*?) (.*?) -> (.*?)$', r)
14+
rul.append((match[1], match[2], match[3], match[4]))
15+
changed = True
16+
while changed:
17+
changed = False
18+
for a, oper, b, c in rul:
19+
if c not in values and a in values and b in values:
20+
if oper == 'OR':
21+
values[c] = values[a] or values[b]
22+
elif oper == 'AND':
23+
values[c] = values[a] and values[b]
24+
elif oper == 'XOR':
25+
values[c] = values[a] ^ values[b]
26+
changed = True
27+
aaa = sorted(
28+
((k, c) for k, c in values.items() if k[0] == 'z'),
29+
key=lambda x: x[0],
30+
reverse=True,
31+
)
32+
number = int(''.join(str(int(c)) for _, c in aaa), base=2)
33+
return number
34+
35+
36+
DAY = 24
37+
FILE_TEST = f"{DAY}_testa.txt"
38+
# FILE_TEST = f"{DAY}_testb.txt"
39+
FILE_EXP = f"{DAY}_exp.txt"
40+
FILE = f"{DAY}.txt"
41+
# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY)
42+
# print(main(FILE_TEST))
43+
print(main(FILE))

2024/24b.py

+3-25
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,10 @@
44

55

66
def main(infi: str):
7-
inp = filerstrip(infi)
8-
inputs, rules = inp.split('\n\n')
9-
values = {}
10-
for i in inputs.split('\n'):
11-
name, val = i.split(': ')
12-
values[name] = bool(int(val))
137
rul = []
14-
for r in rules.split('\n'):
8+
for r in filerstrip(infi).split('\n\n')[1].split('\n'):
159
match = re.fullmatch(r'^(.*?) (.*?) (.*?) -> (.*?)$', r)
1610
rul.append((match[1], match[2], match[3], match[4]))
17-
input_ands = {
18-
d
19-
for x, oper, y, d in rul
20-
if oper == 'AND' and x[0] in 'xy' and y[0] in 'xy'
21-
}
22-
other_ands = {d for x, oper, y, d in rul if oper == 'AND'} - input_ands
2311
carry_inputs = set(
2412
flatten([(x, y) for x, oper, y, d in rul if oper == 'OR'])
2513
)
@@ -38,18 +26,8 @@ def main(infi: str):
3826
# match are misplaced. jfb is a special case since that one is used to
3927
# compute carry for the LSB and isn't merged with previous carry, so it's
4028
# correct.
41-
print(
42-
','.join(
43-
sorted(
44-
list(
45-
(
46-
ands ^ carry_inputs
47-
)
48-
- {'jfb'}
49-
)
50-
+ ['z05', 'hdt']
51-
)
52-
)
29+
return ','.join(
30+
sorted(list((ands ^ carry_inputs) - {'jfb'}) + ['z05', 'hdt'])
5331
)
5432

5533

README.org

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| Day | Time | Rank | Score | Time | Rank | Score |
66
|-----+----------+-------+-------+----------+-------+-------|
77
| 25 | 00:22:37 | 2609 | 0 | 00:38:53 | 2948 | 0 |
8+
| 24 | 00:25:02 | 2018 | 0 | 14:29:20 | 7358 | 0 |
89
| 23 | 00:24:26 | 2632 | 0 | 01:09:24 | 2938 | 0 |
910
| 22 | 00:07:06 | 766 | 0 | 05:23:29 | 7143 | 0 |
1011
| 21 | >24h | 15115 | 0 | >24h | 11364 | 0 |

0 commit comments

Comments
 (0)