Compare commits

...

5 commits

Author SHA1 Message Date
145f1adde4
Added README to Day 4 because language is weird 2022-12-04 01:19:16 -07:00
0d58695d68
Added Day 4 solutions in E2 2022-12-03 23:05:51 -07:00
43958d4529
Added AI generated output because I can 2022-12-03 19:32:21 -07:00
5513d56c4d
Minor performance optimization to D3 2022-12-03 19:03:50 -07:00
ae592b71af
Since everyone wanted me to use lists for D1, here you go
Also, zombie, your PR kinda sucked so I deleted it and made my own
2022-12-03 15:39:19 -07:00
11 changed files with 1345 additions and 13 deletions

22
day1/day1-alt.py Normal file
View file

@ -0,0 +1,22 @@
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,23 +1,24 @@
if __name__ == "__main__":
elves = {}
elves = []
with open('input', 'r') as f:
elf = 0
elves[0] = 0
sum_ = 0
for line in f:
if line.strip() == '':
elf += 1
elves[elf] = 0
elves.append(sum_)
sum_ = 0
else:
elves[elf] += int(line.strip())
sum_ += int(line.strip())
largest = max(elves, key=elves.get)
print(f'Index: {largest}\nValue: {elves[largest]}')
elves.append(sum_)
largest = max(elves)
print(f'Max Value: {largest}')
top3 = 0
for i in range(3):
largest = max(elves, key=elves.get)
top3 += elves[largest]
del elves[largest]
largest = max(elves)
top3 += largest
elves.remove(largest)
print(f'Top 3: {top3}')

55
day1/input2 Normal file
View file

@ -0,0 +1,55 @@
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.match(second, char) then
if string.find(second, char) then
if char == string.upper(char) then
return string.byte(char) - string.byte("A") + 27
elseif char == string.lower(char) then

54
day3/day3ai.lua Normal file
View file

@ -0,0 +1,54 @@
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.match(two, char) and string.match(three, char) then
if string.find(two, char) and string.find(three, char) then
if char == string.upper(char) then
return string.byte(char) - string.byte("A") + 27
elseif char == string.lower(char) then

70
day4/README.md Normal file
View file

@ -0,0 +1,70 @@
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

@ -0,0 +1,55 @@
@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

@ -0,0 +1,55 @@
@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")
}

1000
day4/input.txt Normal file

File diff suppressed because it is too large Load diff

20
day4/input2.txt Normal file
View file

@ -0,0 +1,20 @@
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