-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
122 lines (105 loc) · 3.69 KB
/
day1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import sys
#Add the dir above day run as path for easy import
root_folder = os.path.abspath(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root_folder)
from utils.support import log_time, _877_cache_now, logger, console
from utils import support
from datetime import datetime
from collections import Counter
#Set day/year global variables
DAY:int = 1 #datetime.now().day
YEAR:int = 2024 #datetime.now().year
def problemsolver(arr:list, part:int):
left, right, diffs = [], [], []
#Transform input strings to two lists
for row in arr:
l, r = row.split()
left.append(int(l))
right.append(int(r))
#Sort the lists
left = sorted(left)
right = sorted(right)
if part == 1:
#Find their differences
for l, r in zip(left, right):
diffs.append(abs(l - r))
if part == 2:
res = Counter(right)
for val in left:
if val in right:
diffs.append(val * res[val])
return sum(diffs)
@log_time
def part_A():
logger.info("Solving part A")
#to check your cache status when you need cache nooooow
#call J.... G.... WENTWORTH.
_877_cache_now() #Lol. I blame myself
#Pull puzzle description and testdata
tellstory, testdata = support.pull_puzzle(DAY, YEAR, 1)
# console.log(f"{tellstory}")
# [logger.info(row) for row in testdata]
#Solve puzzle w/testcase
testcase = problemsolver(testdata, 1)
#Assert testcase
assert testcase == 11, "Test case A failed"
#Solve puzzle with full dataset
answerA = problemsolver(data, 1)
return answerA
@log_time
def part_B():
logger.info("Solving part B")
#Check cache status
_877_cache_now()
#Pull puzzle description and testdata
tellstory, testdata = support.pull_puzzle(DAY, YEAR, 2)
# console.log(f"{tellstory}")
# [logger.info(row) for row in testdata]
#Solve puzzle w/testcase
testcase = problemsolver(testdata, 2)
#Assert testcase
assert testcase == 31, "Test case B failed"
#Solve puzzle with full dataset
answerB = problemsolver(data, 2)
return answerB
def main():
global data
data = support.pull_inputdata(DAY, YEAR)
#Solve part A
resultA = part_A()
logger.info(f"part A solution: \n{resultA}\n")
# support.submit_answer(DAY, YEAR, 1, resultA)
#Solve part B
resultB = part_B()
logger.info(f"part B solution: \n{resultB}\n")
# support.submit_answer(DAY, YEAR, 2, resultB)
#Recurse lines of code
LOC = support.recurse_dir(f'./scripts/day{DAY}/')
logger.info(f"Lines of code \n{LOC}")
#Delete the cache after submission
_877_cache_now(".cache", True)
if __name__ == "__main__":
main()
########################################################
#Notes
#Part A Notes
#The theme this year is searching for the Chief Historian! Everyday will be a
#new location where we will try to find their location before Xmas! They were sent out
#on expedition to scout historical sites and they've gone missing!
#
#Starting with an empty list, The search will start at the chief historians office. He's not there
#But they did find lists of where they were going to go for the year. As the historians
#split into two groups, each groups tries to reconcile a list of location id's of where
#the chief historian might have gone.
#
#Initially the two lists are very disimilar.
#
# 1. We'll first sort each list from smallest to largest
# 2. for each pair, what is their difference.
# 3. Add up the list of differences. Easy peasy
#Part B
#For each value in the left list, if it exists in the right list,
#Add the value multipled by its number of occurances to the diffs.
#sum the diffs
#Fun start!