2024 day 6 part 1

This commit is contained in:
tristan 2024-12-06 07:13:59 +00:00
parent 1f2171f3a5
commit 66b7646869
5 changed files with 294 additions and 0 deletions

10
2024/06/example.txt Normal file
View file

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

130
2024/06/input.txt Normal file
View file

@ -0,0 +1,130 @@
..........#.............................#...............#...........#....................#...........#............................
..............................#.......#...#.....#.....#...............#.#..#.......................#............#.................
.............#...........#.#........#............#..........................#.........#.......#......#............................
......................................................#....#..#.............#................................#...#............##..
...............#.....#....................................#......................................................#................
..................##............#......#...............................................................#...................#......
.#......#...........................................................#............................#................................
#..#.............#...............................#.....#....#.............................#....#..............................#...
#...##..........#.........................#.................................................#............#...............#........
............#...............#..............................................#.................................#..........##........
..............#...................#......##...#.....#....................#.......................................................#
.#.....#....#......................#...#........................................#.#.............#.........#.......................
#..#...............#........##.........#.....#..........#.....##.........................#.....#.#................................
...............#...............................#.....#.................................................#..##......................
......#...................#..........#...........................................#....#...........................................
...............................................#...#....##.................................................#.....#................
.................#.........#..............................#........#...#...#...................................#..................
...........#..#.....#...#.........................#..........#.#.............#....#...................##..........................
.........................................#.........................#.#............#...#...##............................#.........
.....#......................................#....................................................#..............#.................
..................................................#..................#................#.............#.#..#..................#.....
............................................#.....#......................#........#..........................................#....
.......#..........................#...#...................................#.........................#...#..........#......#.......
............#...........##........................................................................#......................#........
#.#...............................................#........#..........#.#..#...#................................#.................
..#.........................#.#........................#....##..................................................#............#....
.....................................................................................#...............#...........#..........#.....
....................#..........#................................#...............................................#.................
#..............#.#...........#.........#..........#..........................#......................##........#.........#.........
....#....#...............................................................................#...#...........#............#...#...#...
.......................................................##......................#...#................................#.............
.......#...........##...........................#..............#..............................#...................#......#........
......#.........#..............................................................#........#..#..........#.......#..#...#.....#....#.
....#.............................................#........#....#..................................#..............................
..........#......#...........#....................................#..............#.......#...#............#.......................
...........................................................................................#.......#..............................
.#..................................................#.............................#..................#...................#........
......#..#.#................#.....................#..................................#.............#...............#..............
...............................#..................#........#......................................................................
......................#..................................................................................#.........#..............
...............................................................................................................................#..
.......#..............#...........................................................#......................#.......................#
#...............................#..............................#.......#..............................#...#...#........#..........
............................#....#...#.........#....#................................................................#............
.....................#.................................................................#..#....#..................#...............
......................#.............#.............................#...................................................#...........
....#.................#.#.........##................#.....#....................................................#.............#....
...............................#...#...................#.......................................#......#...........................
....#.............................................................................................................................
...#.......................................................................................................#............#.......#.
..#.............##.............................................................#...#............................#............#....
......................#.......#............#........................................................#..................#......#...
...................#.............#..................#..............................#............#........#...................#....
....................#...........#.........................................................#......#.......#...........#.....##.....
...........#.........................................#.............................#..............#..................#............
......................#.........#...................#......#...........................................#..........................
..#....#...#.............................#..............#.....................#............#...................................#..
.....#.............................#....#..................................#...................#...#.............#................
...................#...#...........#........................#.............#........................#...#.............#..........#.
...#...........#......#.......#..............#......#..........#.....#............................................................
..#......................................................#......#...........................#...........#......#..#...............
........#............................#.#....................#...#...#......#.....#...............................................#
.............#....#........#.........................................................................................#.........#..
....................................................................................................#.............................
..#........................#....#............................................................................#....................
.......................................#.....#.#.......................................#......#........#.#...................#....
.................#........#....................................#...#..#................................#..........................
...........#.................................................................................................#....................
......................#..................#...#.............#................##................#.......#...........#.........#.....
............#..#................#.............................................#............##..................##..........#......
......#.#.......................#............#...........................................................#........................
#......................................#.#......^...............................................#.................................
........#...........................................................#...............#.............................................
...#.............................................#.................#...............#.............................#...#.#..........
....................#........#...........##.......................#.................#..................#................#.........
#.................................................................................................................................
............#.......................................................................................#.#.........#..............#..
...............#....#............#..................................................#......##.................................#...
.............................#.........#...............#......#.......#.................#........................#........#.......
......................................................#........................................................#.................#
.................#.#...............#......................................................................................#....#..
........#.............#......................#...........#...........#......................................#..............#......
......................#......................#...........#.....#.....##............................................#....#.........
.............#................................................................................#..........#.....#..................
............##...........................#.................................#..#.#....................................#............
..............................................................................................#............................#......
...#.............................#..#..#....................................#..............#....#.................................
.....#..........................................................#.....#......#.........#..........................................
....##......#.....................#..........#..............#..................#.#..............#........#...........#....#.......
.....#...#.........#............#............#.............................................................#......................
.........#...................................#...........#.....................................##.....#............#..............
.....##.#............##.....................#..........................#.................................#........#...............
.......................#..............##...#...................................................#.........#........................
..#........#.......#...#..##............#....................................................................#...........#.....#..
..#.....................................................................#..............#.............#...................#........
...........#..........#..........#.....#.................................................................#..#.....................
..........#................#........................................................##.#...............................#.#......#.
#............................................#...........................##...#.......................#.............#.............
..#............#........................................................................................................#.........
....................................................#....................#............#...............#..........................#
........................................................................#.......#.....................................#.#.........
................#..#......................#...........................................#...........................................
........................##............................................#.............##..........................#.......#....#....
.........#.......#..............................#......#....#............#..................#...#.................................
#.........#............................#....#.#..#.........................................................##.....................
.#..##...............#....#.............................#...............#........#.......................................#......#.
...#............................................................#.....................#.................#.....#...................
...#...........#..#.............................#.....#.............................................................#.............
...#.........................#..............................................................................................##....
.........................#...........................#.......................#...........#........................................
.#..................................#.............................................................................................
....................................................................#...........##.....#.#.........#.....#........................
................#...............................#........#........................................................................
......#....#.................................#...................................#.#.........#.....#....................#.........
................#..............................#.......................#...#.....#.......#................#.......................
....................#......................................#..#...........#.#.................#...#.......................#.......
.............#..#...#............................#.#...........................................#.......................#..........
.........#..........#.................#....................................#.#..........................................#.........
....#.............#......................................................#...........#...........................#................
........#.#............................#...........................................#.................#..............#..........#..
..............#..........................#............#.........#....#...#.....#.........................#....#...............##..
...................#.........................................................#.......#........#..............#..........#.........
.#........#....#..................................#..#.#...............#............#...........#.................................
...............................................................................................#.....#....#.............#.........
...........#..........#...........................................#.........#..............#................#..........#..........
............#.................................###..............#.#...............................................................#
......#...................................................................................#............#...#..........#.....#.....
.............#..................#...................#......#.........#..#.......................#....#................#..........#
..#....##................................#.................#...........................#.......#....#...#..........#..............
...............................#......................................#.........#.............#......................#..........#.

