Compare commits

..

1 commit

Author SHA1 Message Date
102988caed
optimized the code and added a bunch of comment
please accept this pr pl0x pl0x pl0x pl0x pl0x pl0x
2022-12-03 14:00:20 -05:00
11 changed files with 24 additions and 1347 deletions

View file

@ -1,22 +0,0 @@
if __name__ == "__main__":
elves = []
with open('input', 'r') as f:
sum_ = 0
for line in f:
if line.strip() == '':
elves.append(sum_)
sum_ = 0
else:
sum_ += int(line.strip())
elves.append(sum_)
elves.sort(reverse=True)
largest = elves[0]
print(f'Max Value: {largest}')
top3 = elves[0] + elves[1] + elves[2]
print(f'Top 3: {top3}')

View file

@ -1,24 +1,32 @@
if __name__ == "__main__":
elves = []
# Initialize empty list to store sums
sums = []
# Open input file
with open('input', 'r') as f:
sum_ = 0
# Initialize sum for current group of numbers
current_sum = 0
# Read lines from file
for line in f:
# If line is empty, append current sum to list and reset sum
if line.strip() == '':
elves.append(sum_)
sum_ = 0
sums.append(current_sum)
current_sum = 0
else:
sum_ += int(line.strip())
# Add number from line to current sum
current_sum += int(line.strip())
elves.append(sum_)
# Add last sum to list
sums.append(current_sum)
largest = max(elves)
print(f'Max Value: {largest}')
# Sort sums in descending order
sums.sort(reverse=True)
top3 = 0
for i in range(3):
largest = max(elves)
top3 += largest
elves.remove(largest)
# Print index and value of largest group of numbers
print(f'Index: 0\nValue: {sums[0]}')
# Calculate sum of top 3 groups of numbers
top3_sum = sums[0] + sums[1] + sums[2]
print(f'Top 3: {top3_sum}')
print(f'Top 3: {top3}')

View file

@ -1,55 +0,0 @@
5324
5176
2197
2701
6185
3901
5392
2065
6467
6085
5062
1841
1197
1318
4901
3443
1403
5570
4336
5672
2532
5627
6038
1099
4305
2317
1382
3226
4427
2612
15638
4118
4687
2243
3532
2089
3937
1146
5069
5728
2962
3099
5882
5448
6064
2642
7996
5334
10384
1106
2742

View file

@ -7,7 +7,7 @@ function find_duplicates(first, second)
for i = 1, #first do
local char = string.sub(first, i, i)
if string.find(second, char) then
if string.match(second, char) then
if char == string.upper(char) then
return string.byte(char) - string.byte("A") + 27
elseif char == string.lower(char) then

View file

