wip before nuking nixos@Felia

master
pegasust 2022-12-21 00:07:33 +00:00
parent 4c4bb6744b
commit 220f2f6b68
32 changed files with 947 additions and 2195 deletions

View File

@ -1,14 +1,146 @@
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
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop

View File

@ -1,34 +1,143 @@
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
noop
noop
addx 5
noop
noop
addx 6
addx 4
addx -4
addx 4
addx -6
addx 11
addx -1
addx 2
addx 4
addx 3
noop
addx 2
addx -30
addx 2
addx 33
noop
addx -37
noop
noop
noop
addx 3
addx 2
addx 5
addx 20
addx 7
addx -24
addx 2
noop
addx 7
addx -2
addx -6
addx 13
addx 3
addx -2
addx 2
noop
addx -5
addx 10
addx 5
addx -39
addx 1
addx 5
noop
addx 3
noop
addx -5
addx 10
addx -2
addx 2
noop
noop
addx 7
noop
noop
noop
noop
addx 3
noop
addx 3
addx 2
addx 8
addx -1
addx -20
addx 21
addx -38
addx 5
addx 2
noop
noop
noop
addx 8
noop
noop
addx -2
addx 2
addx -7
addx 14
addx 5
noop
noop
noop
addx -16
addx 17
addx 2
addx -12
addx 19
noop
noop
addx -37
noop
noop
noop
addx 3
addx 2
addx 2
addx 5
addx 20
addx -19
addx 2
noop
noop
noop
addx 5
addx 19
addx -12
addx 3
addx -2
addx 2
addx -18
addx 25
addx -14
addx -22
addx 1
noop
noop
noop
addx 3
addx 5
addx -4
addx 7
addx 4
noop
addx 1
noop
noop
addx 2
addx -6
addx 15
addx -1
addx 4
noop
noop
addx 1
addx 4
addx -33
noop
addx 21
noop

View File

@ -1,138 +1,45 @@
#!/usr/bin/env python3
import sys
from typing import Iterable, Optional
from dataclasses import dataclass
from typing import Iterable
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
def part1(insns: list[list[str]], init = 1):
reg = init
regs = [reg]
for insn in insns:
match insn:
case ["noop"]:
regs.append(regs[-1])
case ["addx", adder]:
reg += int(adder)
regs.append(regs[-1])
regs.append(reg)
case e:
raise RuntimeError(f"Unexpected {e}")
raise RuntimeError(f"lol what case is this? {e=}")
print("regs", os.pathsep.join(str(reg) for reg in regs))
return list(" ".join(sensor_display(e) if e else '.' for e in row) for row in graph)
def ret(queries: Iterable[int]):
signal_bases = [(i, regs[i-1]) for i in queries]
print(signal_bases)
return sum(signal[0] * signal[1] for signal in signal_bases)
return (regs, ret)
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 part2(insns: list[list[str]], init = 1, width = 40):
regs, _ = part1(insns, init)
position = lambda cycle: (cycle) % width
active = [['#' if (position(cycle) in range(regs[cycle]-1, regs[cycle]+2)) else '.'
for cycle in range(row, min(row + width, len(regs)))]
for row in range(0, len(regs),width)]
return active
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))
instructions = [s.split() for s in (line.strip() for line in lines) if len(s) > 0]
# print("part1", part1(instructions)([1,2,3,4,5]))
print("part1", part1(instructions)[1]([20, 60, 100, 140, 180, 220]))
part2_ans = part2(instructions)
print("part2")
for line in part2_ans:
print(str().join(line))
pass
@ -147,5 +54,12 @@ if __name__=="__main__":
REPL = """
_main("./data/example.txt")
_main("./data/submission.txt")
main([
"noop",
"addx 3",
"addx -5"
])
[1, 1, 1, 4, 4, -1, -1, -6, -6]
"""

View File

@ -1,8 +1,3 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9

File diff suppressed because it is too large Load Diff

4
2022/d15/.envrc Normal file
View File

@ -0,0 +1,4 @@
if command -v nix-shell &> /dev/null
then
use flake
fi

2
2022/d15/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.direnv
target

7
2022/d15/Cargo.lock generated Normal file
View File

@ -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"

8
2022/d15/Cargo.toml Normal file
View File

@ -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]

