Skip to content

Commit

Permalink
Part 1 works with day 13 test input
Browse files Browse the repository at this point in the history
  • Loading branch information
dale-c-anderson committed Dec 13, 2022
1 parent 8019efb commit 11bb6d7
Showing 1 changed file with 66 additions and 11 deletions.
77 changes: 66 additions & 11 deletions 2022/13/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,80 @@
import argparse
import logging
import sys
import json


def main(data0):
data0 = data0.split('\n\n')

if args.part1 or not args.part2:
part1_answer = part1(data0.splitlines())
part1_answer = part1(data0)
print(f'Part 1: {part1_answer}')

if args.part2 or not args.part1:
part2_answer = part2(data0.splitlines())
part2_answer = part2(data0)
print(f'Part 2: {part2_answer}')


def part1(data1):
return data1


def part2(data2):
return data2
global level
level = 0
def part1(data):
corrects = []
for index, pair in enumerate(data):
left = json.loads(pair.split('\n')[0])
right = json.loads(pair.split('\n')[1])
if left_side_is_smaller(left, right):
corrects.append(index + 1)
log.debug('')
return sum(corrects)


def left_side_is_smaller(left, right):
global level
level += 1
indent = ' ' * level
ret = None # Default == no determination ... keep checking.
log.debug(f'{indent} compare left: {left}, right: {right}, type: {type(left)}, type: {type(right)}')
if type(left) is list and type(right) is list:
while len(left) > 0 and len(right) > 0:
sub_left = left.pop(0)
sub_right = right.pop(0)
log.debug(f'{indent} both are lists. Comparing sub items. sub_left: {sub_left}, sub_right: {sub_right}')
sub_ret = left_side_is_smaller(sub_left, sub_right)
if sub_ret is not None:
level -= 1
return sub_ret
if len(right) > 0:
log.debug(f'{indent} Left ran out of items. Order is good.')
ret = True
elif len(left) > 0:
log.debug(f'{indent} Right ran out of items. Order is bad.')
ret = False
elif type(left) is int and type(right) is list:
log.debug(f'{indent} Mixed types. Convert and compare sub items.')
left = [left]
sub_left = left.pop(0)
sub_right = right.pop(0)
ret = left_side_is_smaller(sub_left, sub_right)
elif type(left) is list and type(right) is int:
log.debug(f'{indent} Mixed types. Convert and compare sub items.')
right = [right]
sub_left = left.pop(0)
sub_right = right.pop(0)
ret = left_side_is_smaller(sub_left, sub_right)
elif type(left) is int and type(right) is int and left == right:
log.debug(f'{indent} both are ints. Dont return anything. Keep comparing.')
elif type(left) is int and type(right) is int and left < right:
log.debug(f'{indent} both are ints, and left is smaller. Order good.')
ret = True
elif type(left) is int and type(right) is int and left > right:
log.debug(f'{indent} both are ints, and left is larger. Order bad.')
ret = False
level -= 1
log.debug(f'{indent} return: {ret}')
return ret

def part2(data):
return 0


if __name__ == "__main__":
Expand Down Expand Up @@ -56,7 +111,7 @@ def part2(data2):

# Prefix log output with timestamps
default_date_format = '%Y-%m-%d %H:%M:%S'
default_log_format = '%(asctime)s.%(msecs)03d %(levelname)s %(module)s/%(funcName)s(): %(message)s'
default_log_format = '%(levelname)s: %(message)s'

# A named logger lets us add more handlers if/when needed.
log = logging.getLogger("foo")
Expand All @@ -71,4 +126,4 @@ def part2(data2):
console_handler.setFormatter(logging.Formatter(default_log_format, datefmt=default_date_format))
log.addHandler(console_handler)

main(data)
main(data)

0 comments on commit 11bb6d7

Please sign in to comment.