From e7085453864431ace3ad8f3123b259ed0829ae74 Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Thu, 30 Dec 2021 15:11:21 -0800 Subject: all solutions for 2015 --- day3.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 day3.py (limited to 'day3.py') diff --git a/day3.py b/day3.py new file mode 100644 index 0000000..8d5854e --- /dev/null +++ b/day3.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +from collections import defaultdict +with open('day3.txt') as data: + directions = data.read().strip() +# part 1 +x, y = 0, 0 +houses = defaultdict(int) +houses[(x, y)] = 1 +for direction in directions: + match direction: + case '^': + y += 1 + case 'v': + y -= 1 + case '>': + x += 1 + case '<': + x -= 1 + houses[(x, y)] += 1 + +print(len(houses)) + +# part 2 +santaX, santaY, roboX, roboY = 0, 0, 0, 0 +houses = defaultdict(int) +houses[(0, 0)] = 2 +isSanta = True +for direction in directions: + if isSanta: + match direction: + case '^': + santaY += 1 + case 'v': + santaY -= 1 + case '>': + santaX += 1 + case '<': + santaX -= 1 + houses[(santaX, santaY)] += 1 + else: + match direction: + case '^': + roboY += 1 + case 'v': + roboY -= 1 + case '>': + roboX += 1 + case '<': + roboX -= 1 + houses[(roboX, roboY)] += 1 + isSanta = not isSanta +print(len(houses)) -- cgit 1.4.1-2-gfad0