1
2022/d15/README.md Normal file
View File

@ -0,0 +1 @@
# Bootstrapping clojure project on

7
2022/d15/aoc/Cargo.lock generated Normal file
View File

@ -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"

8
2022/d15/aoc/Cargo.toml Normal file
View File

@ -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]

3
2022/d15/aoc/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

14
2022/d15/data/example.txt Normal file
View File

@ -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

View File

@ -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

94
2022/d15/flake.lock Normal file
View File

@ -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
}

77
2022/d15/flake.nix Normal file
View File

@ -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;
};
};
}
);
}

7
2022/d15/run-py.sh Executable file
View File

@ -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

151
2022/d15/src/main.py Normal file
View File

@ -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")
"""

6
2022/d15/src/main.rs Normal file
View File

@ -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(" "))
}

1
2022/d6/README.md Normal file
View File

@ -0,0 +1 @@
# Bootstrapping clojure project on

1
2022/d6/data/example.txt Normal file
View File

@ -0,0 +1 @@
mjqjpqmgbljsphdztnvjfqwrcgsmlb

View File

@ -0,0 +1,3 @@
hrbbjllllspssblslvvrdrbbpbbmcccfppvbbwvbbmrmjrjrfrgfgbffgfqfqlltlwttscsncscchssrppffvwwvvpnnwwwpvwvhhnvhhbttvzzdlzdlzzwmmjhhznnjdnnnqddbtdbdbsdsmdsdrrdpdwpdppgcgqgcctftsfszslljbljbjwbwbnwnqqrnnztntmtrmmzwzdwzwgwwwjhjsjgjtjjhpjhhppqzqdqffrvrtvvsmmgwmgwgbbclltctptzpzhpzptzppcfpcfftflfzftztddzgzmmfsmsrmmsstttvbvmbvvsmsqmqlldjdtthwtwbwggrzzjrzrcctffsshqshhpthhlnhlhqhdqqrwrmmcttpfttzfzgzdgdzzwrrtsrrsnrnccrbbsssbpbjjvzzwlwtwjwsjwjggzqgzzrsszzjnzjzwjzzcrzczncnqqztzfzhfhvvtjvjdvjjmrjrppvzppczpczcggshghvhnhhsrsnsdszzdpzpzlpzzhwwmnwmwmcwwfnwfwjjcbcncllcsllqdqzzhqhmqmbbjvjjwwcjjpnpllzfzddtmtccqrcrtrwrpphpmpplslmltthnnvhhvrvbblhhrrdqqmbqqgtqggdgcdcvvsbsswvvpggbbtftlflglzlmlbbfhhrshswhshffhhdnnrfrvrmrnrprrmfmpmnpnfnggvvcncdcrczrzccpmmssrbbdjdtdrdwrrwhrrvrtvvszvvwvzzmhhjhhwlhlqlvlttzftztdtstftrfrdddmtmzzsqzqvvpdpdcpcncnrrtntznzrzgznztnznhhsqqnrqrhhlzhzthhfddrzdrrqmqggcmmllnjjvwwjccfjfqfcfzccwvcwvcvjcjtjtnnqsqmqrmrzrszrszzfwfggnmmcdmdjmmhwwgfwfnwnlwwcffsrffvnvbvnvwnwgnwnmmbzbmbpplmplpspzpmzpzdzgzrzrtrjrbbppwvwgwmggqwgghqqshhcwcqwcqcfcbbnsnrrtztzrtrvtvhthzhmmrqqrwqqsjqjcctgtwthhqmmnffmgmdgdlgljjhwjwggrqqfrqqjvqvhqqgsqsgsttmrrbprbppmjppslpsllvlfvlvrvhvtqmrjcdzwsbzfmgmwmwqwhztqrsdzhqjqvbjbntnbndflthljcczdmmhszfgsplrtlqnfzbrlqngwdqtfwcmrdjrsmdpmjmqwrbwfjzwnvqhfmlqtvvnlfzbfccwslqpbzzjccbvrzhghqwtvqgwrmsfzqnmnqqjsjtpcmngpqgllfsnpqtjjbqcdppnsmtwrslnrbqtwvnbctzvwfmgctscmzjbqqgqdwbpzmrdwgfcjzftzgmfcjhchbnmnqnrgtqngwrmncjvptqqdtjtgtpzzdrfsdgmwlwrjnqldbwrqjrhwcczlzvlhpgrnwzhbwjnpthggczfgtrjnzvnlfdfbwcnzfbwlwlmgnnjnpvhbhqgnzhqsnmvbcftsmrcgpvnnnmgnrvpbzlpwnbwpzmwpgqvbfgjwfrjqnvvgmqwwcfddqmdznmfhpjcfgptqdqwmplrglbwlmsqzjshrlhflcjvptgrcfhjfgqmlfzrtphpbvcqzwpcnwljjdlmqzhcctqshdngrgtlfsrfccdtlvmqcdgnpcvphdsrpzfzwclvsqcpzqlfvvqzggdhpfzdvhshglvfzfmcllrdfjfsjtngjgddcpqnlmrnplwtlvwdvzftltnsnspcdztgqhlhvvbnwvnmhscfnqbngpvprzfrjcmfpfzfftrlnwgllhnjndpjdrwcgqpcgcqngnbfzlvzvhnqdjthflmwvppmbdssddmgsbgrqnpjzrjpzdddqgsdlmwnhhpjbthclvqhgrsnrbqgtnsjhncnzbhrdgftvbptrqssvsqfpqnddhmgwcrfqndqjsqgffmhdvqhjrdlmrlcqctqccprwlbqgqrwmtfhwmfjfqzdqbsdsjbtsvfvgbsrvqwnqqqqthpsqgcfslsqtnjwtsrcdcctggdghrjwpbfccrtwgszwbrsjswmjmjbcqrsgbcfsdjzsbjnnssnddnnvwgftlrqvphnqcgjszscrlhhjnljlqcjqtqfwbmdmrgdlcqqwmbsmsdhpplvlfglqwspbfptlbzqjwhqmfvzvsvpjclcdzsbvntmhdqdvhghcmmflpjbglsghbswdshtsbdrgpsrsclrmfwwqbrgdjsqztgttqpwhnfhszlgbfpzhczsnwqflmshlgbrpmdzgpqwtsbssgfjbtrwbmztlwwfmsdgpgfgdjfdccwlfgztbcbqjvjtvslmddjplrswwcszspgplsrhrnwnmrrfbcgdmntcrlvnfqtwwcczsglrhtrfqnmhvgzjpmlplqvqhmnfgvzqcmzhqszgslvndqtqhvrbvbmclbcbjdswvcjrzgfdmdwnnlzlzqcffsrqdfmmpzfnmdsnqlpcrhzsdnsflblcjsfsgcnsspftjrlmdjsmfpqtmlgfvnlfnjscsgwzwvpjrvvclhsbqldlnmtglhbjfwlzmvrbvgtprfjbjhhnlqnbrswwlqtcgrjrltdrnfrjhrntllptlsbhqrwvdsfrlghtfcndznzjwcgmtdvffltgrdmljlqhdtmdvnfsfsrvdpmhlrrsttvqlwfptddwbpfrbclwwzmfpttmrmmqzjnbbnnfvzwmmcfshvrlbdbjzprftbqvdsghnnzwbjccpthdsvsdlgvphsgjdqjwsgmzqnqpqvgqjvwgjtzpmqqwnlwrwhqqjjclcbhjgpwhqdclwmqfmwbwmwwvcbhfznfhcfbprfcdqlbcttnvgnjwswcmpbrghtzgdbppbprffzjgvddzpwmdctrhnrfzdfhtmnfrsfdqvzcnrtncflhvldcndwqtvbggmwlzhchlcwtcbqcvlfhdwljgddwpvcfczvfqmphgtdsnsqwdpvvmwnwqjbrjwbdhhgtffphsdrvspsbgmfrmwmhnrgqdfppzgfpgmqjcsnglczgwhjthfhztzrlpgzjhcfrjpjvtjptptbvflftjtcfhmbwlhlbhvnjnbfmwjrgbvvhmdlncdgncgfjcnnpdljfcjsmsfscqpwsgcmlhhqmldsnjfrrqpghwncmgwgnjsdtvbhrbbnmpqjrrctqqnqzztmbqmdsgdvmmlwmbvprllzgntnmttrlzrttmjjlrwpwmtfznmwnsjmjhjdnsppfhcrjpzhjqzdtdbsjshfzzvrwvjbjbgtsfpgggbdztczwlhpmthfjdgsbrvlwmlrvgdrpjzccwmgpcnqqzmqdjqmwsrzwsmtmdjdhmjrwfwnzlmfnqtcgtslwtlnwhvmqntmglhntnsjlnmzfvfdztcfwmpchsrsdmqvqcwljzrmmssjvbmvvnmqlbsdwnrbmqctdtmfzlgfzpmjcnftgftvjpfbwwmzfdrrwjwcfwfcfmzbbnppgjrmbcvmvnjpdrzmvndvddtvshlnjjwgtsvnwtwnhcbfpnthpjlrhgrqccdgppjvdqjwqrfrrgnvhfwvjhnwhntnpmghphrtgqhwtbrqhqljfdjbgnlgmqqgfcqpqfhcpgspdbvlbfjvlrgmtjztwdzlrhqwwtcpdvsqgssjbjjgqlwbcctzzqvvmdzpfrmspmqhtzwgcfsslpnhpjfwqrrfbwbndrvhnnsjnlvlvqdsgwzjsrprhgtvsfbhbcpljdczbtdwzcnhzntrwcrjctmhtjfdlthznzmqblppzcqgpjhlzjrmcvpptfjjzltdhmvwphwlccscwrwfcqpqwwrzcmnltzdcfvtjrcvsqwtchrmdfzjmzjfhppjzbhglwqggzqqnspfmzrfwrqdqdrsdbsdhcgdqrrnjlwrqhfhpzjhrvjndqphndnnnbwhrjvqrrbvlhhbljjcwmfpvnhcszfshlsnczgtcfhjslbhzczdqdmdnvqdzhbmbpcnbntwgllfscrcwhfrgtfvftmwhbgfhjzjrbvvwc

View File

@ -3,8 +3,12 @@
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
clj-nix = {
url = "github:jlesquembre/clj-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { nixpkgs, flake-utils, ... } @ inputs:
outputs = { nixpkgs, flake-utils, clj-nix, ... } @ inputs:
flake-utils.lib.eachSystem flake-utils.lib.defaultSystems (sys:
let
overlays = [ ];
@ -13,19 +17,21 @@
echo "The input files should be placed under ./data/{submission,example}.txt"
echo "This problem shares one input between two parts"
'';
clj_pkgs = import clj-nix { system = sys; };
py_pkgs = [ pkgs.python310 ];
# lua_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.busted luapkgs.luafilesystem ])) ];
fennel_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.fennel luapkgs.readline ])) ];
clojure_pkgs = [ ];
in
{
# Jack of all trades
devShell = pkgs.mkShell {
nativeBuildInputs = py_pkgs ++ fennel_pkgs;
shellHook = ''
echo "> Default runtime. This contains both fennel and python3 env"
echo "Run ./run-py.sh for Python's output and ./run-fnl.sh for Fennel's output"
'' + shellHookAfter;
};
devShell = pkgs.mkShell
{
nativeBuildInputs = py_pkgs ++ fennel_pkgs;
shellHook = ''
echo "> Default runtime. This contains both fennel and python3 env"
echo "Run ./run-py.sh for Python's output and ./run-fnl.sh for Fennel's output"
'' + shellHookAfter;
};
devShells = {
# nix develop ./#lua
# lua = pkgs.mkShell {

2
2022/d7/_build/log Normal file
View File

@ -0,0 +1,2 @@
# dune init project aoc_37_ocaml
# OCAMLPARAM: unset

View File

View File

@ -0,0 +1,4 @@
(executable
(public_name aoc_37_ocaml)
(name main)
(libraries aoc_37_ocaml))

View File

@ -0,0 +1 @@
let () = print_endline "Hello, World!"

View File

@ -0,0 +1,2 @@
(library
(name aoc_37_ocaml))

View File

@ -0,0 +1,2 @@
(test
(name aoc_37_ocaml))

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3
from typing import Iterable
def main(lines: Iterable[str]):
ROOT_WD = [""]
ROOT="/".join(ROOT_WD)
@ -39,5 +38,3 @@ if __name__ == "__main__":
import sys
with open(sys.argv[1], "r") as f:
main(f)
"/".join([""])