Compare commits

...

2 Commits

Author SHA1 Message Date
6ddcc106ec completed 2017.03.1 2025-07-25 00:02:45 +00:00
8ec6322f51 completed 2017-02.2 2025-07-24 23:07:02 +00:00
3 changed files with 124 additions and 0 deletions

21
2017/02/2checksum.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
if __name__ == '__main__':
total = 0
with open("input", "r", encoding="utf-8") as sheet:
remainder = []
for line in sheet:
values = list(map(int, line.split()))
values.sort()
result = ""
i = 1
while i < len(values) and result == "":
j = 0
while j >= 0 and j < i and result == "":
remainder = values[i] % values[j]
if remainder == 0:
result = int(values[i] / values[j])
j += 1
i += 1
total += result
print(total)

41
2017/03/1spiral_memory.py Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env python3
if __name__ == '__main__':
target = 277678
xstep = 1
ystep = 1
origin = [0, 0]
square = 1
pointer = origin
def inc_step(step):
if step >= 0:
return int(-1 * (step + 1))
else:
return int(-1 * (step - 1))
while square < target:
i = 1
while i <= abs(xstep) and square < target:
square += 1
if xstep >= 0:
pointer[0] += 1
else:
pointer[0] += -1
i += 1
xstep = inc_step(xstep)
i = 1
while i <= abs(ystep) and square < target:
square += 1
if ystep >= 0:
pointer[1] += 1
else:
pointer[1] += -1
i += 1
ystep = inc_step(ystep)
if square == target:
xloc = pointer[0]
yloc = pointer[1]
distance = abs(xloc) + abs(yloc)
print(f"dist to square {target} is {int(abs(xloc))}+{int(abs(yloc))}={distance}")

62
2017/03/notes Normal file
View File

@ -0,0 +1,62 @@
spiral memory!
i think i'll keep a live count of the current position relative to square 1,
slowly stepping one by one through each square sequentially.
my input is only 277678, so this should be fine!
i notice that the pointer starts at square 1, at, say, (0,0),
and then moves one square right, then one up,
then two left, then two down,
then three right, three up, etc
seems like it follows the pattern:
(x1 , y1 ) = (0 , 0 )
(x2 , y2 ) = (x1 + 1 , y1 )
(x3 , y3 ) = (x2 , y1 + 1)
(x5 , y5 ) = (x3 - 2 , y3 )
(x7 , y7 ) = (x5 , y3 - 2)
(x10, y10) = (x5 + 3 , y7 )
etc.
i'll just for loop this shit
something like: (copied to 1spiral_memory.py and worked further from there)
(the following is my first rough sketch. see 1spiral_memory.py)
target = 277678
xstep = 1
ystep = 1
origin = (0, 0)
square = 1
pointer = origin
def inc_step(step):
if step >= 0:
return int(-1 * (step + 1))
else:
return int(-1 * (step - 1))
while square < target:
i = 1
while i <= abs(xstep) and square < target:
square += 1
if xstep >= 0:
pointer += (1, 0)
else:
pointer -= (1, 0)
i += 1
xstep = inc_step(xstep)
i = 1
while i <= abs(ystep) and square < target:
square += 1
if ystep >= 0:
pointer += (0, 1)
else:
pointer -= (0, 1)
ystep = inc_step(ystep)
if square == target:
xloc = pointer[0]
yloc = pointer[1]
distance = abs(xloc) + abs(yloc)