mirror of
https://github.com/Xevion/advent-of-code.git
synced 2025-12-06 09:14:20 -06:00
2015 day 3 part 1 & 2
This commit is contained in:
1
2015/day-3/input
Normal file
1
2015/day-3/input
Normal file
File diff suppressed because one or more lines are too long
28
2015/day-3/python/part_1.py
Normal file
28
2015/day-3/python/part_1.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
PATH = os.path.join(sys.path[0], '..', 'input')
|
||||
DATA = open(PATH).read()
|
||||
|
||||
pos = [0, 0]
|
||||
|
||||
def UP():
|
||||
pos[1] += 1
|
||||
|
||||
def DOWN():
|
||||
pos[1] -= 1
|
||||
def RIGHT():
|
||||
pos[0] += 1
|
||||
def LEFT():
|
||||
pos[0] -= 1
|
||||
|
||||
DIRECTIONS = {'^' : UP, '<' : LEFT, '>' : RIGHT, 'v' : DOWN}
|
||||
|
||||
houses = defaultdict(int)
|
||||
for direction in DATA:
|
||||
DIRECTIONS[direction]()
|
||||
houses[f'{pos[0]},{pos[1]}'] += 1
|
||||
|
||||
print(len(list(v for v in houses.values() if v >= 1)))
|
||||
32
2015/day-3/python/part_2.py
Normal file
32
2015/day-3/python/part_2.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
from collections import defaultdict
|
||||
from itertools import cycle
|
||||
|
||||
PATH = os.path.join(sys.path[0], '..', 'input')
|
||||
DATA = '--' + open(PATH).read()
|
||||
|
||||
def UP(santa): santa[1] += 1
|
||||
def DOWN(santa): santa[1] -= 1
|
||||
def RIGHT(santa): santa[0] += 1
|
||||
def LEFT(santa): santa[0] -= 1
|
||||
|
||||
DIRECTIONS = {'^' : UP, '<' : LEFT, '>' : RIGHT, 'v' : DOWN, '-' : lambda *args : None}
|
||||
|
||||
humanSanta = [0, 0]
|
||||
roboSanta = [0, 0]
|
||||
houses = defaultdict(int)
|
||||
cycleIter = cycle(range(2))
|
||||
|
||||
for direction in DATA:
|
||||
alternate = next(cycleIter)
|
||||
|
||||
if bool(alternate):
|
||||
DIRECTIONS[direction](roboSanta)
|
||||
houses[f'{roboSanta[0]},{roboSanta[1]}'] += 1
|
||||
else:
|
||||
DIRECTIONS[direction](humanSanta)
|
||||
houses[f'{humanSanta[0]},{humanSanta[1]}'] += 1
|
||||
|
||||
print(len(list(v for v in houses.values() if v >= 1)))
|
||||
Reference in New Issue
Block a user