Skip to content

Commit c319279

Browse files
committed
add 2023 AOC day 4 and 5
1 parent c02d2a4 commit c319279

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from collections import defaultdict, deque, Counter
2+
# d = deque()
3+
# d.append(5)
4+
# x = d.popleft()
5+
import re
6+
# m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
7+
# # or re.search
8+
# >>> m.group(0) # The entire match
9+
# 'Isaac Newton'
10+
# >>> m.group(1) # The first parenthesized subgroup.
11+
# 'Isaac'
12+
# >>> m.group(2) # The second parenthesized subgroup.
13+
# 'Newton'
14+
# >>> m.group(1, 2) # Multiple arguments give us a tuple.
15+
# ('Isaac', 'Newton')
16+
from heapq import heappush, heappop
17+
# >>> heap = []
18+
# >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
19+
# >>> for item in data:
20+
# ... heappush(heap, item)
21+
# heap[0] is the smallest item
22+
import string
23+
# string.ascii_lowercase == 'abcde...'
24+
# string.ascii_uppercase == 'ABCDE...'
25+
from functools import lru_cache
26+
# @lru_cache(maxsize=None)
27+
28+
import sys
29+
30+
sys.setrecursionlimit(100000)
31+
32+
def get_ints(s):
33+
return list(map(int, re.findall(r"-?\d+", s))) # copied from mcpower from mserrano on betaveros' recommendation
34+
dirs = [(0,1), (1,0), (0,-1), (-1,0)]
35+
directions = 'RDLU'
36+
octs = [(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)]
37+
def is_grid_valid(n,m, r,c,):
38+
return (0<=r<n) and (0<=c<m)
39+
def sign_of(x):
40+
if x==0:
41+
return 0
42+
return x/abs(x)
43+
44+
if False:
45+
ans = 0
46+
inps = []
47+
while True:
48+
try:
49+
inps.append(input())
50+
except EOFError:
51+
break
52+
for line in inps:
53+
xs = get_ints(line)
54+
win = xs[1:11]
55+
me = xs[11:]
56+
x = 1
57+
for m in me:
58+
if m in win:
59+
x *= 2
60+
ans += x//2
61+
62+
print(ans)
63+
else:
64+
ans = 0
65+
inps = []
66+
while True:
67+
try:
68+
inps.append(input())
69+
except EOFError:
70+
break
71+
dp = [1 for i in range(len(inps))]
72+
N = 11
73+
for r in reversed(range(len(inps))):
74+
line = inps[r]
75+
xs = get_ints(line)
76+
win = xs[1:N]
77+
me = xs[N:]
78+
x = 0
79+
for m in me:
80+
if m in win:
81+
x += 1
82+
for i in range(x):
83+
dp[r] += dp[r+i+1]
84+
ans += dp[r]
85+
86+
print(ans)
87+
pass
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from collections import defaultdict, deque, Counter
2+
# d = deque()
3+
# d.append(5)
4+
# x = d.popleft()
5+
import re
6+
# m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
7+
# # or re.search
8+
# >>> m.group(0) # The entire match
9+
# 'Isaac Newton'
10+
# >>> m.group(1) # The first parenthesized subgroup.
11+
# 'Isaac'
12+
# >>> m.group(2) # The second parenthesized subgroup.
13+
# 'Newton'
14+
# >>> m.group(1, 2) # Multiple arguments give us a tuple.
15+
# ('Isaac', 'Newton')
16+
from heapq import heappush, heappop
17+
# >>> heap = []
18+
# >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
19+
# >>> for item in data:
20+
# ... heappush(heap, item)
21+
# heap[0] is the smallest item
22+
import string
23+
# string.ascii_lowercase == 'abcde...'
24+
# string.ascii_uppercase == 'ABCDE...'
25+
from functools import lru_cache
26+
# @lru_cache(maxsize=None)
27+
28+
import sys
29+
30+
sys.setrecursionlimit(100000)
31+
32+
def get_ints(s):
33+
return list(map(int, re.findall(r"-?\d+", s))) # copied from mcpower from mserrano on betaveros' recommendation
34+
dirs = [(0,1), (1,0), (0,-1), (-1,0)]
35+
directions = 'RDLU'
36+
octs = [(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)]
37+
def is_grid_valid(n,m, r,c,):
38+
return (0<=r<n) and (0<=c<m)
39+
def sign_of(x):
40+
if x==0:
41+
return 0
42+
return x/abs(x)
43+
44+
if False:
45+
ans = 0
46+
seeds = get_ints(input())
47+
next_seeds = seeds[:]
48+
inps = []
49+
maps = []
50+
51+
while True:
52+
try:
53+
i = input()
54+
if i.strip() == '':
55+
seeds = next_seeds
56+
next_seeds = seeds[:]
57+
continue
58+
59+
if i.split()[-1] == 'map:':
60+
continue
61+
d, s, l = get_ints(i)
62+
for j, seed in enumerate(seeds):
63+
if s <= seed < s+l:
64+
next_seeds[j] = d + seed-s
65+
except EOFError:
66+
break
67+
68+
print(min(seeds))
69+
else:
70+
pass
71+
ans = 0
72+
seeds = get_ints(input())
73+
ranges = []
74+
for i in range(0, len(seeds), 2):
75+
ranges.append((seeds[i], seeds[i] + seeds[i+1]-1))
76+
next_ranges = []
77+
while True:
78+
try:
79+
i = input()
80+
print(ranges, next_ranges)
81+
print(i)
82+
if i.strip() == '':
83+
ranges += next_ranges
84+
next_ranges = []
85+
continue
86+
if i.split()[-1] == 'map:':
87+
continue
88+
d, s, l = get_ints(i)
89+
temp_ranges = []
90+
for j, (left, right) in enumerate(ranges):
91+
if right < s or left >= s+l:
92+
temp_ranges.append((left, right))
93+
continue
94+
source_start = max(left, s)
95+
source_end = min(right, s+l-1)
96+
next_ranges.append((d + source_start-s, d + source_end-s))
97+
if source_start > left:
98+
temp_ranges.append((left, source_start-1))
99+
if source_end < right:
100+
temp_ranges.append((source_end+1, right))
101+
ranges = temp_ranges
102+
except EOFError:
103+
break
104+
105+
106+
print(min(ranges))

0 commit comments

Comments
 (0)