Skip to content

Commit

Permalink
AoC: And we're (tentatively) off
Browse files Browse the repository at this point in the history
  • Loading branch information
neilstudd committed Dec 1, 2024
1 parent 036d550 commit 58dd06c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
Binary file added 2024/common/__pycache__/common.cpython-311.pyc
Binary file not shown.
22 changes: 22 additions & 0 deletions 2024/common/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os, sys

# Returns the lines of the file, ready to iterate over.
# Returns error if the file is empty.
# Usage:
# my_lines = open_file("input.txt")
def open_file(filename):
file_path = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),filename)
if os.stat(file_path).st_size == 0:
print("\n❌ File " + filename + " is empty!")
sys.exit()
file = open(file_path, 'r')
return file.readlines()

# Prints the answer, and (if there's an expected value to check) verifies the answer.
def print_and_verify_answer(mode, part, answer, expected):
print("\nPart " + part + " " + mode + " answer: " + str(answer))
if expected != None:
if answer != expected:
print("❌ FAILED! Expected", expected, "got", answer)
else:
print("βœ… Output matches expected value")
37 changes: 37 additions & 0 deletions 2024/day01/day01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
from common import open_file, print_and_verify_answer

def construct_and_sort_lists(data_file):
list_one = []
list_two = []
for line in data_file:
numbers = line.split()
list_one.append(int(numbers[0]))
list_two.append(int(numbers[1]))
return list_one, list_two

def run_part_one(mode, expected = None):
data_file = open_file( mode + ".txt")
list_one, list_two = construct_and_sort_lists(data_file)
total_difference = 0
list_one.sort()
list_two.sort()
for i in range(len(list_one)):
total_difference += abs(list_one[i] - list_two[i])
print_and_verify_answer(mode, "one", total_difference, expected)

def run_part_two(mode, expected = None):
data_file = open_file( mode + ".txt")
list_one, list_two = construct_and_sort_lists(data_file)
running_total = 0
for i in range(len(list_one)):
running_total += list_two.count(list_one[i]) * list_one[i]
print_and_verify_answer(mode, "two", running_total, expected)

# ADD EXPECTED OUTPUTS TO TESTS HERE πŸ‘‡
run_part_one("test", 11)
run_part_one("prod", 2066446)
run_part_two("test", 31)
run_part_two("prod", 24931009)
# Now run it and watch the magic happen πŸͺ„
6 changes: 6 additions & 0 deletions 2024/day01/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Started in 2022, but I'll backfill previous years when I need a festive boost.
⬜ = incomplete or not attempted

### Stats
**2024:** 🟧⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ _(2)_
**2023:** 🟨🟨🟨🟨🟧🟨🟨🟧⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ _(16)_
**2022:** πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ§πŸŸ¨πŸŸ¨β¬›β¬œβ¬œβ¬›β¬œβ¬›πŸŸ¨β¬œπŸŸ¨β¬œβ¬› _(36)_
**2021:** 🟨🟨🟨🟨🟨🟨🟨🟨⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ _(16)_

0 comments on commit 58dd06c

Please sign in to comment.