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." )
0 commit comments