diff --git a/2024/06/example.txt b/2024/06/example.txt new file mode 100644 index 0000000..a4eb402 --- /dev/null +++ b/2024/06/example.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... diff --git a/2024/06/input.txt b/2024/06/input.txt new file mode 100644 index 0000000..1f20b72 --- /dev/null +++ b/2024/06/input.txt @@ -0,0 +1,130 @@ +..........#.............................#...............#...........#....................#...........#............................ +..............................#.......#...#.....#.....#...............#.#..#.......................#............#................. +.............#...........#.#........#............#..........................#.........#.......#......#............................ +......................................................#....#..#.............#................................#...#............##.. +...............#.....#....................................#......................................................#................ +..................##............#......#...............................................................#...................#...... +.#......#...........................................................#............................#................................ +#..#.............#...............................#.....#....#.............................#....#..............................#... +#...##..........#.........................#.................................................#............#...............#........ +............#...............#..............................................#.................................#..........##........ +..............#...................#......##...#.....#....................#.......................................................# +.#.....#....#......................#...#........................................#.#.............#.........#....................... +#..#...............#........##.........#.....#..........#.....##.........................#.....#.#................................ +...............#...............................#.....#.................................................#..##...................... +......#...................#..........#...........................................#....#........................................... +...............................................#...#....##.................................................#.....#................ +.................#.........#..............................#........#...#...#...................................#.................. +...........#..#.....#...#.........................#..........#.#.............#....#...................##.......................... +.........................................#.........................#.#............#...#...##............................#......... +.....#......................................#....................................................#..............#................. +..................................................#..................#................#.............#.#..#..................#..... +............................................#.....#......................#........#..........................................#.... +.......#..........................#...#...................................#.........................#...#..........#......#....... +............#...........##........................................................................#......................#........ +#.#...............................................#........#..........#.#..#...#................................#................. +..#.........................#.#........................#....##..................................................#............#.... +.....................................................................................#...............#...........#..........#..... +....................#..........#................................#...............................................#................. +#..............#.#...........#.........#..........#..........................#......................##........#.........#......... +....#....#...............................................................................#...#...........#............#...#...#... +.......................................................##......................#...#................................#............. +.......#...........##...........................#..............#..............................#...................#......#........ +......#.........#..............................................................#........#..#..........#.......#..#...#.....#....#. +....#.............................................#........#....#..................................#.............................. +..........#......#...........#....................................#..............#.......#...#............#....................... +...........................................................................................#.......#.............................. +.#..................................................#.............................#..................#...................#........ +......#..#.#................#.....................#..................................#.............#...............#.............. +...............................#..................#........#...................................................................... +......................#..................................................................................#.........#.............. +...............................................................................................................................#.. +.......#..............#...........................................................#......................#.......................# +#...............................#..............................#.......#..............................#...#...#........#.......... +............................#....#...#.........#....#................................................................#............ +.....................#.................................................................#..#....#..................#............... +......................#.............#.............................#...................................................#........... +....#.................#.#.........##................#.....#....................................................#.............#.... +...............................#...#...................#.......................................#......#........................... +....#............................................................................................................................. +...#.......................................................................................................#............#.......#. +..#.............##.............................................................#...#............................#............#.... +......................#.......#............#........................................................#..................#......#... +...................#.............#..................#..............................#............#........#...................#.... +....................#...........#.........................................................#......#.......#...........#.....##..... +...........#.........................................#.............................#..............#..................#............ +......................#.........#...................#......#...........................................#.......................... +..#....#...#.............................#..............#.....................#............#...................................#.. +.....#.............................#....#..................................#...................#...#.............#................ +...................#...#...........#........................#.............#........................#...#.............#..........#. +...#...........#......#.......#..............#......#..........#.....#............................................................ +..#......................................................#......#...........................#...........#......#..#............... +........#............................#.#....................#...#...#......#.....#...............................................# +.............#....#........#.........................................................................................#.........#.. +....................................................................................................#............................. +..#........................#....#............................................................................#.................... +.......................................#.....#.#.......................................#......#........#.#...................#.... +.................#........#....................................#...#..#................................#.......................... +...........#.................................................................................................#.................... +......................#..................#...#.............#................##................#.......#...........#.........#..... +............#..#................#.............................................#............##..................##..........#...... +......#.#.......................#............#...........................................................#........................ +#......................................#.#......^...............................................#................................. +........#...........................................................#...............#............................................. +...#.............................................#.................#...............#.............................#...#.#.......... +....................#........#...........##.......................#.................#..................#................#......... +#................................................................................................................................. +............#.......................................................................................#.#.........#..............#.. +...............#....#............#..................................................#......##.................................#... +.............................#.........#...............#......#.......#.................#........................#........#....... +......................................................#........................................................#.................# +.................#.#...............#......................................................................................#....#.. +........#.............#......................#...........#...........#......................................#..............#...... +......................#......................#...........#.....#.....##............................................#....#......... +.............#................................................................................#..........#.....#.................. +............##...........................#.................................#..#.#....................................#............ +..............................................................................................#............................#...... +...#.............................#..#..#....................................#..............#....#................................. +.....#..........................................................#.....#......#.........#.......................................... +....##......#.....................#..........#..............#..................#.#..............#........#...........#....#....... +.....#...#.........#............#............#.............................................................#...................... +.........#...................................#...........#.....................................##.....#............#.............. +.....##.#............##.....................#..........................#.................................#........#............... +.......................#..............##...#...................................................#.........#........................ +..#........#.......#...#..##............#....................................................................#...........#.....#.. +..#.....................................................................#..............#.............#...................#........ +...........#..........#..........#.....#.................................................................#..#..................... +..........#................#........................................................##.#...............................#.#......#. +#............................................#...........................##...#.......................#.............#............. +..#............#........................................................................................................#......... +....................................................#....................#............#...............#..........................# +........................................................................#.......#.....................................#.#......... +................#..#......................#...........................................#........................................... +........................##............................................#.............##..........................#.......#....#.... +.........#.......#..............................#......#....#............#..................#...#................................. +#.........#............................#....#.#..#.........................................................##..................... +.#..##...............#....#.............................#...............#........#.......................................#......#. +...#............................................................#.....................#.................#.....#................... +...#...........#..#.............................#.....#.............................................................#............. +...#.........................#..............................................................................................##.... +.........................#...........................#.......................#...........#........................................ +.#..................................#............................................................................................. +....................................................................#...........##.....#.#.........#.....#........................ +................#...............................#........#........................................................................ +......#....#.................................#...................................#.#.........#.....#....................#......... +................#..............................#.......................#...#.....#.......#................#....................... +....................#......................................#..#...........#.#.................#...#.......................#....... +.............#..#...#............................#.#...........................................#.......................#.......... +.........#..........#.................#....................................#.#..........................................#......... +....#.............#......................................................#...........#...........................#................ +........#.#............................#...........................................#.................#..............#..........#.. +..............#..........................#............#.........#....#...#.....#.........................#....#...............##.. +...................#.........................................................#.......#........#..............#..........#......... +.#........#....#..................................#..#.#...............#............#...........#................................. +...............................................................................................#.....#....#.............#......... +...........#..........#...........................................#.........#..............#................#..........#.......... +............#.................................###..............#.#...............................................................# +......#...................................................................................#............#...#..........#.....#..... +.............#..................#...................#......#.........#..#.......................#....#................#..........# +..#....##................................#.................#...........................#.......#....#...#..........#.............. +...............................#......................................#.........#.............#......................#..........#. diff --git a/2024/06/solution.nix b/2024/06/solution.nix new file mode 100644 index 0000000..33ad9b0 --- /dev/null +++ b/2024/06/solution.nix @@ -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) + ; + +} diff --git a/2024/flake.lock b/2024/flake.lock new file mode 100644 index 0000000..dc94ab4 --- /dev/null +++ b/2024/flake.lock @@ -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 +} diff --git a/2024/flake.nix b/2024/flake.nix new file mode 100644 index 0000000..02d846d --- /dev/null +++ b/2024/flake.nix @@ -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 + ]; + + }; +}