Easy one today. And on the first day I switched to python

This commit is contained in:
Connor Johnstone
2023-12-14 23:11:11 -07:00
parent d5b3be1b2c
commit b7343dc6d5
8 changed files with 159 additions and 0 deletions

12
day_15/Pipfile Normal file
View File

@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
numpy = "*"
[dev-packages]
[requires]
python_version = "3.11"

64
day_15/Pipfile.lock generated Normal file
View File

@@ -0,0 +1,64 @@
{
"_meta": {
"hash": {
"sha256": "09a1230e580eaa3316a4e22c8cde61e6c6f10a97b72d483d6afb8e89cfdda897"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.11"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"numpy": {
"hashes": [
"sha256:06fa1ed84aa60ea6ef9f91ba57b5ed963c3729534e6e54055fc151fad0423f0a",
"sha256:174a8880739c16c925799c018f3f55b8130c1f7c8e75ab0a6fa9d41cab092fd6",
"sha256:1a13860fdcd95de7cf58bd6f8bc5a5ef81c0b0625eb2c9a783948847abbef2c2",
"sha256:1cc3d5029a30fb5f06704ad6b23b35e11309491c999838c31f124fee32107c79",
"sha256:22f8fc02fdbc829e7a8c578dd8d2e15a9074b630d4da29cda483337e300e3ee9",
"sha256:26c9d33f8e8b846d5a65dd068c14e04018d05533b348d9eaeef6c1bd787f9919",
"sha256:2b3fca8a5b00184828d12b073af4d0fc5fdd94b1632c2477526f6bd7842d700d",
"sha256:2beef57fb031dcc0dc8fa4fe297a742027b954949cabb52a2a376c144e5e6060",
"sha256:36340109af8da8805d8851ef1d74761b3b88e81a9bd80b290bbfed61bd2b4f75",
"sha256:3703fc9258a4a122d17043e57b35e5ef1c5a5837c3db8be396c82e04c1cf9b0f",
"sha256:3ced40d4e9e18242f70dd02d739e44698df3dcb010d31f495ff00a31ef6014fe",
"sha256:4a06263321dfd3598cacb252f51e521a8cb4b6df471bb12a7ee5cbab20ea9167",
"sha256:4eb8df4bf8d3d90d091e0146f6c28492b0be84da3e409ebef54349f71ed271ef",
"sha256:5d5244aabd6ed7f312268b9247be47343a654ebea52a60f002dc70c769048e75",
"sha256:64308ebc366a8ed63fd0bf426b6a9468060962f1a4339ab1074c228fa6ade8e3",
"sha256:6a3cdb4d9c70e6b8c0814239ead47da00934666f668426fc6e94cce869e13fd7",
"sha256:854ab91a2906ef29dc3925a064fcd365c7b4da743f84b123002f6139bcb3f8a7",
"sha256:94cc3c222bb9fb5a12e334d0479b97bb2df446fbe622b470928f5284ffca3f8d",
"sha256:96ca5482c3dbdd051bcd1fce8034603d6ebfc125a7bd59f55b40d8f5d246832b",
"sha256:a2bbc29fcb1771cd7b7425f98b05307776a6baf43035d3b80c4b0f29e9545186",
"sha256:a4cd6ed4a339c21f1d1b0fdf13426cb3b284555c27ac2f156dfdaaa7e16bfab0",
"sha256:aa18428111fb9a591d7a9cc1b48150097ba6a7e8299fb56bdf574df650e7d1f1",
"sha256:aa317b2325f7aa0a9471663e6093c210cb2ae9c0ad824732b307d2c51983d5b6",
"sha256:b04f5dc6b3efdaab541f7857351aac359e6ae3c126e2edb376929bd3b7f92d7e",
"sha256:b272d4cecc32c9e19911891446b72e986157e6a1809b7b56518b4f3755267523",
"sha256:b361d369fc7e5e1714cf827b731ca32bff8d411212fccd29ad98ad622449cc36",
"sha256:b96e7b9c624ef3ae2ae0e04fa9b460f6b9f17ad8b4bec6d7756510f1f6c0c841",
"sha256:baf8aab04a2c0e859da118f0b38617e5ee65d75b83795055fb66c0d5e9e9b818",
"sha256:bcc008217145b3d77abd3e4d5ef586e3bdfba8fe17940769f8aa09b99e856c00",
"sha256:bd3f0091e845164a20bd5a326860c840fe2af79fa12e0469a12768a3ec578d80",
"sha256:cc392fdcbd21d4be6ae1bb4475a03ce3b025cd49a9be5345d76d7585aea69440",
"sha256:d73a3abcac238250091b11caef9ad12413dab01669511779bc9b29261dd50210",
"sha256:f43740ab089277d403aa07567be138fc2a89d4d9892d113b76153e0e412409f8",
"sha256:f65738447676ab5777f11e6bbbdb8ce11b785e105f690bc45966574816b6d3ea",
"sha256:f79b231bf5c16b1f39c7f4875e1ded36abee1591e98742b05d8a0fb55d8a3eec",
"sha256:fe6b44fb8fcdf7eda4ef4461b97b3f63c466b27ab151bec2366db8b197387841"
],
"index": "pypi",
"markers": "python_version >= '3.9'",
"version": "==1.26.2"
}
},
"develop": {}
}

1
day_15/input.txt Normal file

File diff suppressed because one or more lines are too long

1
day_15/input_test.txt Normal file
View File

@@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7

5
day_15/main.py Normal file
View File

@@ -0,0 +1,5 @@
import part1 as p1
import part2 as p2
print(p1.run())
print(p2.run())

22
day_15/part1.py Normal file
View File

@@ -0,0 +1,22 @@
def test(input, solution):
print(f"Running input {input}")
result = process(input)
print(f"Solution was {result}")
assert result == solution
def run():
file = open("input.txt", "r")
return process(file.read()[:-1])
def hash_value(word):
count = 0
for character in word:
count += ord(character)
count *= 17
count = count % 256
return count
def process(input):
words = input.split(",")
hashes = [hash_value(word) for word in words]
return sum(hashes)

47
day_15/part2.py Normal file
View File

@@ -0,0 +1,47 @@
def test(input, solution):
print(f"Running input {input}")
result = process(input)
print(f"Solution was {result}")
assert result == solution
def run():
file = open("input.txt", "r")
return process(file.read()[:-1])
def hash_value(word):
count = 0
for character in word:
count += ord(character)
count *= 17
count = count % 256
return count
def process(input):
labels = []
focal_lengths = []
for _ in range(256):
labels.append([])
focal_lengths.append([])
words = input.split(",")
for word in words:
if word[-1] == "-":
label = word[:-1]
index = hash_value(label)
if label in labels[index]:
inner_index = labels[index].index(label)
labels[index].pop(inner_index)
focal_lengths[index].pop(inner_index)
else:
[label, focal_length] = word.split("=")
index = hash_value(label)
if label in labels[index]:
inner_index = labels[index].index(label)
focal_lengths[index][inner_index] = focal_length
else:
labels[index].append(label)
focal_lengths[index].append(focal_length)
total = 0
for (box_number, (label_list, focal_length_list)) in enumerate(zip(labels, focal_lengths)):
for (lens_number, (label, focal_length)) in enumerate(zip(label_list, focal_length_list)):
total += (box_number + 1) * (lens_number + 1) * int(focal_length)
return total

7
day_15/test.py Normal file
View File

@@ -0,0 +1,7 @@
import part1
import part2
part1.test("HASH", 52)
part1.test("rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7", 1320)
part2.test("rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7", 145)