Skip to content

Commit 52ba3e3

Browse files
committed
2 parents 62fe200 + 9ea27b7 commit 52ba3e3

File tree

8 files changed

+322
-5
lines changed

8 files changed

+322
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ config.py
33
*/inputs/*.txt
44
Completed.md
55
*.dump.*
6-
debug.html
6+
debug.html
7+
2024/14

2024/12.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
def parse(data):
5+
grid = {}
6+
bounds = len(data.split('\n')[0]), len(data.split('\n'))
7+
for y, line in enumerate(data.split('\n')):
8+
for x, char in enumerate(line):
9+
grid[(x, y)] = char
10+
11+
return grid, bounds
12+
13+
split_data = parse
14+
completed = 1
15+
raw_data = None # Not To be touched
16+
17+
def part1(data):
18+
grid, bounds = data
19+
price = 0
20+
21+
def fencing(anchor):
22+
x, y = anchor
23+
area = 1
24+
perimeter = 0
25+
patch = set([(x, y)])
26+
crop = grid[anchor]
27+
movement = [(1, 0), (-1, 0), (0, 1), (0, -1)]
28+
queue = [anchor]
29+
30+
while queue:
31+
x, y = queue.pop(0)
32+
for dx, dy in movement:
33+
nx, ny = x + dx, y + dy
34+
if not (0 <= nx < bounds[0] and 0 <= ny < bounds[1]):
35+
perimeter += 1 # The border at the edge of the map
36+
elif (nx, ny) in patch:
37+
continue # Already seen this
38+
elif grid[(nx, ny)] == crop:
39+
area += 1
40+
queue.append((nx, ny))
41+
patch.add((nx, ny))
42+
else:
43+
perimeter += 1
44+
45+
return area, perimeter, patch
46+
47+
seen = set()
48+
49+
for y in range(bounds[1]):
50+
for x in range(bounds[0]):
51+
if (x, y) in seen: continue
52+
area, perimeter, patch = fencing((x, y))
53+
# print(f'A region of {grid[(x, y)]} plants with price {area} * {perimeter} = {area*perimeter}')
54+
seen.update(patch)
55+
price += area*perimeter
56+
57+
return price
58+
59+
def part2(data):
60+
grid, bounds = data
61+
...

2024/13.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from re import findall
5+
6+
def parse(data):
7+
machines = []
8+
9+
for machine in data.split("\n\n"):
10+
Ax,Ay,Bx,By,Px,Py = findall(r"(\d+)", machine)
11+
machines.append((int(Ax),int(Ay),int(Bx),int(By),int(Px),int(Py)))
12+
13+
return machines
14+
15+
split_data = parse
16+
completed = True
17+
raw_data = None # Not To be touched
18+
19+
def part1(data):
20+
# This can be solved quickly using some simple math
21+
tokens = 0
22+
for machine in data:
23+
Ax,Ay,Bx,By,Px,Py = machine
24+
25+
for Bpress in range(101):
26+
pending = Px - Bpress*Bx
27+
if pending < 0: break # Looks like we are over shooting here...
28+
Apress = pending / Ax
29+
if Apress % 1 != 0 or Apress > 100:
30+
continue # Not a valid solution
31+
32+
# By now Bpress is atmost 100, Apress is atmost 100 and both are integers
33+
if Bpress*By + Apress*Ay == Py:
34+
assert Apress*Ax + Bpress*Bx == Px # Just a flex!
35+
tokens += Apress*3 + Bpress
36+
break
37+
38+
return int(tokens)
39+
40+
def part2(data):
41+
# We can do some simple math on paper and come up with the formula
42+
# Ax * x + Bx * y = Px
43+
# Ay * x + By * y = Py
44+
45+
# https://www.desmos.com/calculator/h566btcmir
46+
47+
tokens = 0
48+
for machine in data:
49+
Ax,Ay,Bx,By,Px,Py = machine
50+
Px += 10000000000000 # The only change
51+
Py += 10000000000000 # The only change
52+
53+
aPress = ((By*Px) - (Bx*Py))/((By*Ax) - (Bx*Ay))
54+
if aPress % 1 != 0 or aPress < 0: continue # We only accept integer solutions
55+
bPress = (Px - aPress*Ax)/Bx
56+
if bPress % 1 != 0 or aPress < 0: continue # We only accept integer solutions
57+
58+
# print(aPress, bPress)
59+
assert aPress*Ax + bPress*Bx == Px and aPress*Ay + bPress*By == Py # Just a flex!
60+
tokens += aPress*3 + bPress
61+
62+
63+
return int(tokens)

2024/14.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from PIL import Image
5+
import os
6+
7+
def parse(data:str):
8+
robots = []
9+
for line in data.split("\n"):
10+
p, v = line.split(' ')
11+
p = p[2:].split(',')
12+
v = v[2:].split(',')
13+
robots.append(((int(p[0]), int(p[1])), (int(v[0]), int(v[1]))))
14+
15+
return robots
16+
17+
split_data = parse
18+
completed = True
19+
raw_data = None # Not To be touched
20+
21+
def part1(data):
22+
bx, by = 101, 103
23+
quadrants = [0, 0, 0, 0]
24+
25+
# Simulation
26+
for robot in data:
27+
# Instant 100s simulation
28+
((x, y), (vx, vy)) = robot
29+
nx = (x + vx * 100) % bx
30+
ny = (y + vy * 100) % by
31+
32+
# Figuring out which quadrant the robot is in.
33+
if nx < bx // 2:
34+
if ny < by // 2:
35+
quadrants[0] += 1
36+
elif ny > by // 2:
37+
quadrants[1] += 1
38+
elif nx > bx // 2:
39+
if ny < by // 2:
40+
quadrants[2] += 1
41+
elif ny > by // 2:
42+
quadrants[3] += 1
43+
44+
# print(quadrants)
45+
return quadrants[0] * quadrants[1] * quadrants[2] * quadrants[3]
46+
47+
48+
def part2(data):
49+
bx, by = 101, 103
50+
51+
os.makedirs("2024/14", exist_ok=True)
52+
53+
# Simulation
54+
robots = data
55+
for i in range(1, 10_001):
56+
nRobots = []
57+
58+
# Gray scale image
59+
img = Image.new('L', (bx, by), 255)
60+
for robot in robots:
61+
# Instant 100s simulation
62+
((x, y), (vx, vy)) = robot
63+
nx = (x + vx) % bx
64+
ny = (y + vy) % by
65+
66+
# Drawing the robot
67+
img.putpixel((nx, ny), 0)
68+
69+
nRobots.append(((nx, ny), (vx, vy)))
70+
71+
# Save the image
72+
img.save(f"2024/14/{i}.png")
73+
robots = nRobots
74+
75+
76+
77+
print("Please open the folder 2024/14 and search for the file that forms a christmas tree. You will see 10,000 images and one of them forms the christmas tree.")

2024/7.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from typing import List
5+
from itertools import product
6+
7+
split_data = True
8+
completed = True
9+
raw_data = None # Not To be touched
10+
11+
def part1(data:List[str]):
12+
result = 0
13+
14+
for line in data:
15+
output, numbers = line.split(': ')
16+
numbers = numbers.split(' ')
17+
18+
for operators in product(['+', '*'], repeat=len(numbers) - 1):
19+
equation = int(numbers[0])
20+
for op, num in zip(operators, numbers[1:]):
21+
if op == '+':
22+
equation += int(num)
23+
else:
24+
equation *= int(num)
25+
26+
if equation == int(output):
27+
result += int(output)
28+
break
29+
30+
return result
31+
32+
def part2(data:List[str]):
33+
result = 0
34+
35+
for line in data:
36+
output, numbers = line.split(': ')
37+
numbers = numbers.split(' ')
38+
39+
for operators in product(['+', '*', '||'], repeat=len(numbers) - 1):
40+
equation = int(numbers[0])
41+
for op, num in zip(operators, numbers[1:]):
42+
if op == '+':
43+
equation += int(num)
44+
elif op == '*':
45+
equation *= int(num)
46+
else:
47+
equation = int(str(equation)+num)
48+
49+
if equation == int(output):
50+
result += int(output)
51+
break
52+
53+
return result

2024/8.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from itertools import combinations
5+
6+
def parse(data):
7+
ants = {}
8+
bounds = len(data.split('\n')[0]), len(data.split('\n'))
9+
for y, line in enumerate(data.split('\n')):
10+
for x, char in enumerate(line):
11+
if char == '.': continue
12+
if char not in ants: ants[char] = []
13+
ants[char].append((x, y))
14+
15+
return ants, bounds
16+
17+
split_data = parse
18+
completed = True
19+
raw_data = None # Not To be touched
20+
21+
def part1(data):
22+
# Wow, this is pure math...
23+
ants, bounds = data
24+
antiNodes = set()
25+
for nodes in ants.values():
26+
27+
for (a1x, a1y), (a2x, a2y) in combinations(nodes, r=2):
28+
dx, dy = a2x - a1x, a2y - a1y
29+
anti1 = (a1x - dx, a1y - dy)
30+
anti2 = (a2x + dx, a2y + dy)
31+
32+
if 0 <= anti1[0] < bounds[0] and 0 <= anti1[1] < bounds[1]:
33+
antiNodes.add(anti1)
34+
if 0 <= anti2[0] < bounds[0] and 0 <= anti2[1] < bounds[1]:
35+
antiNodes.add(anti2)
36+
37+
return len(antiNodes)
38+
39+
def part2(data):
40+
ants, bounds = data
41+
antiNodes = set()
42+
for nodes in ants.values():
43+
44+
for (a1x, a1y), (a2x, a2y) in combinations(nodes, r=2):
45+
dx, dy = a2x - a1x, a2y - a1y
46+
47+
nx, ny = a1x, a1y
48+
while 0 <= nx < bounds[0] and 0 <= ny < bounds[1]:
49+
antiNodes.add((nx, ny))
50+
nx -= dx
51+
ny -= dy
52+
53+
nx, ny = a1x, a1y
54+
while 0 <= nx < bounds[0] and 0 <= ny < bounds[1]:
55+
antiNodes.add((nx, ny))
56+
nx += dx
57+
ny += dy
58+
59+
return len(antiNodes)

Completed.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
Over Here you can see the completion status of the project.
44

5-
Parts Completed: 301/500 (60.20%)
5+
Parts Completed: 310/500 (62.00%)
66
<br>
7-
Days Completed: 147/250 (58.80%)
7+
Days Completed: 151/250 (60.40%)
88
<br>
9-
Lines of code: 8577
9+
Lines of code: 8859
1010

1111
### Legend
1212

@@ -17,7 +17,7 @@ Lines of code: 8577
1717

1818
||1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|
1919
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
20-
|**2024**||||||🚫|🚫|🚫||||🚫|🚫|🚫|🚫|🚫|||🚫|🚫|🚫|🚫|🚫|🚫|🚫|
20+
|**2024**||||||🚫||||||½||🚫|🚫|🚫||||🚫|🚫|🚫|🚫|🚫|🚫|
2121
|**2023**|||||½|||||½||🚫|||||🚫|🚫||🚫|½|🚫|🚫|🚫|🚫|
2222
|**2022**||||||||🚫|||½|🚫|🚫||🚫|🚫|🚫||🚫|🚫||½||🚫||
2323
|**2021**||||||||||||🚫||||🚫|🚫|🚫|🚫|🚫|🚫|🚫|🚫|🚫|🚫|

main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,8 @@ def formatTime(timeTaken:float) -> str:
214214
if not raw_data:
215215
print("Looks like the data doesn't exsist! Refetching data", end="\r")
216216
collect_data(True)
217+
elif args.submit:
218+
print(f"Force Downloading data as it will be submitted", end="\r")
219+
collect_data(True)
217220
print("Running Code...",end="\r")
218221
run_code()

0 commit comments

Comments
 (0)