mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Compare commits
4 Commits
16092fe5b2
...
443ff2cee6
| Author | SHA1 | Date | |
|---|---|---|---|
| 443ff2cee6 | |||
| f4a5ffe3ce | |||
| 73f886359b | |||
| 26ee876f7a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -67,3 +67,6 @@ flamegraph.svg
|
||||
|
||||
# Ignore saved inputs
|
||||
inputs/
|
||||
|
||||
# Terraform dir
|
||||
.terraform
|
||||
|
||||
28
2024/bonus/day01/main.tf
Normal file
28
2024/bonus/day01/main.tf
Normal file
@@ -0,0 +1,28 @@
|
||||
variable "input" {
|
||||
type = string
|
||||
}
|
||||
|
||||
locals {
|
||||
cleaned_input = replace(var.input, "/ +/", " ")
|
||||
lines = split("\n", trim(local.cleaned_input, "\n"))
|
||||
lines_split = [for line in local.lines: split(" ", line)]
|
||||
left = [for line in local.lines_split: parseint(line[0], 10)]
|
||||
right = [for line in local.lines_split: parseint(line[1], 10)]
|
||||
|
||||
left_sorted = sort(local.left)
|
||||
right_sorted = sort(local.right)
|
||||
|
||||
diffs = [for i in range(length(local.left_sorted)): abs(local.left_sorted[i] - local.right_sorted[i])]
|
||||
|
||||
counts = {for num in local.right: num => num...}
|
||||
|
||||
matching = [for left in local.left: left * length(lookup(local.counts, left, []))]
|
||||
}
|
||||
|
||||
output "part1" {
|
||||
value = sum(local.diffs)
|
||||
}
|
||||
|
||||
output "part2" {
|
||||
value = sum(local.matching)
|
||||
}
|
||||
19
2024/bonus/day01/sample.tftest.hcl
Normal file
19
2024/bonus/day01/sample.tftest.hcl
Normal file
@@ -0,0 +1,19 @@
|
||||
variables {
|
||||
input = file("../../tests/samples/01.txt")
|
||||
}
|
||||
|
||||
run "run" {
|
||||
|
||||
command = plan
|
||||
|
||||
assert {
|
||||
condition = output.part1 == 11
|
||||
error_message = "Part1 output is wrong"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = output.part2 == 31
|
||||
error_message = "Part2 output is wrong"
|
||||
}
|
||||
|
||||
}
|
||||
16
2024/bonus/main.tf
Normal file
16
2024/bonus/main.tf
Normal file
@@ -0,0 +1,16 @@
|
||||
terraform {
|
||||
|
||||
}
|
||||
|
||||
module "day01" {
|
||||
source = "./day01"
|
||||
input = file("../inputs/01.txt")
|
||||
}
|
||||
|
||||
output "day01_1" {
|
||||
value = module.day01.part1
|
||||
}
|
||||
|
||||
output "day01_2" {
|
||||
value = module.day01.part2
|
||||
}
|
||||
@@ -22,11 +22,11 @@ from aoc import days
|
||||
@click.argument("day", required=True)
|
||||
def main(day: int, timing: bool, data: IO[str]) -> None:
|
||||
runner_class = days.get_runner(day)
|
||||
data = data.read()
|
||||
contents = data.read()
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
|
||||
part1, part2 = runner_class.run_both(data)
|
||||
part1, part2 = runner_class.run_both(contents)
|
||||
|
||||
if timing:
|
||||
elapsed = time.perf_counter_ns() - start
|
||||
|
||||
@@ -10,8 +10,7 @@ from . import CombinedRunner
|
||||
class DayRunner(CombinedRunner):
|
||||
@classmethod
|
||||
def run_both(cls, data: str) -> tuple[Any, Any]:
|
||||
data = StringIO(data)
|
||||
nums = numpy.loadtxt(data, dtype=numpy.int32)
|
||||
nums = numpy.loadtxt(StringIO(data), dtype=numpy.int32)
|
||||
|
||||
left = nums[..., 0]
|
||||
right = nums[..., 1]
|
||||
@@ -21,7 +20,7 @@ class DayRunner(CombinedRunner):
|
||||
|
||||
diff = numpy.abs(left - right).sum()
|
||||
|
||||
counts = defaultdict(int)
|
||||
counts: defaultdict[int, int] = defaultdict(int)
|
||||
for val in right:
|
||||
counts[val] += 1
|
||||
|
||||
|
||||
@@ -2,13 +2,16 @@ import os
|
||||
|
||||
from aoc.days.day1 import DayRunner
|
||||
|
||||
|
||||
def get_data() -> str:
|
||||
sample = os.path.dirname(__file__) + "/samples/01.txt"
|
||||
with open(sample, mode="rt", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
|
||||
def test_sample_part1():
|
||||
|
||||
def test_sample_part1() -> None:
|
||||
assert DayRunner.part1(get_data()) == 11
|
||||
|
||||
def test_sample_part2():
|
||||
|
||||
def test_sample_part2() -> None:
|
||||
assert DayRunner.part2(get_data()) == 31
|
||||
|
||||
Reference in New Issue
Block a user