Skip to content

Commit

Permalink
AoC 2024: day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 1, 2024
1 parent 06a7209 commit 020404c
Show file tree
Hide file tree
Showing 13 changed files with 1,130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ My own solutions to [Advent of Code](https://adventofcode.com/).
[solutions in Python 3](https://github.com/loociano/advent-of-code/tree/master/aoc2022)
* [Advent of Code 2023](https://adventofcode.com/2023)
[solutions in Python 3](https://github.com/loociano/advent-of-code/tree/master/aoc2023)
* [Advent of Code 2024](https://adventofcode.com/2024)
[solutions in Python 3](https://github.com/loociano/advent-of-code/tree/master/aoc2024)
7 changes: 7 additions & 0 deletions aoc2024/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Advent of Code 2024

My own solutions to [Advent of Code 2024](https://adventofcode.com/2024).

> Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as a speed contest, interview prep, company training, university coursework, practice problems, or to challenge each other. —Eric Wastl
* [Day 1: Historian Hysteria](https://adventofcode.com/2023/day/1)[Python3 solution](https://github.com/loociano/advent-of-code/blob/master/aoc2024/src/day01/python/solution.py)
Empty file added aoc2024/__init__.py
Empty file.
Empty file added aoc2024/src/__init__.py
Empty file.
Empty file added aoc2024/src/day01/__init__.py
Empty file.
Empty file.
77 changes: 77 additions & 0 deletions aoc2024/src/day01/python/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import defaultdict
from typing import Sequence

type LocationIds = Sequence[int]

def _parse_input(input: Sequence[str]) -> tuple[LocationIds, LocationIds]:
"""Converts puzzle input into 2 lists of location IDs.
n = len(input)
Time complexity: O(n)
Space complexity: O(n) + O(n) = O(n)
"""
location_ids1 = []
location_ids2 = []
# Parse input:
for line in input: # t:O(n)
# Line format is: '<int>\s\s\s<int>'
id1, id2 = map(int, line.split(' ')) # Assumes valid input.
location_ids1.append(id1)
location_ids2.append(id2)
return location_ids1, location_ids2

def calculate_distance(input: Sequence[str]) -> int:
"""
Calculates total distance between 2 sequences of location IDs.
n = len(input)
Time complexity: O(n) + O(2nlogn) + O(n) = O(nlogn).
Space complexity: O(n) + O(n) = O(n)
Args:
input: Sequence of location ID pairs.
Returns
Total distance.
"""
location_ids1, location_ids2 = _parse_input(input) # O(n)
# Calculate total distance:
location_ids1.sort() # t:O(nlogn)
location_ids2.sort() # t:O(nlogn)
return sum(
abs(location_ids1[i] - location_ids2[i])
for i in range(0, len(location_ids1))) # t:O(n)

def calculate_similarity_score(input: Sequence[str]) -> int:
"""
Calculates similarity score between 2 sequences of location IDs.
n = len(input)
Time complexity: O(n) + O(n) + O(n) = O(n)
Space complexity: O(2n) + O(n) = O(n)
Args:
input: Sequence of location ID pairs.
Returns
Total distance.
"""
location_ids1, location_ids2 = _parse_input(input) # t:O(n)
histogram = defaultdict(int)
# Generate histogram:
for id in location_ids2: # t:O(n)
histogram[id] += 1
# Calculate similarity score:
return sum(id * histogram.get(id, 0) for id in location_ids1) # t:O(n)
Empty file added aoc2024/test/__init__.py
Empty file.
Empty file added aoc2024/test/day01/__init__.py
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions aoc2024/test/day01/python/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
Loading

0 comments on commit 020404c

Please sign in to comment.