Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 10 #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ jobs:
# nix-instantiate --eval 09/9.nix > /tmp/output9
# diff /tmp/output9 09/output

# - run:
# name: Day10
# command: |
# nix-instantiate --eval 10/10.nix > /tmp/output10
# diff /tmp/output10 10/output
- run:
name: Day10
command: |
nix-instantiate --eval 10/10.nix > /tmp/output10
diff /tmp/output10 10/output


workflows:
Expand Down
108 changes: 108 additions & 0 deletions 10/10.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/nix-instantiate --eval

let lib = import <nixpkgs/lib>;
inherit (builtins) readFile length split elem getAttr attrNames filter trace;
inherit (lib.lists) head last remove findFirst reverseList tail concatLists unique elemAt sort;
inherit (lib.strings) splitString stringToCharacters;
inherit (lib.attrsets) mapAttrs' genAttrs filterAttrs;

inherit (import ../string-extra.nix) trim splitAndMap;
inherit (import ../math-extra.nix) maximum maximumBy manhattanDistance atan2 pseudoangle rad2def;
inherit (import ../lazy-extra.nix) strict;
inherit (import ../lists-extra.nix) enumerate uniqueBy removeMany rotateUntil;
inherit (import ../advent-utils.nix) splitStringAndMapFromTrimmedFile;
vector = import ../vector2.nix;

input = splitStringAndMapFromTrimmedFile ./input "\n" stringToCharacters;
coordinateGrid = (
let enumeratedLines = enumerate input;
coordinateGrid =
map (elines:
map (ecolumns:
genAttrs ["x" "y" "obj"] (name:
if name == "x" then
ecolumns.fst
else if name == "y" then
elines.fst
else
ecolumns.snd
)
) (enumerate elines.snd)
) enumeratedLines;
in concatLists coordinateGrid
);

asteroidCoordinates =
map (filterAttrs (n: v: n != "obj"))
(filter (grid: grid.obj == "#") coordinateGrid);

preprocessAsteroids = (originAsteroid: targetAsteroid:
let unit = vector.unit (vector.fromPoints originAsteroid targetAsteroid);
in {x1 = toString unit.x1; x2 = toString unit.x2;}
);

countVisibleAsteroid =
allAsteroids:
initialAsteroid: (
let
asteroids = remove initialAsteroid allAsteroids;
preprocessedAsteroids = map (preprocessAsteroids initialAsteroid) asteroids;
nVisibleAsteroids = length (unique preprocessedAsteroids);
in nVisibleAsteroids
);


idealPosition = maximumBy (countVisibleAsteroid asteroidCoordinates) asteroidCoordinates;
part1 = countVisibleAsteroid asteroidCoordinates idealPosition;

listVisibleAsteroid =
allAsteroids:
initialAsteroid: (
let
asteroids = remove initialAsteroid allAsteroids;
visibleAsteroids = uniqueBy (preprocessAsteroids initialAsteroid) (sort (a: b: manhattanDistance initialAsteroid a < manhattanDistance initialAsteroid b) asteroids);
in visibleAsteroids
);

laserFullRotation = (originAsteroid: allAsteroids:
removeMany (listVisibleAsteroid allAsteroids originAsteroid) allAsteroids
);

laserOrderedRotation = (originAsteroid: allAsteroids:
let rotateF = rotateUntil (a: a.x >= originAsteroid.x && a.y < originAsteroid.y);
sortF = sort (fst: snd:
let v1 = vector.unit (vector.fromPoints originAsteroid fst);
v2 = vector.unit (vector.fromPoints originAsteroid snd);
in atan2 v1.x1 v1.x2 > atan2 v2.x1 v2.x2
);
visibleAsteroids = listVisibleAsteroid allAsteroids originAsteroid;
in
rotateF (sortF visibleAsteroids)
);

nthAsteroidVaporized = (originAsteroid: allAsteroids: n:
let
nInitialAsteroids = length allAsteroids;
remainingAsteroids = laserFullRotation originAsteroid allAsteroids;
nRemainingAsteroids = length remainingAsteroids;
nVaporizedAsteroids = trace (nInitialAsteroids - nRemainingAsteroids) (nInitialAsteroids - nRemainingAsteroids);
in if nVaporizedAsteroids == 0 then
abort "Never got to n."
else if nVaporizedAsteroids >= n then
# laserOrderedRotation originAsteroid allAsteroids
elemAt (laserOrderedRotation originAsteroid allAsteroids) n
# else if nVaporizedAsteroids != countVisibleAsteroid allAsteroids originAsteroid then
# abort "Wrong"
else
nthAsteroidVaporized originAsteroid remainingAsteroids (n - nVaporizedAsteroids)
);

# pos = {x = 4; y = 4;};
# bet = nthAsteroidVaporized pos asteroidCoordinates 9;
bet = nthAsteroidVaporized idealPosition asteroidCoordinates 200;
part2 = bet.x * 100 + bet.y;

# abc = listVisibleAsteroid asteroidCoordinates pos;

# in strict { inherit abc; }
in strict { inherit part1 part2; }
19 changes: 19 additions & 0 deletions 10/aaaa
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
MyAsteroid:
3,4

