d10 bootstrap
parent
17c3671974
commit
b5cc74e536
|
@ -0,0 +1,4 @@
|
||||||
|
if command -v nix-shell &> /dev/null
|
||||||
|
then
|
||||||
|
use flake
|
||||||
|
fi
|
|
@ -0,0 +1,2 @@
|
||||||
|
.direnv
|
||||||
|
target
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "d9"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "d9"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1 @@
|
||||||
|
# Bootstrapping clojure project on
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "aoc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
|
||||||
|
Sensor at x=9, y=16: closest beacon is at x=10, y=16
|
||||||
|
Sensor at x=13, y=2: closest beacon is at x=15, y=3
|
||||||
|
Sensor at x=12, y=14: closest beacon is at x=10, y=16
|
||||||
|
Sensor at x=10, y=20: closest beacon is at x=10, y=16
|
||||||
|
Sensor at x=14, y=17: closest beacon is at x=10, y=16
|
||||||
|
Sensor at x=8, y=7: closest beacon is at x=2, y=10
|
||||||
|
Sensor at x=2, y=0: closest beacon is at x=2, y=10
|
||||||
|
Sensor at x=0, y=11: closest beacon is at x=2, y=10
|
||||||
|
Sensor at x=20, y=14: closest beacon is at x=25, y=17
|
||||||
|
Sensor at x=17, y=20: closest beacon is at x=21, y=22
|
||||||
|
Sensor at x=16, y=7: closest beacon is at x=15, y=3
|
||||||
|
Sensor at x=14, y=3: closest beacon is at x=15, y=3
|
||||||
|
Sensor at x=20, y=1: closest beacon is at x=15, y=3
|
|
@ -0,0 +1,34 @@
|
||||||
|
Sensor at x=3842919, y=126080: closest beacon is at x=3943893, y=1918172
|
||||||
|
Sensor at x=406527, y=2094318: closest beacon is at x=-1066, y=1333278
|
||||||
|
Sensor at x=2208821, y=3683408: closest beacon is at x=2914373, y=3062268
|
||||||
|
Sensor at x=39441, y=1251806: closest beacon is at x=-1066, y=1333278
|
||||||
|
Sensor at x=3093352, y=2404566: closest beacon is at x=2810772, y=2699609
|
||||||
|
Sensor at x=3645473, y=2234498: closest beacon is at x=3943893, y=1918172
|
||||||
|
Sensor at x=3645012, y=2995540: closest beacon is at x=4001806, y=2787325
|
||||||
|
Sensor at x=18039, y=3083937: closest beacon is at x=103421, y=3007511
|
||||||
|
Sensor at x=2375680, y=551123: closest beacon is at x=2761373, y=2000000
|
||||||
|
Sensor at x=776553, y=123250: closest beacon is at x=-1066, y=1333278
|
||||||
|
Sensor at x=2884996, y=2022644: closest beacon is at x=2761373, y=2000000
|
||||||
|
Sensor at x=1886537, y=2659379: closest beacon is at x=2810772, y=2699609
|
||||||
|
Sensor at x=3980015, y=3987237: closest beacon is at x=3844688, y=3570059
|
||||||
|
Sensor at x=3426483, y=3353349: closest beacon is at x=3844688, y=3570059
|
||||||
|
Sensor at x=999596, y=1165648: closest beacon is at x=-1066, y=1333278
|
||||||
|
Sensor at x=2518209, y=2287271: closest beacon is at x=2761373, y=2000000
|
||||||
|
Sensor at x=3982110, y=3262128: closest beacon is at x=3844688, y=3570059
|
||||||
|
Sensor at x=3412896, y=3999288: closest beacon is at x=3844688, y=3570059
|
||||||
|
Sensor at x=2716180, y=2798731: closest beacon is at x=2810772, y=2699609
|
||||||
|
Sensor at x=3575486, y=1273265: closest beacon is at x=3943893, y=1918172
|
||||||
|
Sensor at x=7606, y=2926795: closest beacon is at x=103421, y=3007511
|
||||||
|
Sensor at x=2719370, y=2062251: closest beacon is at x=2761373, y=2000000
|
||||||
|
Sensor at x=1603268, y=1771299: closest beacon is at x=2761373, y=2000000
|
||||||
|
Sensor at x=3999678, y=1864727: closest beacon is at x=3943893, y=1918172
|
||||||
|
Sensor at x=3157947, y=2833781: closest beacon is at x=2914373, y=3062268
|
||||||
|
Sensor at x=3904662, y=2601010: closest beacon is at x=4001806, y=2787325
|
||||||
|
Sensor at x=3846359, y=1608423: closest beacon is at x=3943893, y=1918172
|
||||||
|
Sensor at x=2831842, y=3562642: closest beacon is at x=2914373, y=3062268
|
||||||
|
Sensor at x=3157592, y=1874755: closest beacon is at x=2761373, y=2000000
|
||||||
|
Sensor at x=934300, y=2824967: closest beacon is at x=103421, y=3007511
|
||||||
|
Sensor at x=3986911, y=1907590: closest beacon is at x=3943893, y=1918172
|
||||||
|
Sensor at x=200888, y=3579976: closest beacon is at x=103421, y=3007511
|
||||||
|
Sensor at x=967209, y=3837958: closest beacon is at x=103421, y=3007511
|
||||||
|
Sensor at x=3998480, y=1972726: closest beacon is at x=3943893, y=1918172
|
|
@ -0,0 +1,94 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"flake-utils": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1667395993,
|
||||||
|
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"flake-utils_2": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1659877975,
|
||||||
|
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1670064435,
|
||||||
|
"narHash": "sha256-+ELoY30UN+Pl3Yn7RWRPabykwebsVK/kYE9JsIsUMxQ=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "61a8a98e6d557e6dd7ed0cdb54c3a3e3bbc5e25c",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs_2": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1665296151,
|
||||||
|
"narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixpkgs-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs",
|
||||||
|
"rust-overlay": "rust-overlay"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-overlay": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils_2",
|
||||||
|
"nixpkgs": "nixpkgs_2"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1670552927,
|
||||||
|
"narHash": "sha256-lCE51eAGrAFS4k9W5aDGFpVtOAwQQ/rFMN80PCDh0vo=",
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"rev": "a0fdafd18c9cf599fde17fbaf07dbb20fa57eecb",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
{
|
||||||
|
description = "Python + Rust workspace for AoC";
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
rust-overlay.url = "github:oxalica/rust-overlay";
|
||||||
|
};
|
||||||
|
outputs = { nixpkgs, flake-utils, rust-overlay, ... } @ inputs:
|
||||||
|
flake-utils.lib.eachSystem flake-utils.lib.defaultSystems (sys:
|
||||||
|
let
|
||||||
|
overlays = [ rust-overlay.overlays.default ];
|
||||||
|
pkgs = import nixpkgs { system = sys; overlays = overlays; };
|
||||||
|
shellHookAfter = ''
|
||||||
|
echo "The input files should be placed under ./data/{submission,example}.txt"
|
||||||
|
echo "This problem shares one input between two parts"
|
||||||
|
'';
|
||||||
|
py_pkgs = [ pkgs.python310 ];
|
||||||
|
rs_pkgs = [
|
||||||
|
pkgs.openssl
|
||||||
|
pkgs.pkg-config
|
||||||
|
|
||||||
|
# Add rust-src, which rust-analyzer seems to rely upon
|
||||||
|
(pkgs.rust-bin.selectLatestNightlyWith
|
||||||
|
(
|
||||||
|
toolchain:
|
||||||
|
toolchain.default.override {
|
||||||
|
extensions = [ "rust-src" ];
|
||||||
|
}
|
||||||
|
))
|
||||||
|
# rust devex
|
||||||
|
pkgs.bacon # kinda like nodemon
|
||||||
|
pkgs.rust-analyzer
|
||||||
|
];
|
||||||
|
in
|
||||||
|
{
|
||||||
|
# Jack of all trades
|
||||||
|
devShell = pkgs.mkShell
|
||||||
|
{
|
||||||
|
buildInputs = py_pkgs ++ rs_pkgs;
|
||||||
|
shellHook = ''
|
||||||
|
echo "> Default runtime. This contains both rust and python3 env"
|
||||||
|
echo "Run ./run-py.sh for Python's output and ./run-rs.sh for Rust's output"
|
||||||
|
'' + shellHookAfter;
|
||||||
|
};
|
||||||
|
devShells = {
|
||||||
|
# nix develop ./#lua
|
||||||
|
# lua = pkgs.mkShell {
|
||||||
|
# nativeBuildInputs = lua_pkgs;
|
||||||
|
# shellHook = ''
|
||||||
|
# echo "> Lua runtime"
|
||||||
|
# echo "Run ./run-lua.sh to see the output of the solution"
|
||||||
|
# '' + shellHookAfter;
|
||||||
|
# };
|
||||||
|
|
||||||
|
# nix develop ./#fennel
|
||||||
|
|
||||||
|
# nix develop ./#python
|
||||||
|
rust = pkgs.mkShell {
|
||||||
|
nativeBuildInputs = rs_pkgs;
|
||||||
|
shellHook = ''
|
||||||
|
echo "> Rust runtime"
|
||||||
|
echo "Run ./run-rs.sh to see output of the solution"
|
||||||
|
echo "For quick feedback loop: bacon check-all"
|
||||||
|
'' + shellHookAfter;
|
||||||
|
};
|
||||||
|
python = pkgs.mkShell {
|
||||||
|
nativeBuildInputs = py_pkgs;
|
||||||
|
shellHook = ''
|
||||||
|
echo "> Python3 runtime"
|
||||||
|
echo "Run ./run-py.sh to see the output of the solution"
|
||||||
|
'' + shellHookAfter;
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env sh
|
||||||
|
echo "example"
|
||||||
|
python3 ./src/main.py ./data/example.txt
|
||||||
|
|
||||||
|
echo "submission"
|
||||||
|
python3 ./src/main.py ./data/submission.txt
|
||||||
|
|
|
@ -0,0 +1,151 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
from typing import Iterable, Optional
|
||||||
|
from dataclasses import dataclass
|
||||||
|
import os
|
||||||
|
import string
|
||||||
|
from functools import reduce
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class RectBound:
|
||||||
|
bot_left: tuple[int, int]
|
||||||
|
top_right: tuple[int, int]
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Sensor:
|
||||||
|
loc: tuple[int, int]
|
||||||
|
radius_1k: int
|
||||||
|
nearest_beacon: tuple[int, int]
|
||||||
|
_rect_bound: Optional[RectBound] = None # inclusive
|
||||||
|
|
||||||
|
def rect_bound(self):
|
||||||
|
if not self._rect_bound:
|
||||||
|
self._rect_bound = RectBound(\
|
||||||
|
(self.loc[0]-self.radius_1k, self.loc[1]-self.radius_1k),
|
||||||
|
(self.loc[0]+self.radius_1k, self.loc[1]+self.radius_1k),
|
||||||
|
)
|
||||||
|
return self._rect_bound
|
||||||
|
|
||||||
|
def vec2d_str(x: str, y: str):
|
||||||
|
return (int(x), int(y))
|
||||||
|
|
||||||
|
def vec_diff(lhs: Iterable[int], rhs: Iterable[int]):
|
||||||
|
return tuple(l - r for l, r in zip(lhs, rhs))
|
||||||
|
|
||||||
|
def norm_1k(vec: Iterable[int]):
|
||||||
|
return sum(abs(e) for e in vec)
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SolutionBound:
|
||||||
|
min_x: int
|
||||||
|
min_y: int
|
||||||
|
max_x: int
|
||||||
|
max_y: int
|
||||||
|
|
||||||
|
def solution_bound(sensors: list[Sensor]):
|
||||||
|
return SolutionBound(min(sensor.loc[0] - sensor.radius_1k for sensor in sensors),
|
||||||
|
min(sensor.loc[1] - sensor.radius_1k for sensor in sensors),
|
||||||
|
max(sensor.loc[0] + sensor.radius_1k for sensor in sensors),
|
||||||
|
max(sensor.loc[1] + sensor.radius_1k for sensor in sensors),
|
||||||
|
)
|
||||||
|
|
||||||
|
def which_sensor(loc: tuple[int, int], sensors: list[Sensor]):
|
||||||
|
"""
|
||||||
|
Arbitrary sensor number, not nearest or anything.
|
||||||
|
Actually, this fancies the lower index
|
||||||
|
"""
|
||||||
|
for i, sensor in enumerate(sensors):
|
||||||
|
if norm_1k(vec_diff(sensor.loc, loc)) <= sensor.radius_1k:
|
||||||
|
return (i, sensor if sensor.loc == loc else None)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def beacons_set(sensors: list[Sensor]):
|
||||||
|
return set(sensor.nearest_beacon for sensor in sensors)
|
||||||
|
|
||||||
|
def gen_graph(sensors: list[Sensor], sol_bound: Optional[SolutionBound] = None):
|
||||||
|
sol_bound = sol_bound if sol_bound is not None else solution_bound(sensors)
|
||||||
|
beacons = beacons_set(sensors)
|
||||||
|
return [[which_sensor((x, y), sensors) if (x, y) not in beacons else 'B'
|
||||||
|
for x in range(sol_bound.min_x, sol_bound.max_x + 1)]
|
||||||
|
for y in range(sol_bound.min_y, sol_bound.max_y + 1)]
|
||||||
|
|
||||||
|
def render_graph(graph: list[list[None | tuple[int, Optional[Sensor]] | str]]):
|
||||||
|
def sensor_display(x: None | tuple[int, Optional[Sensor]] | str):
|
||||||
|
match x:
|
||||||
|
case (int(v), s):
|
||||||
|
sensor = ("0123456789"+string.ascii_lowercase)
|
||||||
|
signal = (")!@#$%^&*("+string.ascii_uppercase)
|
||||||
|
return (signal if s is not None else sensor)[v]
|
||||||
|
case None:
|
||||||
|
return '.'
|
||||||
|
case str(s):
|
||||||
|
return s
|
||||||
|
case e:
|
||||||
|
raise RuntimeError(f"Unexpected {e}")
|
||||||
|
|
||||||
|
return list(" ".join(sensor_display(e) if e else '.' for e in row) for row in graph)
|
||||||
|
|
||||||
|
def sensor_range_at(sensor: Sensor, y: int):
|
||||||
|
effective_radius = max(sensor.radius_1k - abs(y - sensor.loc[1]), 0)
|
||||||
|
if effective_radius == 0 and sensor.loc[1] != y:
|
||||||
|
return iter([])
|
||||||
|
return range(sensor.loc[0] - effective_radius, sensor.loc[0] + effective_radius + 1)
|
||||||
|
|
||||||
|
def index_render_graph(rendered_graph: list[str], sol: SolutionBound):
|
||||||
|
return os.linesep.join(f"{str(i + sol.min_y).zfill(3)} {row}"
|
||||||
|
for i, row in enumerate(rendered_graph))
|
||||||
|
|
||||||
|
def part1(sensors: list[Sensor], y:int):
|
||||||
|
sol = solution_bound(sensors)
|
||||||
|
print(sol)
|
||||||
|
# rendered_graph = render_graph(gen_graph(sensors, sol))
|
||||||
|
# print(index_render_graph(rendered_graph, sol))
|
||||||
|
beacons = beacons_set(sensors)
|
||||||
|
ranges = list(set(sensor_range_at(sensor, y))
|
||||||
|
for sensor in sensors)
|
||||||
|
print("ranges", ranges)
|
||||||
|
x_sense = reduce(lambda lhs, rhs: lhs.union(rhs), ranges).difference({beacon[0] for beacon in beacons if beacon[1] == y})
|
||||||
|
# return sum((x, y) not in beacons and which_sensor((x, y), sensors) is not None
|
||||||
|
# for x in range(sol.min_x, sol.max_x + 1))
|
||||||
|
print("x_sense", x_sense)
|
||||||
|
return len(x_sense)
|
||||||
|
|
||||||
|
def part2(sensors: list[Sensor], y:int):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def sensor_from_input_line(line: str):
|
||||||
|
# print(line)
|
||||||
|
spl = line.split("=")
|
||||||
|
# print(spl)
|
||||||
|
_, sensor_x, sensor_y, beacon_x, beacon_y =spl
|
||||||
|
digit_only = lambda x: "".join(filter(lambda v: isinstance(v, str) and v in "0123456789-", x))
|
||||||
|
sensor_loc = vec2d_str(digit_only(sensor_x), digit_only(sensor_y))
|
||||||
|
beacon_loc = vec2d_str(digit_only(beacon_x), beacon_y)
|
||||||
|
return Sensor(sensor_loc, norm_1k(vec_diff(beacon_loc, sensor_loc)), beacon_loc)
|
||||||
|
|
||||||
|
def main(lines: Iterable[str]):
|
||||||
|
sensors = list(sensor_from_input_line(stripped_line)
|
||||||
|
for stripped_line in (line.strip() for line in lines) if len(stripped_line) > 0)
|
||||||
|
beacons = beacons_set(sensors)
|
||||||
|
print(os.linesep.join(repr(sensor) for sensor in sensors))
|
||||||
|
print("beacons:", beacons)
|
||||||
|
y1, y2 = 10, 2000000
|
||||||
|
print("part1", part1(sensors, y1), part1(sensors, y2))
|
||||||
|
# print("part2", part2(sensors, y1), part2(sensors, y2))
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _main(filename: str):
|
||||||
|
with open(filename, "r") as f:
|
||||||
|
main(f)
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
_main(sys.argv[1])
|
||||||
|
|
||||||
|
# For neovim integrated repl which can evaluate per selection
|
||||||
|
REPL = """
|
||||||
|
_main("./data/example.txt")
|
||||||
|
_main("./data/submission.txt")
|
||||||
|
|
||||||
|
"""
|
|
@ -0,0 +1,6 @@
|
||||||
|
fn main() {
|
||||||
|
// let test = include_str!("../data/example.txt");
|
||||||
|
let sub = inlcude_str!("../data/submission.txt");
|
||||||
|
let commands = sub.lines()
|
||||||
|
.map(|line| line.split(" "))
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
if command -v nix-shell &> /dev/null
|
||||||
|
then
|
||||||
|
use flake
|
||||||
|
fi
|
|
@ -0,0 +1,2 @@
|
||||||
|
.direnv
|
||||||
|
target
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "d9"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "d9"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1 @@
|
||||||
|
# Bootstrapping clojure project on
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "aoc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
R 4
|
||||||
|
U 4
|
||||||
|
L 3
|
||||||
|
D 1
|
||||||
|
R 4
|
||||||
|
D 1
|
||||||
|
L 5
|
||||||
|
R 2
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,94 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"flake-utils": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1667395993,
|
||||||
|
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"flake-utils_2": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1659877975,
|
||||||
|
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1670064435,
|
||||||
|
"narHash": "sha256-+ELoY30UN+Pl3Yn7RWRPabykwebsVK/kYE9JsIsUMxQ=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "61a8a98e6d557e6dd7ed0cdb54c3a3e3bbc5e25c",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs_2": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1665296151,
|
||||||
|
"narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixpkgs-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs",
|
||||||
|
"rust-overlay": "rust-overlay"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-overlay": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils_2",
|
||||||
|
"nixpkgs": "nixpkgs_2"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1670552927,
|
||||||
|
"narHash": "sha256-lCE51eAGrAFS4k9W5aDGFpVtOAwQQ/rFMN80PCDh0vo=",
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"rev": "a0fdafd18c9cf599fde17fbaf07dbb20fa57eecb",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
{
|
||||||
|
description = "D4 AOC with Lua!";
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
rust-overlay.url = "github:oxalica/rust-overlay";
|
||||||
|
};
|
||||||
|
outputs = { nixpkgs, flake-utils, rust-overlay, ... } @ inputs:
|
||||||
|
flake-utils.lib.eachSystem flake-utils.lib.defaultSystems (sys:
|
||||||
|
let
|
||||||
|
overlays = [ rust-overlay.overlays.default ];
|
||||||
|
pkgs = import nixpkgs { system = sys; overlays = overlays; };
|
||||||
|
shellHookAfter = ''
|
||||||
|
echo "The input files should be placed under ./data/{submission,example}.txt"
|
||||||
|
echo "This problem shares one input between two parts"
|
||||||
|
'';
|
||||||
|
py_pkgs = [ pkgs.python310 ];
|
||||||
|
rs_pkgs = [
|
||||||
|
pkgs.openssl
|
||||||
|
pkgs.pkg-config
|
||||||
|
|
||||||
|
# Add rust-src, which rust-analyzer seems to rely upon
|
||||||
|
(pkgs.rust-bin.selectLatestNightlyWith
|
||||||
|
(
|
||||||
|
toolchain:
|
||||||
|
toolchain.default.override {
|
||||||
|
extensions = [ "rust-src" ];
|
||||||
|
}
|
||||||
|
))
|
||||||
|
# rust devex
|
||||||
|
pkgs.bacon # kinda like nodemon
|
||||||
|
pkgs.rust-analyzer
|
||||||
|
];
|
||||||
|
in
|
||||||
|
{
|
||||||
|
# Jack of all trades
|
||||||
|
devShell = pkgs.mkShell
|
||||||
|
{
|
||||||
|
buildInputs = py_pkgs ++ rs_pkgs;
|
||||||
|
shellHook = ''
|
||||||
|
echo "> Default runtime. This contains both rust and python3 env"
|
||||||
|
echo "Run ./run-py.sh for Python's output and ./run-rs.sh for Rust's output"
|
||||||
|
'' + shellHookAfter;
|
||||||
|
};
|
||||||
|
devShells = {
|
||||||
|
# nix develop ./#lua
|
||||||
|
# lua = pkgs.mkShell {
|
||||||
|
# nativeBuildInputs = lua_pkgs;
|
||||||
|
# shellHook = ''
|
||||||
|
# echo "> Lua runtime"
|
||||||
|
# echo "Run ./run-lua.sh to see the output of the solution"
|
||||||
|
# '' + shellHookAfter;
|
||||||
|
# };
|
||||||
|
|
||||||
|
# nix develop ./#fennel
|
||||||
|
|
||||||
|
# nix develop ./#python
|
||||||
|
rust = pkgs.mkShell {
|
||||||
|
nativeBuildInputs = rs_pkgs;
|
||||||
|
shellHook = ''
|
||||||
|
echo "> Rust runtime"
|
||||||
|
echo "Run ./run-rs.sh to see output of the solution"
|
||||||
|
echo "For quick feedback loop: bacon check-all"
|
||||||
|
'' + shellHookAfter;
|
||||||
|
};
|
||||||
|
python = pkgs.mkShell {
|
||||||
|
nativeBuildInputs = py_pkgs;
|
||||||
|
shellHook = ''
|
||||||
|
echo "> Python3 runtime"
|
||||||
|
echo "Run ./run-py.sh to see the output of the solution"
|
||||||
|
'' + shellHookAfter;
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env sh
|
||||||
|
echo "example"
|
||||||
|
python3 ./src/d9.py ./data/example.txt
|
||||||
|
|
||||||
|
echo "submission"
|
||||||
|
python3 ./src/d9.py ./data/submission.txt
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
|
def add_nd(lhs: Iterable[int], rhs: Iterable[int]):
|
||||||
|
# Add two generic n-d vector together
|
||||||
|
return tuple(l + r for l, r in zip(lhs, rhs))
|
||||||
|
def negate_nd(vec: Iterable[int]):
|
||||||
|
# Negates a generic n-d vector
|
||||||
|
return tuple(-e for e in vec)
|
||||||
|
def norm_infk_nd(vec: Iterable[int]):
|
||||||
|
# Mathematical concept where I get the length of the longest vector component
|
||||||
|
return max(abs(e) for e in vec)
|
||||||
|
def sign(e: int):
|
||||||
|
return 0 if e == 0 else (1 if e > 0 else -1)
|
||||||
|
|
||||||
|
def dir_to_offset(dir: str):
|
||||||
|
match dir:
|
||||||
|
case "U":
|
||||||
|
return (0, 1)
|
||||||
|
case "D":
|
||||||
|
return (0, -1)
|
||||||
|
case "L":
|
||||||
|
return (-1, 0)
|
||||||
|
case "R":
|
||||||
|
return (1, 0)
|
||||||
|
case d:
|
||||||
|
raise RuntimeError(f"Unknown direction {d}")
|
||||||
|
|
||||||
|
def move_knot(knot: list[tuple[int, int]], offset: Iterable[int]):
|
||||||
|
knot.append(add_nd(knot[-1], offset))
|
||||||
|
return knot
|
||||||
|
|
||||||
|
def part1(commands: list[tuple[str, int]]):
|
||||||
|
tail_locs: list[tuple[int, int]] = [(0,0)] # keeps track of where tail moved
|
||||||
|
# NOTE: I don't need to keep track of head's history here. This is only
|
||||||
|
# for ease of debugging
|
||||||
|
head_locs : list[tuple[int, int]] = [(0,0)] # keeps track of where head moved
|
||||||
|
for dir, rep in commands:
|
||||||
|
for _ in range(rep):
|
||||||
|
move_knot(head_locs, dir_to_offset(dir))
|
||||||
|
offset = add_nd(head_locs[-1], negate_nd(tail_locs[-1]))
|
||||||
|
# print(f"{offset=}")
|
||||||
|
if norm_infk_nd(offset) <= 1:
|
||||||
|
continue # no need to catch up
|
||||||
|
move_knot(tail_locs, (sign(e) for e in offset))
|
||||||
|
# print(f"{tail_locs=}")
|
||||||
|
# print(f"{head_locs=}")
|
||||||
|
return len(set(tail_locs))
|
||||||
|
|
||||||
|
def part2(commands: list[tuple[str, int]]):
|
||||||
|
KNOTS = 10
|
||||||
|
# NOTE: I don't need to keep track of any history but tail's. This is only
|
||||||
|
# for ease of debugging
|
||||||
|
locs = [[(0,0)] for _ in range(KNOTS)] # 10 knots :)
|
||||||
|
for dir, rep in commands:
|
||||||
|
for _ in range(rep):
|
||||||
|
move_knot(locs[0], dir_to_offset(dir)) # move head
|
||||||
|
for i in range(1, KNOTS):
|
||||||
|
offset = add_nd(locs[i-1][-1], negate_nd(locs[i][-1]))
|
||||||
|
if norm_infk_nd(offset) <= 1:
|
||||||
|
continue # no need to catch up
|
||||||
|
move_knot(locs[i], (sign(e) for e in offset))
|
||||||
|
return len(set(locs[-1]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main(lines: Iterable[str]):
|
||||||
|
splited = (line.strip().split() for line in lines)
|
||||||
|
# -> [" D 4 ",...] -> [["D", "4"], ...]
|
||||||
|
## Filters out any line that has empty newline and turn repitions into int
|
||||||
|
commands = [(comp[0], int(comp[1])) for comp in splited if len(comp) > 0]
|
||||||
|
# -> [[], ["D", "4"], ...] -> [["D", 4]]
|
||||||
|
print("part1", part1(commands))
|
||||||
|
print("part2", part2(commands))
|
||||||
|
pass
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
with open(sys.argv[1], "r") as f:
|
||||||
|
main(f)
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
fn main() {
|
||||||
|
// let test = include_str!("../data/example.txt");
|
||||||
|
let sub = inlcude_str!("../data/submission.txt");
|
||||||
|
let commands = sub.lines()
|
||||||
|
.map(|line| line.split(" "))
|
||||||
|
}
|
Loading…
Reference in New Issue