@ -1,54 +0,0 @@
for i = 1, 1000 do
-- Open the file for reading
local file = assert(io.open("strings.txt", "r"))
-- Initialize a variable to store the sum of scores
local score = 0
-- Read each line from the file
for line in file:lines() do
-- Split the line in half
local first_half = line:sub(1, #line / 2)
local second_half = line:sub(#line / 2 + 1)
-- Create a table to store the characters in each half
local first_half_chars = {}
local second_half_chars = {}
-- Iterate over each character in each half and store it in the table
for i = 1, #first_half do
first_half_chars[first_half:sub(i, i)] = true
end
for i = 1, #second_half do
second_half_chars[second_half:sub(i, i)] = true
end
-- Create a table to store the characters that have already been counted
local counted_chars = {}
-- Iterate over each character in the first half again
-- and check if it exists in the second half
for i = 1, #first_half do
local char = first_half:sub(i, i)
if second_half_chars[char] and not counted_chars[char] then
-- If it does and it hasn't been counted yet,
-- assign it a score and add it to the sum of scores
local score_value = 0
if char:lower() == char then
-- If the character is lowercase, its score is its ASCII value - 96
score_value = string.byte(char) - 96
else
-- If the character is uppercase, its score is its ASCII value - 64 + 26
score_value = string.byte(char) - 64 + 26
end
score = score + score_value
-- Add the character to the counted_chars table so it won't be counted again
counted_chars[char] = true
end
end
end
-- Print the final sum of scores
print(score)
end

View file

@ -11,7 +11,7 @@ end
function find_duplicates(one, two, three)
for i = 1, #one do
local char = string.sub(one, i, i)
if string.find(two, char) and string.find(three, char) then
if string.match(two, char) and string.match(three, char) then
if char == string.upper(char) then
return string.byte(char) - string.byte("A") + 27
elseif char == string.lower(char) then

View file

@ -1,70 +0,0 @@
This is my solution to the Day 4 puzzle in the 2022 Advent of Code.
It is written in a language called Expression 2, a scripting language for the Wiremod addon in Garry's Mod.
Due to the unusual nature of the language, here is an annotated verison of Part 1:
```golo
@name Advent of Code
@inputs
@outputs
# These variables persist through multiple executions of the script
@persist Input:array [I Overlaps]:number
@trigger
# When the file has successfully uploaded, run this script
runOnFile(1)
# Run this script every 100ms
interval(100)
# When you first place the E2 entity down, upload the input dataset to the server
if(first()) {
fileLoad("input.txt")
}
# If the current reason the script is being executed is the file being uploaded, parse it.
if(fileClk()) {
Input = fileRead():explode("\n")
Input:removeString(Input:count())
I = 1
Overlaps = 0
}
# If the file is fully loaded and the index variable is below the maximum of the array, loop through it.
if(fileLoaded() && I <= Input:count()) {
#printTable(Input)
# This function will loop forever until the execution time limit for the current game tick is almost met, then the loop will break and wait until the next time the script is ran to continue
while(perf()) {
#print(Input[I,string])
Thing = Input[I,string]:explode(",")
Range1 = Thing[1,string]:explode("-")
Range2 = Thing[2,string]:explode("-")
if(Range1[1,string]:toNumber() >= Range2[1,string]:toNumber()
&& Range1[2,string]:toNumber() <= Range2[2,string]:toNumber()) {
#print(I+": True1")
Overlaps++
} elseif(Range2[1,string]:toNumber() >= Range1[1,string]:toNumber()
&& Range2[2,string]:toNumber() <= Range1[2,string]:toNumber()) {
#print(I+": True2")
Overlaps++
} else {
#print(I+": False")
}
I++
if(I > Input:count()) {
break
}
}
}
# If the file is fully loaded and I is above the array size (meaning it is done), print the results
if(fileLoaded() && I > Input:count()) {
print(Overlaps)
print(I)
runOnTick(0)
stoptimer("interval")
}
```

View file

@ -1,55 +0,0 @@
@name Advent of Code
@inputs
@outputs
@persist Input:array [I Overlaps]:number
@trigger
runOnFile(1)
interval(100)
if(first()) {
fileLoad("input.txt")
}
if(fileClk()) {
Input = fileRead():explode("\n")
Input:removeString(Input:count())
I = 1
Overlaps = 0
}
if(fileLoaded() && I <= Input:count()) {
#printTable(Input)
while(perf()) {
#print(Input[I,string])
Thing = Input[I,string]:explode(",")
Range1 = Thing[1,string]:explode("-")
Range2 = Thing[2,string]:explode("-")
if(Range1[1,string]:toNumber() >= Range2[1,string]:toNumber()
&& Range1[2,string]:toNumber() <= Range2[2,string]:toNumber()) {
#print(I+": True1")
Overlaps++
} elseif(Range2[1,string]:toNumber() >= Range1[1,string]:toNumber()
&& Range2[2,string]:toNumber() <= Range1[2,string]:toNumber()) {
#print(I+": True2")
Overlaps++
} else {
#print(I+": False")
}
I++
if(I > Input:count()) {
break
}
}
}
if(fileLoaded() && I > Input:count()) {
print(Overlaps)
print(I)
runOnTick(0)
stoptimer("interval")
}

View file

@ -1,55 +0,0 @@
@name Advent of Code Part 2
@inputs
@outputs
@persist Input:array [I Overlaps]:number
@trigger
runOnFile(1)
interval(100)
if(first()) {
fileLoad("input.txt")
}
if(fileClk()) {
Input = fileRead():explode("\n")
Input:removeString(Input:count())
I = 1
Overlaps = 0
}
if(fileLoaded() && I <= Input:count()) {
#printTable(Input)
while(perf()) {
#print(Input[I,string])
Thing = Input[I,string]:explode(",")
Range1 = Thing[1,string]:explode("-")
Range2 = Thing[2,string]:explode("-")
if(Range2[1,string]:toNumber() <= Range1[2,string]:toNumber()
&& Range2[2,string]:toNumber() >= Range1[1,string]:toNumber()) {
#print(I+": True1")
Overlaps++
} elseif (Range1[1,string]:toNumber() <= Range2[2,string]:toNumber()
&& Range1[2,string]:toNumber() >= Range2[1,string]:toNumber()) {
#print(I+": True2")
Overlaps++
} else {
#print(I+": False")
}
I++
if(I > Input:count()) {
break
}
}
}
if(fileLoaded() && I > Input:count()) {
print(Overlaps)
print(I)
runOnTick(0)
stoptimer("interval")
}

File diff suppressed because it is too large Load diff

View file

@ -1,20 +0,0 @@
51-88,52-87
41-55,22-56
6-74,74-86
51-98,52-86
8-77,3-94
76-76,77-97
29-42,29-35
59-97,60-60
9-86,27-86
58-85,59-85
23-41,26-39
87-91,74-93
13-83,83-84
16-82,15-82
15-95,14-96
1-1,2-93
3-87,2-74
6-47,7-78
47-68,47-92
4-64,5-68