Supa-AI commited on
Commit
2409b6b
·
verified ·
1 Parent(s): 02fecfd

Upload aoc.csv

Browse files
Files changed (1) hide show
  1. aoc.csv +1556 -0
aoc.csv ADDED
@@ -0,0 +1,1556 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ year,day,part,question,answer,solution,language
2
+ 2024,2,1,"--- Day 2: Red-Nosed Reports ---
3
+
4
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
5
+
6
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
7
+
8
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
9
+
10
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
11
+
12
+ 7 6 4 2 1
13
+ 1 2 7 8 9
14
+ 9 7 6 2 1
15
+ 1 3 2 4 5
16
+ 8 6 4 4 1
17
+ 1 3 6 7 9
18
+ This example data contains six reports each containing five levels.
19
+
20
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
21
+
22
+ The levels are either all increasing or all decreasing.
23
+ Any two adjacent levels differ by at least one and at most three.
24
+ In the example above, the reports can be found safe or unsafe by checking those rules:
25
+
26
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
27
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
28
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
29
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
30
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
31
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
32
+ So, in this example, 2 reports are safe.
33
+
34
+ Analyze the unusual data from the engineers. How many reports are safe?",341,"def parseInput():
35
+ input = []
36
+
37
+ with open('input.txt', 'r') as file:
38
+ tmp = file.read().splitlines()
39
+ tmp2 = [i.split(' ') for i in tmp]
40
+ for item in tmp2:
41
+ input.append([int(i) for i in item])
42
+
43
+ return input
44
+
45
+ if __name__ == ""__main__"":
46
+ input = parseInput()
47
+
48
+ safe = 0
49
+ for item in input:
50
+ tmpSafe = True
51
+ increasing = item[1] >= item[0]
52
+ for i in range(len(item) - 1):
53
+ diff = item[i + 1] - item[i]
54
+
55
+ if increasing and diff <= 0:
56
+ tmpSafe = False
57
+ break
58
+
59
+ if not increasing and diff >= 0:
60
+ tmpSafe = False
61
+ break
62
+
63
+ if 0 < abs(diff) > 3:
64
+ tmpSafe = False
65
+ break
66
+ if tmpSafe:
67
+ safe += 1
68
+
69
+ print(safe)
70
+ ",python:3.9
71
+ 2024,2,1,"--- Day 2: Red-Nosed Reports ---
72
+
73
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
74
+
75
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
76
+
77
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
78
+
79
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
80
+
81
+ 7 6 4 2 1
82
+ 1 2 7 8 9
83
+ 9 7 6 2 1
84
+ 1 3 2 4 5
85
+ 8 6 4 4 1
86
+ 1 3 6 7 9
87
+ This example data contains six reports each containing five levels.
88
+
89
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
90
+
91
+ The levels are either all increasing or all decreasing.
92
+ Any two adjacent levels differ by at least one and at most three.
93
+ In the example above, the reports can be found safe or unsafe by checking those rules:
94
+
95
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
96
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
97
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
98
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
99
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
100
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
101
+ So, in this example, 2 reports are safe.
102
+
103
+ Analyze the unusual data from the engineers. How many reports are safe?",341,"import sys
104
+
105
+ input_path = sys.argv[1] if len(sys.argv) > 1 else ""input.txt""
106
+
107
+
108
+ def solve(input):
109
+ with open(input) as f:
110
+ lines = f.read().splitlines()
111
+ L = [list(map(int, line.split("" ""))) for line in lines]
112
+ # L = [[int(l) for l in line.split("" "")] for line in lines]
113
+ counter = 0
114
+
115
+ for l in L:
116
+ print(f""Evaluating {l}"")
117
+ skip = False
118
+ # decreasing
119
+ if l[0] > l[1]:
120
+ for i in range(len(l) - 1):
121
+ if l[i] - l[i + 1] > 3 or l[i] < l[i + 1] or l[i] == l[i + 1]:
122
+ skip = True
123
+ break
124
+ # increasing
125
+ elif l[0] < l[1]:
126
+ for i in range(len(l) - 1):
127
+ if l[i + 1] - l[i] > 3 or l[i] > l[i + 1] or l[i] == l[i + 1]:
128
+ skip = True
129
+ break
130
+ else:
131
+ continue
132
+ if skip:
133
+ continue
134
+ print(""Safe"")
135
+ counter += 1
136
+ print(counter)
137
+
138
+
139
+ solve(input_path)
140
+ ",python:3.9
141
+ 2024,2,1,"--- Day 2: Red-Nosed Reports ---
142
+
143
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
144
+
145
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
146
+
147
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
148
+
149
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
150
+
151
+ 7 6 4 2 1
152
+ 1 2 7 8 9
153
+ 9 7 6 2 1
154
+ 1 3 2 4 5
155
+ 8 6 4 4 1
156
+ 1 3 6 7 9
157
+ This example data contains six reports each containing five levels.
158
+
159
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
160
+
161
+ The levels are either all increasing or all decreasing.
162
+ Any two adjacent levels differ by at least one and at most three.
163
+ In the example above, the reports can be found safe or unsafe by checking those rules:
164
+
165
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
166
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
167
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
168
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
169
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
170
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
171
+ So, in this example, 2 reports are safe.
172
+
173
+ Analyze the unusual data from the engineers. How many reports are safe?",341,"l = [list(map(int,x.split())) for x in open(""i.txt"")]
174
+
175
+ print(sum(any(all(d<b-a<u for a,b in zip(s,s[1:])) for d,u in[(0,4),(-4,0)])for s in l))",python:3.9
176
+ 2024,2,1,"--- Day 2: Red-Nosed Reports ---
177
+
178
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
179
+
180
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
181
+
182
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
183
+
184
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
185
+
186
+ 7 6 4 2 1
187
+ 1 2 7 8 9
188
+ 9 7 6 2 1
189
+ 1 3 2 4 5
190
+ 8 6 4 4 1
191
+ 1 3 6 7 9
192
+ This example data contains six reports each containing five levels.
193
+
194
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
195
+
196
+ The levels are either all increasing or all decreasing.
197
+ Any two adjacent levels differ by at least one and at most three.
198
+ In the example above, the reports can be found safe or unsafe by checking those rules:
199
+
200
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
201
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
202
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
203
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
204
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
205
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
206
+ So, in this example, 2 reports are safe.
207
+
208
+ Analyze the unusual data from the engineers. How many reports are safe?",341,"import sys
209
+
210
+ input_path = sys.argv[1] if len(sys.argv) > 1 else ""input.txt""
211
+
212
+
213
+ def solve(input):
214
+ with open(input) as f:
215
+ lines = f.read().splitlines()
216
+ L = [list(map(int, line.split("" ""))) for line in lines]
217
+ counter = 0
218
+
219
+ for l in L:
220
+ inc_dec = l == sorted(l) or l == sorted(l, reverse=True)
221
+ size_ok = True
222
+ for i in range(len(l) - 1):
223
+ if not 0 < abs(l[i] - l[i + 1]) <= 3:
224
+ size_ok = False
225
+
226
+ if inc_dec and size_ok:
227
+ counter += 1
228
+ print(counter)
229
+
230
+
231
+ solve(input_path)
232
+ ",python:3.9
233
+ 2024,2,1,"--- Day 2: Red-Nosed Reports ---
234
+
235
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
236
+
237
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
238
+
239
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
240
+
241
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
242
+
243
+ 7 6 4 2 1
244
+ 1 2 7 8 9
245
+ 9 7 6 2 1
246
+ 1 3 2 4 5
247
+ 8 6 4 4 1
248
+ 1 3 6 7 9
249
+ This example data contains six reports each containing five levels.
250
+
251
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
252
+
253
+ The levels are either all increasing or all decreasing.
254
+ Any two adjacent levels differ by at least one and at most three.
255
+ In the example above, the reports can be found safe or unsafe by checking those rules:
256
+
257
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
258
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
259
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
260
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
261
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
262
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
263
+ So, in this example, 2 reports are safe.
264
+
265
+ Analyze the unusual data from the engineers. How many reports are safe?",341,"with open('input.txt', 'r') as file:
266
+ lines = file.readlines()
267
+ sum = 0
268
+ for line in lines:
269
+ A = [int(x) for x in line.split()]
270
+ ascending = False
271
+ valid = True
272
+
273
+ if A[0] < A[1]:
274
+ ascending = True
275
+
276
+ for i in range(len(A) - 1):
277
+ if ascending:
278
+ difference = A[i + 1] - A[i]
279
+ else:
280
+ difference = A[i] - A[i + 1]
281
+ if difference > 3 or difference <= 0:
282
+ valid = False
283
+
284
+ if valid:
285
+ sum += 1
286
+ print(sum)",python:3.9
287
+ 2024,2,2,"--- Day 2: Red-Nosed Reports ---
288
+
289
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
290
+
291
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
292
+
293
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
294
+
295
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
296
+
297
+ 7 6 4 2 1
298
+ 1 2 7 8 9
299
+ 9 7 6 2 1
300
+ 1 3 2 4 5
301
+ 8 6 4 4 1
302
+ 1 3 6 7 9
303
+ This example data contains six reports each containing five levels.
304
+
305
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
306
+
307
+ The levels are either all increasing or all decreasing.
308
+ Any two adjacent levels differ by at least one and at most three.
309
+ In the example above, the reports can be found safe or unsafe by checking those rules:
310
+
311
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
312
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
313
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
314
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
315
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
316
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
317
+ So, in this example, 2 reports are safe.
318
+
319
+ Analyze the unusual data from the engineers. How many reports are safe?
320
+
321
+ Your puzzle answer was 341.
322
+
323
+ --- Part Two ---
324
+
325
+ The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener.
326
+
327
+ The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level in what would otherwise be a safe report. It's like the bad level never happened!
328
+
329
+ Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe.
330
+
331
+ More of the above example's reports are now safe:
332
+
333
+ 7 6 4 2 1: Safe without removing any level.
334
+ 1 2 7 8 9: Unsafe regardless of which level is removed.
335
+ 9 7 6 2 1: Unsafe regardless of which level is removed.
336
+ 1 3 2 4 5: Safe by removing the second level, 3.
337
+ 8 6 4 4 1: Safe by removing the third level, 4.
338
+ 1 3 6 7 9: Safe without removing any level.
339
+ Thanks to the Problem Dampener, 4 reports are actually safe!
340
+
341
+ Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. How many reports are now safe?",404,"def is_safe(levels: list[int]) -> bool:
342
+ # Calculate the difference between each report
343
+ differences = [first - second for first, second in zip(levels, levels[1:])]
344
+
345
+ max_difference = 3
346
+ return (all(0 < difference <= max_difference for difference in differences)
347
+ or all(-max_difference <= difference < 0 for difference in differences))
348
+
349
+ def part1():
350
+ with open(""input.txt"") as levels:
351
+ safe_reports = 0
352
+ for level in levels:
353
+ if is_safe(list(map(int, level.split()))):
354
+ safe_reports += 1
355
+
356
+ print(safe_reports)
357
+
358
+ def part2():
359
+ with open(""input.txt"") as levels:
360
+ safe_reports = 0
361
+ for level in levels:
362
+ level = list(map(int, level.split()))
363
+
364
+ for i in range(len(level)):
365
+ if is_safe(level[:i] + level[i + 1:]):
366
+ print(level[:i] + level[i + 1:])
367
+ safe_reports += 1
368
+ break
369
+
370
+ print(safe_reports)
371
+
372
+
373
+
374
+ part2()",python:3.9
375
+ 2024,2,2,"--- Day 2: Red-Nosed Reports ---
376
+
377
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
378
+
379
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
380
+
381
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
382
+
383
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
384
+
385
+ 7 6 4 2 1
386
+ 1 2 7 8 9
387
+ 9 7 6 2 1
388
+ 1 3 2 4 5
389
+ 8 6 4 4 1
390
+ 1 3 6 7 9
391
+ This example data contains six reports each containing five levels.
392
+
393
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
394
+
395
+ The levels are either all increasing or all decreasing.
396
+ Any two adjacent levels differ by at least one and at most three.
397
+ In the example above, the reports can be found safe or unsafe by checking those rules:
398
+
399
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
400
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
401
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
402
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
403
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
404
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
405
+ So, in this example, 2 reports are safe.
406
+
407
+ Analyze the unusual data from the engineers. How many reports are safe?
408
+
409
+ Your puzzle answer was 341.
410
+
411
+ --- Part Two ---
412
+
413
+ The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener.
414
+
415
+ The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level in what would otherwise be a safe report. It's like the bad level never happened!
416
+
417
+ Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe.
418
+
419
+ More of the above example's reports are now safe:
420
+
421
+ 7 6 4 2 1: Safe without removing any level.
422
+ 1 2 7 8 9: Unsafe regardless of which level is removed.
423
+ 9 7 6 2 1: Unsafe regardless of which level is removed.
424
+ 1 3 2 4 5: Safe by removing the second level, 3.
425
+ 8 6 4 4 1: Safe by removing the third level, 4.
426
+ 1 3 6 7 9: Safe without removing any level.
427
+ Thanks to the Problem Dampener, 4 reports are actually safe!
428
+
429
+ Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. How many reports are now safe?",404,"def if_asc(x):
430
+ result = all(y < z for y,z in zip(x, x[1:]))
431
+ return result
432
+
433
+ def if_dsc(x):
434
+ result = all (y > z for y,z in zip(x, x[1:]))
435
+ return result
436
+
437
+ def if_asc_morethan3(x):
438
+ result = all(y > z - 4 for y,z in zip(x,x[1:]))
439
+ return result
440
+
441
+ def if_dsc_morethan3(x):
442
+ result = all(y < z + 4 for y, z in zip(x, x[1:]))
443
+ return result
444
+
445
+ input = [list(map(int,x.split("" ""))) for x in open(""input2.txt"", ""r"").read().splitlines()]
446
+ safe = 0
447
+
448
+ for x in input:
449
+ is_safe = 0
450
+ asc = if_asc(x)
451
+ dsc = if_dsc(x)
452
+
453
+ if asc:
454
+ asc3 = if_asc_morethan3(x)
455
+ if asc3:
456
+ safe +=1
457
+ is_safe +=1
458
+
459
+ if dsc:
460
+ dsc3 = if_dsc_morethan3(x)
461
+ if dsc3:
462
+ safe+=1
463
+ is_safe +=1
464
+
465
+ if is_safe == 0:
466
+ list_safe = 0
467
+ for i in range(len(x)):
468
+ list = x[:i] + x[i+1:]
469
+ list_asc = if_asc(list)
470
+ list_dsc = if_dsc(list)
471
+
472
+ if list_asc:
473
+ list_asc3 = if_asc_morethan3(list)
474
+ if list_asc3:
475
+ list_safe +=1
476
+ elif list_dsc:
477
+ list_dsc3 = if_dsc_morethan3(list)
478
+ if list_dsc3:
479
+ list_safe +=1
480
+ if list_safe > 0:
481
+ safe+=1
482
+
483
+ print (safe)",python:3.9
484
+ 2024,2,2,"--- Day 2: Red-Nosed Reports ---
485
+
486
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
487
+
488
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
489
+
490
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
491
+
492
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
493
+
494
+ 7 6 4 2 1
495
+ 1 2 7 8 9
496
+ 9 7 6 2 1
497
+ 1 3 2 4 5
498
+ 8 6 4 4 1
499
+ 1 3 6 7 9
500
+ This example data contains six reports each containing five levels.
501
+
502
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
503
+
504
+ The levels are either all increasing or all decreasing.
505
+ Any two adjacent levels differ by at least one and at most three.
506
+ In the example above, the reports can be found safe or unsafe by checking those rules:
507
+
508
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
509
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
510
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
511
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
512
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
513
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
514
+ So, in this example, 2 reports are safe.
515
+
516
+ Analyze the unusual data from the engineers. How many reports are safe?
517
+
518
+ Your puzzle answer was 341.
519
+
520
+ --- Part Two ---
521
+
522
+ The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener.
523
+
524
+ The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level in what would otherwise be a safe report. It's like the bad level never happened!
525
+
526
+ Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe.
527
+
528
+ More of the above example's reports are now safe:
529
+
530
+ 7 6 4 2 1: Safe without removing any level.
531
+ 1 2 7 8 9: Unsafe regardless of which level is removed.
532
+ 9 7 6 2 1: Unsafe regardless of which level is removed.
533
+ 1 3 2 4 5: Safe by removing the second level, 3.
534
+ 8 6 4 4 1: Safe by removing the third level, 4.
535
+ 1 3 6 7 9: Safe without removing any level.
536
+ Thanks to the Problem Dampener, 4 reports are actually safe!
537
+
538
+ Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. How many reports are now safe?",404,"import sys
539
+ import logging
540
+
541
+ # logging.basicConfig(,level=sys.argv[2])
542
+
543
+ input_path = sys.argv[1] if len(sys.argv) > 1 else ""input.txt""
544
+
545
+
546
+ def is_ok(lst):
547
+ inc_dec = lst == sorted(lst) or lst == sorted(lst, reverse=True)
548
+ size_ok = True
549
+ for i in range(len(lst) - 1):
550
+ if not 0 < abs(lst[i] - lst[i + 1]) <= 3:
551
+ size_ok = False
552
+
553
+ if inc_dec and size_ok:
554
+ return True
555
+
556
+
557
+ def solve(input_path: str):
558
+ with open(input_path) as f:
559
+ lines = f.read().splitlines()
560
+
561
+ L = [list(map(int, line.split("" ""))) for line in lines]
562
+ counter = 0
563
+ for idx, l in enumerate(L):
564
+ logging.info(f""Evaluating {l}"")
565
+ logging.error(f""there was an error {idx}: {l}"")
566
+ if is_ok(l):
567
+ counter += 1
568
+ else:
569
+ for i in range(len(l)):
570
+ tmp = l.copy()
571
+ tmp.pop(i)
572
+ if is_ok(tmp):
573
+ counter += 1
574
+ break
575
+ print(counter)
576
+
577
+
578
+ solve(input_path)
579
+ ",python:3.9
580
+ 2024,2,2,"--- Day 2: Red-Nosed Reports ---
581
+
582
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
583
+
584
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
585
+
586
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
587
+
588
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
589
+
590
+ 7 6 4 2 1
591
+ 1 2 7 8 9
592
+ 9 7 6 2 1
593
+ 1 3 2 4 5
594
+ 8 6 4 4 1
595
+ 1 3 6 7 9
596
+ This example data contains six reports each containing five levels.
597
+
598
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
599
+
600
+ The levels are either all increasing or all decreasing.
601
+ Any two adjacent levels differ by at least one and at most three.
602
+ In the example above, the reports can be found safe or unsafe by checking those rules:
603
+
604
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
605
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
606
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
607
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
608
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
609
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
610
+ So, in this example, 2 reports are safe.
611
+
612
+ Analyze the unusual data from the engineers. How many reports are safe?
613
+
614
+ Your puzzle answer was 341.
615
+
616
+ --- Part Two ---
617
+
618
+ The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener.
619
+
620
+ The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level in what would otherwise be a safe report. It's like the bad level never happened!
621
+
622
+ Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe.
623
+
624
+ More of the above example's reports are now safe:
625
+
626
+ 7 6 4 2 1: Safe without removing any level.
627
+ 1 2 7 8 9: Unsafe regardless of which level is removed.
628
+ 9 7 6 2 1: Unsafe regardless of which level is removed.
629
+ 1 3 2 4 5: Safe by removing the second level, 3.
630
+ 8 6 4 4 1: Safe by removing the third level, 4.
631
+ 1 3 6 7 9: Safe without removing any level.
632
+ Thanks to the Problem Dampener, 4 reports are actually safe!
633
+
634
+ Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. How many reports are now safe?",404,"count = 0
635
+
636
+ def safe(levels):
637
+ # Check if the sequence is either all increasing or all decreasing
638
+ if all(levels[i] < levels[i + 1] for i in range(len(levels) - 1)): # Increasing
639
+ diffs = [levels[i + 1] - levels[i] for i in range(len(levels) - 1)]
640
+ elif all(levels[i] > levels[i + 1] for i in range(len(levels) - 1)): # Decreasing
641
+ diffs = [levels[i] - levels[i + 1] for i in range(len(levels) - 1)]
642
+ else:
643
+ return False # Mixed increasing and decreasing, not allowed
644
+
645
+ return all(1 <= x <= 3 for x in diffs) # Check if diffs are between 1 and 3
646
+
647
+ # Open and process the input file
648
+ with open('input.txt', 'r') as file:
649
+ for report in file:
650
+ levels = list(map(int, report.split()))
651
+
652
+ # Check if the report is safe without modification
653
+ if safe(levels):
654
+ count += 1
655
+ continue
656
+
657
+ # Try removing one level and check if the modified report is safe
658
+ for index in range(len(levels)):
659
+ modified_levels = levels[:index] + levels[index+1:]
660
+ if safe(modified_levels):
661
+ count += 1
662
+ break
663
+
664
+ print(count)
665
+ ",python:3.9
666
+ 2024,2,2,"--- Day 2: Red-Nosed Reports ---
667
+
668
+ Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
669
+
670
+ While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
671
+
672
+ They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
673
+
674
+ The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
675
+
676
+ 7 6 4 2 1
677
+ 1 2 7 8 9
678
+ 9 7 6 2 1
679
+ 1 3 2 4 5
680
+ 8 6 4 4 1
681
+ 1 3 6 7 9
682
+ This example data contains six reports each containing five levels.
683
+
684
+ The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
685
+
686
+ The levels are either all increasing or all decreasing.
687
+ Any two adjacent levels differ by at least one and at most three.
688
+ In the example above, the reports can be found safe or unsafe by checking those rules:
689
+
690
+ 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2.
691
+ 1 2 7 8 9: Unsafe because 2 7 is an increase of 5.
692
+ 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4.
693
+ 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing.
694
+ 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease.
695
+ 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3.
696
+ So, in this example, 2 reports are safe.
697
+
698
+ Analyze the unusual data from the engineers. How many reports are safe?
699
+
700
+ Your puzzle answer was 341.
701
+
702
+ --- Part Two ---
703
+
704
+ The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener.
705
+
706
+ The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level in what would otherwise be a safe report. It's like the bad level never happened!
707
+
708
+ Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe.
709
+
710
+ More of the above example's reports are now safe:
711
+
712
+ 7 6 4 2 1: Safe without removing any level.
713
+ 1 2 7 8 9: Unsafe regardless of which level is removed.
714
+ 9 7 6 2 1: Unsafe regardless of which level is removed.
715
+ 1 3 2 4 5: Safe by removing the second level, 3.
716
+ 8 6 4 4 1: Safe by removing the third level, 4.
717
+ 1 3 6 7 9: Safe without removing any level.
718
+ Thanks to the Problem Dampener, 4 reports are actually safe!
719
+
720
+ Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. How many reports are now safe?",404,"def check_sequence(numbers):
721
+ nums = [int(x) for x in numbers.split()]
722
+
723
+ # First check if sequence is valid as is
724
+ if is_valid_sequence(nums):
725
+ return True
726
+
727
+ # Try removing one number at a time
728
+ for i in range(len(nums)):
729
+ test_nums = nums[:i] + nums[i+1:]
730
+ if is_valid_sequence(test_nums):
731
+ return True
732
+
733
+ return False
734
+
735
+ def is_valid_sequence(nums):
736
+ if len(nums) < 2:
737
+ return True
738
+
739
+ diffs = [nums[i+1] - nums[i] for i in range(len(nums)-1)]
740
+
741
+ # Check if all differences are within -3 to 3
742
+ if any(abs(d) > 3 for d in diffs):
743
+ return False
744
+
745
+ # Check if sequence is strictly increasing or decreasing
746
+ return all(d > 0 for d in diffs) or all(d < 0 for d in diffs)
747
+
748
+ # Read the file and count valid sequences
749
+ valid_count = 0
750
+ with open('02.input', 'r') as file:
751
+ for line in file:
752
+ line = line.strip()
753
+ if check_sequence(line):
754
+ print(line)
755
+ valid_count += 1
756
+
757
+ print(f""Number of valid sequences: {valid_count}"")
758
+ ",python:3.9
759
+ 2024,1,1,"--- Day 1: Historian Hysteria ---
760
+
761
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
762
+
763
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
764
+
765
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
766
+
767
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
768
+
769
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
770
+
771
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
772
+
773
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
774
+
775
+ For example:
776
+
777
+ 3 4
778
+ 4 3
779
+ 2 5
780
+ 1 3
781
+ 3 9
782
+ 3 3
783
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
784
+
785
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
786
+
787
+ In the example list above, the pairs and distances would be as follows:
788
+
789
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
790
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
791
+ The third-smallest number in both lists is 3, so the distance between them is 0.
792
+ The next numbers to pair up are 3 and 4, a distance of 1.
793
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
794
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
795
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
796
+
797
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"def parseInput():
798
+ left = []
799
+ right = []
800
+
801
+ with open('input.txt', 'r') as file:
802
+ input = file.read().splitlines()
803
+ for line in input:
804
+ split = line.split("" "")
805
+ left.append(int(split[0]))
806
+ right.append(int(split[1]))
807
+ return left, right
808
+
809
+ def sort(arr: list):
810
+ for i in range(len(arr)):
811
+ for j in range(i, len(arr)):
812
+ if arr[j] < arr[i]:
813
+ arr[i], arr[j] = arr[j], arr[i]
814
+ return arr
815
+
816
+ if __name__ == ""__main__"":
817
+ left, right = parseInput()
818
+ left = sort(left)
819
+ right = sort(right)
820
+
821
+ # print(left)
822
+ # print(right)
823
+
824
+ sumDist = 0
825
+ for i in range(len(left)):
826
+ sumDist += left[i] - right[i] if left[i] > right[i] else right[i] - left[i]
827
+ print(sumDist)
828
+ ",python:3.9
829
+ 2024,1,1,"--- Day 1: Historian Hysteria ---
830
+
831
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
832
+
833
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
834
+
835
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
836
+
837
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
838
+
839
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
840
+
841
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
842
+
843
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
844
+
845
+ For example:
846
+
847
+ 3 4
848
+ 4 3
849
+ 2 5
850
+ 1 3
851
+ 3 9
852
+ 3 3
853
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
854
+
855
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
856
+
857
+ In the example list above, the pairs and distances would be as follows:
858
+
859
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
860
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
861
+ The third-smallest number in both lists is 3, so the distance between them is 0.
862
+ The next numbers to pair up are 3 and 4, a distance of 1.
863
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
864
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
865
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
866
+
867
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"def find_min_diff(a, b):
868
+ sorted_a = sorted(a)
869
+ sorted_b = sorted(b)
870
+ d = 0
871
+ for i in range(len(sorted_a)):
872
+ d += abs(sorted_a[i] - sorted_b[i])
873
+ return d
874
+
875
+
876
+ def read_input_file(file_name):
877
+ a = []
878
+ b = []
879
+ with open(file_name, ""r"") as file:
880
+ for line in file:
881
+ values = line.strip().split()
882
+ if len(values) == 2:
883
+ a.append(int(values[0]))
884
+ b.append(int(values[1]))
885
+ return a, b
886
+
887
+ def main():
888
+ d = 0
889
+ a, b = read_input_file('input2.txt')
890
+ print(find_min_diff(a, b))
891
+
892
+ main()",python:3.9
893
+ 2024,1,1,"--- Day 1: Historian Hysteria ---
894
+
895
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
896
+
897
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
898
+
899
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
900
+
901
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
902
+
903
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
904
+
905
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
906
+
907
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
908
+
909
+ For example:
910
+
911
+ 3 4
912
+ 4 3
913
+ 2 5
914
+ 1 3
915
+ 3 9
916
+ 3 3
917
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
918
+
919
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
920
+
921
+ In the example list above, the pairs and distances would be as follows:
922
+
923
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
924
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
925
+ The third-smallest number in both lists is 3, so the distance between them is 0.
926
+ The next numbers to pair up are 3 and 4, a distance of 1.
927
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
928
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
929
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
930
+
931
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"input = open(""day_01\input.txt"", ""r"")
932
+
933
+ distance = 0
934
+ left_list = []
935
+ right_list = []
936
+
937
+ for line in input:
938
+ values = [x for x in line.strip().split()]
939
+ left_list += [int(values[0])]
940
+ right_list += [int(values[1])]
941
+
942
+ left_list.sort()
943
+ right_list.sort()
944
+
945
+ for i in range(len(left_list)):
946
+ distance += abs(left_list[i] - right_list[i])
947
+
948
+ print(f""The total distance between the lists is {distance}"")
949
+
950
+
951
+ ",python:3.9
952
+ 2024,1,1,"--- Day 1: Historian Hysteria ---
953
+
954
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
955
+
956
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
957
+
958
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
959
+
960
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
961
+
962
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
963
+
964
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
965
+
966
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
967
+
968
+ For example:
969
+
970
+ 3 4
971
+ 4 3
972
+ 2 5
973
+ 1 3
974
+ 3 9
975
+ 3 3
976
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
977
+
978
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
979
+
980
+ In the example list above, the pairs and distances would be as follows:
981
+
982
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
983
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
984
+ The third-smallest number in both lists is 3, so the distance between them is 0.
985
+ The next numbers to pair up are 3 and 4, a distance of 1.
986
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
987
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
988
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
989
+
990
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"def read_input(file_path: str) -> list[tuple[int, int]]:
991
+ """"""
992
+ Reads a file and returns its contents as a list of tuples of integers.
993
+
994
+ Args:
995
+ file_path (str): The path to the input file.
996
+
997
+ Returns:
998
+ list of tuple of int: A list where each element is a tuple of integers
999
+ representing a line in the file.
1000
+ """"""
1001
+ with open(file_path) as f:
1002
+ return [tuple(map(int, line.split())) for line in f]
1003
+
1004
+ def calculate_sum_of_differences(pairs: list[tuple[int, int]]) -> int:
1005
+ """"""
1006
+ Calculate the sum of absolute differences between corresponding elements
1007
+ of two lists derived from pairs of numbers.
1008
+
1009
+ Args:
1010
+ pairs (list of tuple): A list of tuples where each tuple contains two numbers.
1011
+
1012
+ Returns:
1013
+ int: The sum of absolute differences between corresponding elements
1014
+ of the two sorted lists derived from the input pairs.
1015
+ """"""
1016
+ list1, list2 = zip(*pairs)
1017
+ list1, list2 = sorted(list1), sorted(list2)
1018
+ return sum(abs(a - b) for a, b in zip(list1, list2))
1019
+
1020
+ if __name__ == ""__main__"":
1021
+ input_file = 'input.txt'
1022
+ pairs = read_input(input_file)
1023
+ result = calculate_sum_of_differences(pairs)
1024
+ print(result)
1025
+ ",python:3.9
1026
+ 2024,1,1,"--- Day 1: Historian Hysteria ---
1027
+
1028
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
1029
+
1030
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
1031
+
1032
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
1033
+
1034
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
1035
+
1036
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
1037
+
1038
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
1039
+
1040
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
1041
+
1042
+ For example:
1043
+
1044
+ 3 4
1045
+ 4 3
1046
+ 2 5
1047
+ 1 3
1048
+ 3 9
1049
+ 3 3
1050
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
1051
+
1052
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
1053
+
1054
+ In the example list above, the pairs and distances would be as follows:
1055
+
1056
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
1057
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
1058
+ The third-smallest number in both lists is 3, so the distance between them is 0.
1059
+ The next numbers to pair up are 3 and 4, a distance of 1.
1060
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
1061
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
1062
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
1063
+
1064
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"with open(""AdventOfCode D-1 input.txt"", ""r"") as file:
1065
+ content = file.read()
1066
+ lines = content.splitlines()
1067
+
1068
+ liste_1 = []
1069
+ liste_2 = []
1070
+ for i in range(len(lines)):
1071
+ mots = lines[i].split()
1072
+ liste_1.append(int(mots[0]))
1073
+ liste_2.append(int(mots[1]))
1074
+
1075
+ liste_paires = []
1076
+ index = 0
1077
+ while liste_1 != []:
1078
+ liste_paires.append([])
1079
+ minimum1 = min(liste_1)
1080
+ liste_paires[index].append(minimum1)
1081
+ minimum2 = min(liste_2)
1082
+ liste_paires[index].append(minimum2)
1083
+ liste_1.remove(minimum1)
1084
+ liste_2.remove(minimum2)
1085
+ index += 1
1086
+
1087
+ total = 0
1088
+ for i in range(len(liste_paires)):
1089
+ total += abs(int(liste_paires[i][0]) - int(liste_paires[i][1]))
1090
+ print(total)
1091
+
1092
+ #the answer was 3508942
1093
+ ",python:3.9
1094
+ 2024,1,2,"--- Day 1: Historian Hysteria ---
1095
+
1096
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
1097
+
1098
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
1099
+
1100
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
1101
+
1102
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
1103
+
1104
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
1105
+
1106
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
1107
+
1108
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
1109
+
1110
+ For example:
1111
+
1112
+ 3 4
1113
+ 4 3
1114
+ 2 5
1115
+ 1 3
1116
+ 3 9
1117
+ 3 3
1118
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
1119
+
1120
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
1121
+
1122
+ In the example list above, the pairs and distances would be as follows:
1123
+
1124
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
1125
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
1126
+ The third-smallest number in both lists is 3, so the distance between them is 0.
1127
+ The next numbers to pair up are 3 and 4, a distance of 1.
1128
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
1129
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
1130
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
1131
+
1132
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?
1133
+
1134
+ Your puzzle answer was 2378066.
1135
+
1136
+ --- Part Two ---
1137
+
1138
+ Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.
1139
+
1140
+ Or are they?
1141
+
1142
+ The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting.
1143
+
1144
+ This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list.
1145
+
1146
+ Here are the same example lists again:
1147
+
1148
+ 3 4
1149
+ 4 3
1150
+ 2 5
1151
+ 1 3
1152
+ 3 9
1153
+ 3 3
1154
+ For these example lists, here is the process of finding the similarity score:
1155
+
1156
+ The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9.
1157
+ The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4.
1158
+ The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0).
1159
+ The fourth number, 1, also does not appear in the right list.
1160
+ The fifth number, 3, appears in the right list three times; the similarity score increases by 9.
1161
+ The last number, 3, appears in the right list three times; the similarity score again increases by 9.
1162
+ So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9).
1163
+
1164
+ Once again consider your left and right lists. What is their similarity score?",18934359,"def calculate_similarity_score(left, right):
1165
+ score = 0
1166
+ for left_element in left:
1167
+ score += left_element * num_times_in_list(left_element, right)
1168
+ return score
1169
+
1170
+ def num_times_in_list(number, right_list):
1171
+ num_times = 0
1172
+ for element in right_list:
1173
+ if number == element:
1174
+ num_times += 1
1175
+ return num_times
1176
+
1177
+ def build_lists(contents):
1178
+ left = []
1179
+ right = []
1180
+
1181
+ for line in contents.split('\n'):
1182
+ if line:
1183
+ le, re = line.split()
1184
+ left.append(int(le))
1185
+ right.append(int(re))
1186
+
1187
+ return left, right
1188
+
1189
+
1190
+ if __name__ == '__main__':
1191
+ with open('1/day_1_input.txt', 'r') as f:
1192
+ contents = f.read()
1193
+
1194
+ left, right = build_lists(contents)
1195
+ score = calculate_similarity_score(left, right)
1196
+ print(score)
1197
+ ",python:3.9
1198
+ 2024,1,2,"--- Day 1: Historian Hysteria ---
1199
+
1200
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
1201
+
1202
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
1203
+
1204
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
1205
+
1206
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
1207
+
1208
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
1209
+
1210
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
1211
+
1212
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
1213
+
1214
+ For example:
1215
+
1216
+ 3 4
1217
+ 4 3
1218
+ 2 5
1219
+ 1 3
1220
+ 3 9
1221
+ 3 3
1222
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
1223
+
1224
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
1225
+
1226
+ In the example list above, the pairs and distances would be as follows:
1227
+
1228
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
1229
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
1230
+ The third-smallest number in both lists is 3, so the distance between them is 0.
1231
+ The next numbers to pair up are 3 and 4, a distance of 1.
1232
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
1233
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
1234
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
1235
+
1236
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?
1237
+
1238
+ Your puzzle answer was 2378066.
1239
+
1240
+ --- Part Two ---
1241
+
1242
+ Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.
1243
+
1244
+ Or are they?
1245
+
1246
+ The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting.
1247
+
1248
+ This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list.
1249
+
1250
+ Here are the same example lists again:
1251
+
1252
+ 3 4
1253
+ 4 3
1254
+ 2 5
1255
+ 1 3
1256
+ 3 9
1257
+ 3 3
1258
+ For these example lists, here is the process of finding the similarity score:
1259
+
1260
+ The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9.
1261
+ The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4.
1262
+ The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0).
1263
+ The fourth number, 1, also does not appear in the right list.
1264
+ The fifth number, 3, appears in the right list three times; the similarity score increases by 9.
1265
+ The last number, 3, appears in the right list three times; the similarity score again increases by 9.
1266
+ So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9).
1267
+
1268
+ Once again consider your left and right lists. What is their similarity score?",18934359,"leftNums, rightNums = [], []
1269
+
1270
+ with open('input.txt') as input:
1271
+ while line := input.readline().strip():
1272
+ left, right = line.split()
1273
+ leftNums.append(int(left.strip()))
1274
+ rightNums.append(int(right.strip()))
1275
+
1276
+ total = 0
1277
+ for i in range(len(leftNums)):
1278
+ total += leftNums[i] * rightNums.count(leftNums[i])
1279
+
1280
+ print(f""total: {total}"")",python:3.9
1281
+ 2024,1,2,"--- Day 1: Historian Hysteria ---
1282
+
1283
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
1284
+
1285
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
1286
+
1287
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
1288
+
1289
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
1290
+
1291
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
1292
+
1293
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
1294
+
1295
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
1296
+
1297
+ For example:
1298
+
1299
+ 3 4
1300
+ 4 3
1301
+ 2 5
1302
+ 1 3
1303
+ 3 9
1304
+ 3 3
1305
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
1306
+
1307
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
1308
+
1309
+ In the example list above, the pairs and distances would be as follows:
1310
+
1311
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
1312
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
1313
+ The third-smallest number in both lists is 3, so the distance between them is 0.
1314
+ The next numbers to pair up are 3 and 4, a distance of 1.
1315
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
1316
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
1317
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
1318
+
1319
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?
1320
+
1321
+ Your puzzle answer was 2378066.
1322
+
1323
+ --- Part Two ---
1324
+
1325
+ Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.
1326
+
1327
+ Or are they?
1328
+
1329
+ The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting.
1330
+
1331
+ This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list.
1332
+
1333
+ Here are the same example lists again:
1334
+
1335
+ 3 4
1336
+ 4 3
1337
+ 2 5
1338
+ 1 3
1339
+ 3 9
1340
+ 3 3
1341
+ For these example lists, here is the process of finding the similarity score:
1342
+
1343
+ The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9.
1344
+ The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4.
1345
+ The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0).
1346
+ The fourth number, 1, also does not appear in the right list.
1347
+ The fifth number, 3, appears in the right list three times; the similarity score increases by 9.
1348
+ The last number, 3, appears in the right list three times; the similarity score again increases by 9.
1349
+ So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9).
1350
+
1351
+ Once again consider your left and right lists. What is their similarity score?",18934359,"import re
1352
+ import heapq
1353
+ from collections import Counter
1354
+
1355
+ f = open('day1.txt', 'r')
1356
+
1357
+ list1 = []
1358
+ list2 = []
1359
+ for line in f:
1360
+ splitLine = re.split(r""\s+"", line.strip())
1361
+ list1.append(int(splitLine[0]))
1362
+ list2.append(int(splitLine[1]))
1363
+
1364
+ list2Count = Counter(list2)
1365
+
1366
+ similarityScore = 0
1367
+ for num in list1:
1368
+ if num in list2Count:
1369
+ similarityScore += num * list2Count[num]
1370
+
1371
+ print(similarityScore)
1372
+ ",python:3.9
1373
+ 2024,1,2,"--- Day 1: Historian Hysteria ---
1374
+
1375
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
1376
+
1377
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
1378
+
1379
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
1380
+
1381
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
1382
+
1383
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
1384
+
1385
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
1386
+
1387
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
1388
+
1389
+ For example:
1390
+
1391
+ 3 4
1392
+ 4 3
1393
+ 2 5
1394
+ 1 3
1395
+ 3 9
1396
+ 3 3
1397
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
1398
+
1399
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
1400
+
1401
+ In the example list above, the pairs and distances would be as follows:
1402
+
1403
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
1404
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
1405
+ The third-smallest number in both lists is 3, so the distance between them is 0.
1406
+ The next numbers to pair up are 3 and 4, a distance of 1.
1407
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
1408
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
1409
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
1410
+
1411
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?
1412
+
1413
+ Your puzzle answer was 2378066.
1414
+
1415
+ --- Part Two ---
1416
+
1417
+ Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.
1418
+
1419
+ Or are they?
1420
+
1421
+ The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting.
1422
+
1423
+ This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list.
1424
+
1425
+ Here are the same example lists again:
1426
+
1427
+ 3 4
1428
+ 4 3
1429
+ 2 5
1430
+ 1 3
1431
+ 3 9
1432
+ 3 3
1433
+ For these example lists, here is the process of finding the similarity score:
1434
+
1435
+ The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9.
1436
+ The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4.
1437
+ The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0).
1438
+ The fourth number, 1, also does not appear in the right list.
1439
+ The fifth number, 3, appears in the right list three times; the similarity score increases by 9.
1440
+ The last number, 3, appears in the right list three times; the similarity score again increases by 9.
1441
+ So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9).
1442
+
1443
+ Once again consider your left and right lists. What is their similarity score?",18934359,"from collections import defaultdict
1444
+
1445
+ with open(""day_01.in"") as fin:
1446
+ data = fin.read()
1447
+
1448
+ ans = 0
1449
+ a = []
1450
+ b = []
1451
+
1452
+ for line in data.strip().split(""\n""):
1453
+ nums = [int(i) for i in line.split("" "")]
1454
+ a.append(nums[0])
1455
+ b.append(nums[1])
1456
+
1457
+ counts = defaultdict(int)
1458
+ for x in b:
1459
+ counts[x] += 1
1460
+
1461
+ for x in a:
1462
+ ans += x * counts[x]
1463
+
1464
+ print(ans)
1465
+ ",python:3.9
1466
+ 2024,1,2,"--- Day 1: Historian Hysteria ---
1467
+
1468
+ The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
1469
+
1470
+ As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th.
1471
+
1472
+ Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
1473
+
1474
+ You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office.
1475
+
1476
+ Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search?
1477
+
1478
+ Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs.
1479
+
1480
+ There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
1481
+
1482
+ For example:
1483
+
1484
+ 3 4
1485
+ 4 3
1486
+ 2 5
1487
+ 1 3
1488
+ 3 9
1489
+ 3 3
1490
+ Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
1491
+
1492
+ Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6.
1493
+
1494
+ In the example list above, the pairs and distances would be as follows:
1495
+
1496
+ The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
1497
+ The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
1498
+ The third-smallest number in both lists is 3, so the distance between them is 0.
1499
+ The next numbers to pair up are 3 and 4, a distance of 1.
1500
+ The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
1501
+ Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
1502
+ To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
1503
+
1504
+ Your actual left and right lists contain many location IDs. What is the total distance between your lists?
1505
+
1506
+ Your puzzle answer was 2378066.
1507
+
1508
+ --- Part Two ---
1509
+
1510
+ Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.
1511
+
1512
+ Or are they?
1513
+
1514
+ The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting.
1515
+
1516
+ This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list.
1517
+
1518
+ Here are the same example lists again:
1519
+
1520
+ 3 4
1521
+ 4 3
1522
+ 2 5
1523
+ 1 3
1524
+ 3 9
1525
+ 3 3
1526
+ For these example lists, here is the process of finding the similarity score:
1527
+
1528
+ The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9.
1529
+ The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4.
1530
+ The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0).
1531
+ The fourth number, 1, also does not appear in the right list.
1532
+ The fifth number, 3, appears in the right list three times; the similarity score increases by 9.
1533
+ The last number, 3, appears in the right list three times; the similarity score again increases by 9.
1534
+ So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9).
1535
+
1536
+ Once again consider your left and right lists. What is their similarity score?",18934359,"input = open(""Day 1\input.txt"", ""r"")
1537
+ list1 = []
1538
+ list2 = []
1539
+ for line in input:
1540
+ nums = line.split("" "")
1541
+ list1.append(int(nums[0]))
1542
+ list2.append(int(nums[-1]))
1543
+ list1.sort()
1544
+ list2.sort()
1545
+ total = 0
1546
+ for i in range(len(list1)):
1547
+ num1 = list1[i]
1548
+ simscore = 0
1549
+ for j in range(len(list2)):
1550
+ num2 = list2[j]
1551
+ if num2 == num1:
1552
+ simscore+=1
1553
+ if num2 > num1:
1554
+ break
1555
+ total+= (num1*simscore)
1556
+ print(total)",python:3.9