62
2024/06/solution.nix Normal file
View file

@ -0,0 +1,62 @@
{lib, ...}: input: rec {
content = lib.trim input;
chart = content
|> lib.strings.splitString "\n"
|> map (lib.strings.splitString "")
|> map (line: lib.sublist 1 (builtins.length line - 2) line)
;
startingPos = let
xs = map (lib.lists.findFirstIndex (char: char == "^") null) chart;
y = lib.lists.findFirstIndex (x: !isNull x) null xs;
x = builtins.elemAt xs y;
in
{inherit x y;}
;
posToString = {x, y}: "${toString x},${toString y}";
addVec = a: b: {x = a.x + b.x; y = a.y + b.y;};
index = i: list: builtins.elemAt list i;
getChar = {x, y}: chart
|> index y
|> index x
;
height = builtins.length chart;
width = chart |> index 0 |> builtins.length;
progress = {visited ? {}, pos, dir, ...}: let
nextChar = getChar (addVec pos dir);
newDir = if nextChar == "#" then rotate dir else dir;
newPos = addVec pos newDir;
fwd = addVec pos dir;
in
{
visited = visited // {${posToString pos} = true;};
exited = fwd.y >= height || fwd.x >= width || fwd.y < 0 || fwd.x < 0;
pos = newPos;
dir = newDir;
};
rotate = {x, y}: {x = -y; y = x;};
startingDir = {x = 0; y = -1;};
getVisits = {pos ? startingPos, dir ? startingDir, visited ? {}, ...}:
let
nextStep = progress {inherit pos dir visited;};
in if nextStep.exited then visited else getVisits nextStep;
part1result = getVisits {}
|> builtins.attrNames
|> builtins.length
# the last position
|> (v: v + 1)
;
}

59
2024/flake.lock Normal file
View file

@ -0,0 +1,59 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1732837521,
"narHash": "sha256-jNRNr49UiuIwaarqijgdTR2qLPifxsVhlJrKzQ8XUIE=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "970e93b9f82e2a0f3675757eb0bfc73297cc6370",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1703013332,
"narHash": "sha256-+tFNwMvlXLbJZXiMHqYq77z/RfmpfpiI3yjL6o/Zo9M=",
"path": "/nix/store/50bgi74d890mpkp90w1jwc5g0dw4dccr-source",
"rev": "54aac082a4d9bb5bbc5c4e899603abfb76a3f6d6",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"tix": "tix"
}
},
"tix": {
"inputs": {
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1704309534,
"narHash": "sha256-U9qeMl0WBlpy335gL06WFGvPxLBTd+ToiiCHUHWrgR0=",
"ref": "refs/heads/master",
"rev": "5007770e88c4d5f67c3790761a1125904a9ef257",
"revCount": 20,
"type": "git",
"url": "https://git.tristans.cloud/tristan/tix"
},
"original": {
"type": "git",
"url": "https://git.tristans.cloud/tristan/tix"
}
}
},
"root": "root",
"version": 7
}

33
2024/flake.nix Normal file
View file

@ -0,0 +1,33 @@
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
tix.url = "git+https://git.tristans.cloud/tristan/tix";
};
outputs = { self, nixpkgs, tix }: let
pkgs = import nixpkgs {system = "x86_64-linux";};
in {
day06 = let
solution = import ./06/solution.nix pkgs;
example = (pkgs.lib.readFile ./06/example.txt);
input = (pkgs.lib.readFile ./06/input.txt);
in {
part1example = (solution example);
part1 = (solution input);
};
packages.x86_64-linux.watch = tix.watch {
cmd = ''
nix run .\#test --impure --extra-experimental-features pipe-operators
'';
};
packages.x86_64-linux.test = tix.run [
./solution.test.nix
];
};
}