AllAsteroids
(Coordinates, AxisDistances, ManhattanDistance, Slope, Quadrant):
4,4 (+1, +0) 1 null
4,3 (+1, -1) 2 -1
3,2 (+0, -2) 2 +0
4,2 (+1, -2) 3 -2
1,2 (-2, -2) 4 +1
4,0 (+1, +4) 5 +4
0,2 (-3, -2) 5 +2/3 3
2,2 (-1, -2) 3 +2 3
5,6 (+1, +2) 3 +2 1 # Do Not Ignore! Reasons: Multiple of (-1, -2)
Multipicity factor < 0 (== -1)
1,0 (-2, -4) 6 +2 # Ignore! Reasons: Multiple of (-1, -2)
Multipicity factor > 0 (== 2)

X can see Y if:
41 changes: 41 additions & 0 deletions 10/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.#....#.###.........#..##.###.#.....##...
...........##.......#.#...#...#..#....#..
...#....##..##.......#..........###..#...
....#....####......#..#.#........#.......
...............##..#....#...##..#...#..#.
..#....#....#..#.....#.#......#..#...#...
.....#.#....#.#...##.........#...#.......
#...##.#.#...#.......#....#........#.....
....##........#....#..........#.......#..
..##..........##.....#....#.........#....
...#..##......#..#.#.#...#...............
..#.##.........#...#.#.....#........#....
#.#.#.#......#.#...##...#.........##....#
.#....#..#.....#.#......##.##...#.......#
..#..##.....#..#.........#...##.....#..#.
##.#...#.#.#.#.#.#.........#..#...#.##...
.#.....#......##..#.#..#....#....#####...
........#...##...#.....#.......#....#.#.#
#......#..#..#.#.#....##..#......###.....
............#..#.#.#....#.....##..#......
...#.#.....#..#.......#..#.#............#
.#.#.....#..##.....#..#..............#...
.#.#....##.....#......##..#...#......#...
.......#..........#.###....#.#...##.#....
.....##.#..#.....#.#.#......#...##..#.#..
.#....#...#.#.#.......##.#.........#.#...
##.........#............#.#......#....#..
.#......#.............#.#......#.........
.......#...##........#...##......#....#..
#..#.....#.#...##.#.#......##...#.#..#...
#....##...#.#........#..........##.......
..#.#.....#.....###.#..#.........#......#
......##.#...#.#..#..#.##..............#.
.......##.#..#.#.............#..#.#......
...#....##.##..#..#..#.....#...##.#......
#....#..#.#....#...###...#.#.......#.....
.#..#...#......##.#..#..#........#....#..
..#.##.#...#......###.....#.#........##..
#.##.###.........#...##.....#..#....#.#..
..........#...#..##..#..##....#.........#
..#..#....###..........##..#...#...#..#..
5 changes: 5 additions & 0 deletions 10/input2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.#....#####...#..
##...##.#####..##
##...#...#.#####.
..#.....X...###..
..#.#.....#....##
20 changes: 20 additions & 0 deletions 10/input3
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##
1 change: 1 addition & 0 deletions 10/output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ part1 = 340; part2 = NaN; }
5 changes: 5 additions & 0 deletions bool-extra.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/nix-instantiate --eval

let not = bool: if bool then false else true;

in { inherit not; }
40 changes: 38 additions & 2 deletions lists-extra.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/nix-instantiate --eval

let lib = import <nixpkgs/lib>;
inherit (lib.lists) head tail take drop range length foldl elemAt count zipLists zipListsWith concatMap foldr;
inherit (builtins) elem;
inherit (lib.lists) head tail take drop range length foldl elemAt count zipLists zipListsWith concatMap foldr filter;
inherit (import ./func-extra.nix) minBy maxBy;
inherit (import ./bool-extra.nix) not;

replace = (xs: i: v: (take i xs) ++ [v] ++ (drop (i + 1) xs));

Expand Down Expand Up @@ -137,4 +139,38 @@ let lib = import <nixpkgs/lib>;
in foldr (xs: concatMap (insertEverywhere xs)) [[]]
);

in { inherit allBy2 anyBy2 cartesianProduct chunks concat countEqual dropWhile enumerate groupIntoList groupIntoListBy maximumBy minimumBy permutations replace scanl takeWhile upTo zipManyListsWith; }
removeMany = deny: xs: (
filter (x: not (elem x deny)) xs
);

uniqueBy = f: xs: (
if xs == [] then [] else

let x = head xs;
in [x] ++ uniqueBy f (filter (y: f y != f x) (tail xs))
);

rotate = n: xs: (
if length xs < 2 then xs else
if n == 0 then xs else

let h = head xs;
t = tail xs;
in rotate (n - 1) (t ++ [h])
);

rotateUntil = p: xs: (
if length xs < 2 then xs else

let succeeded = p (head xs);
in if succeeded then xs else rotateUntil p (rotate 1 xs)
);

rotateUntil2 = p: xs: (
if length xs < 2 then xs else

let succeeded = p (head xs) (head tail xs);
in if succeeded then xs else rotateUntil2 p (rotate 1 xs)
);

in { inherit allBy2 anyBy2 cartesianProduct chunks concat countEqual dropWhile enumerate groupIntoList groupIntoListBy maximumBy minimumBy permutations replace removeMany scanl takeWhile upTo uniqueBy zipManyListsWith rotate rotateUntil rotateUntil2; }
Loading