2024 day 5
This commit is contained in:
parent
d0f2fa688f
commit
1f2171f3a5
|
@ -95,8 +95,6 @@
|
|||
|> break3s
|
||||
;
|
||||
|
||||
log = t: lib.traceSeq t t;
|
||||
|
||||
break3x3s = l: l
|
||||
|> map break3s
|
||||
|> (groups:
|
||||
|
|
28
2024/05/example.txt
Normal file
28
2024/05/example.txt
Normal file
|
@ -0,0 +1,28 @@
|
|||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
75,47,61,53,29
|
||||
97,61,53,29,13
|
||||
75,29,13
|
||||
75,97,47,61,53
|
||||
61,13,29
|
||||
97,13,75,29,47
|
59
2024/05/flake.lock
Normal file
59
2024/05/flake.lock
Normal 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
|
||||
}
|
31
2024/05/flake.nix
Normal file
31
2024/05/flake.nix
Normal file
|
@ -0,0 +1,31 @@
|
|||
{
|
||||
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";};
|
||||
solution = import ./solution.nix pkgs;
|
||||
in {
|
||||
|
||||
part1example = (solution (pkgs.lib.readFile ./example.txt)).part1result;
|
||||
part1 = (solution (pkgs.lib.readFile ./input.txt)).part1result;
|
||||
|
||||
part2example = (solution (pkgs.lib.readFile ./example.txt)).part2result;
|
||||
part2 = (solution (pkgs.lib.readFile ./input.txt)).part2result;
|
||||
|
||||
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
|
||||
];
|
||||
|
||||
};
|
||||
}
|
1362
2024/05/input.txt
Normal file
1362
2024/05/input.txt
Normal file
File diff suppressed because it is too large
Load diff
69
2024/05/solution.nix
Normal file
69
2024/05/solution.nix
Normal file
|
@ -0,0 +1,69 @@
|
|||
{lib, ...}: input: rec {
|
||||
|
||||
content = lib.trim input;
|
||||
|
||||
parts = let
|
||||
sects = lib.splitString "\n\n" content;
|
||||
in {
|
||||
rules = builtins.elemAt sects 0
|
||||
|> lib.splitString "\n"
|
||||
|> map (lib.splitString "|")
|
||||
|> builtins.foldl' (acc: el:
|
||||
let
|
||||
b = builtins.elemAt el 0;
|
||||
a = builtins.elemAt el 1;
|
||||
in
|
||||
acc // {
|
||||
${b} = acc.${b} or [] ++ [a];
|
||||
}) {};
|
||||
updates = builtins.elemAt sects 1
|
||||
|> lib.splitString "\n"
|
||||
|> map (lib.splitString ",");
|
||||
}
|
||||
;
|
||||
|
||||
check = {rules, update}: builtins.attrNames rules
|
||||
|> map
|
||||
(ruleName: let
|
||||
i = lib.lists.findFirstIndex (x: x == ruleName) null update;
|
||||
before = if isNull i then [] else lib.sublist (0) (i) update;
|
||||
in {
|
||||
inherit before;
|
||||
rules = rules.${ruleName};
|
||||
})
|
||||
|> builtins.foldl'
|
||||
(valid: {rules, before}: valid && builtins.all (e: !builtins.any (r: r == e) rules) before)
|
||||
true
|
||||
;
|
||||
|
||||
getValid = {rules, updates}:
|
||||
builtins.filter (update: check {inherit rules update;}) updates;
|
||||
|
||||
getMiddles = list: list
|
||||
|> map (row: builtins.elemAt row ((builtins.length row) / 2))
|
||||
|> map lib.strings.toInt
|
||||
;
|
||||
|
||||
sum = builtins.foldl' (a: n: a + n) 0;
|
||||
|
||||
part1result = parts
|
||||
|> getValid
|
||||
|> getMiddles
|
||||
|> sum
|
||||
;
|
||||
|
||||
sortUpdate = rules: update:
|
||||
builtins.sort (fst: snd: builtins.any (rule: rule == fst) (rules.${snd} or [])) update
|
||||
;
|
||||
|
||||
getInvalid = {rules, updates}:
|
||||
builtins.filter (update: !check {inherit rules update;}) updates;
|
||||
|
||||
part2result = parts
|
||||
|> getInvalid
|
||||
|> map (sortUpdate parts.rules)
|
||||
|> getMiddles
|
||||
|> sum
|
||||
;
|
||||
|
||||
}
|
100
2024/05/solution.test.nix
Normal file
100
2024/05/solution.test.nix
Normal file
|
@ -0,0 +1,100 @@
|
|||
{describe, it, ...}:
|
||||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
solution = import ./solution.nix pkgs;
|
||||
exampleInput = pkgs.lib.readFile ./example.txt;
|
||||
example = solution exampleInput;
|
||||
parsed = {
|
||||
rules = {
|
||||
"29" = ["13"];
|
||||
"47" = ["53" "13" "61" "29"];
|
||||
"53" = ["29" "13"];
|
||||
"61" = ["13" "53" "29"];
|
||||
"75" = ["29" "53" "47" "61" "13"];
|
||||
"97" = ["13" "61" "47" "29" "53" "75"];
|
||||
};
|
||||
updates = [
|
||||
["75" "47" "61" "53" "29"]
|
||||
["97" "61" "53" "29" "13"]
|
||||
["75" "29" "13"]
|
||||
["75" "97" "47" "61" "53"]
|
||||
["61" "13" "29"]
|
||||
["97" "13" "75" "29" "47"]];
|
||||
};
|
||||
in [
|
||||
(describe "part 1" [
|
||||
(it "splits the rules from updates" {
|
||||
actual = example.parts;
|
||||
expected = parsed;
|
||||
})
|
||||
(it "check if an update complies with rule" {
|
||||
actual = map example.check [{
|
||||
rules = {
|
||||
"0" = ["1"];
|
||||
"1" = ["2"];
|
||||
};
|
||||
update = ["0" "1" "2"];
|
||||
}
|
||||
{
|
||||
rules = {
|
||||
"0" = ["1"];
|
||||
"1" = ["0"];
|
||||
};
|
||||
update = ["0" "1" "2"];
|
||||
}
|
||||
{
|
||||
inherit (parsed) rules;
|
||||
update =
|
||||
["75" "47" "61" "53" "29"];
|
||||
}
|
||||
{
|
||||
inherit (parsed) rules;
|
||||
update =
|
||||
["75" "97" "47" "61" "53"];
|
||||
}
|
||||
];
|
||||
expected = [true false true false];
|
||||
})
|
||||
(it "gets valid updates" {
|
||||
actual = example.getValid parsed;
|
||||
expected = [
|
||||
["75" "47" "61" "53" "29"]
|
||||
["97" "61" "53" "29" "13"]
|
||||
["75" "29" "13"]
|
||||
];
|
||||
})
|
||||
(it "sums middles of arrays" {
|
||||
actual = example.getMiddles [
|
||||
["75" "47" "61" "53" "29"]
|
||||
["97" "61" "53" "29" "13"]
|
||||
["75" "29" "13"]
|
||||
];
|
||||
expected = [61 53 29];
|
||||
})
|
||||
(it "gets the result" {
|
||||
actual = example.part1result;
|
||||
expected = 143;
|
||||
})
|
||||
])
|
||||
(describe "part 2" [
|
||||
(it "sorts an update" {
|
||||
actual = parsed.updates
|
||||
|> map (example.sortUpdate parsed.rules);
|
||||
# in reverse but who cares
|
||||
expected =
|
||||
[["29" "53" "61" "47" "75"] ["13" "29" "53" "61" "97"] ["13" "29" "75"] ["53" "61" "47" "75" "97"] ["13" "29" "61"] ["13" "29" "47" "75" "97"]];
|
||||
})
|
||||
(it "filters invalid" {
|
||||
actual = example.getInvalid parsed;
|
||||
expected = [
|
||||
["75" "97" "47" "61" "53"]
|
||||
["61" "13" "29"]
|
||||
["97" "13" "75" "29" "47"]
|
||||
];
|
||||
})
|
||||
(it "gets result" {
|
||||
actual = example.part2result;
|
||||
expected = 123;
|
||||
})
|
||||
])
|
||||
]
|
Loading…
Reference in a new issue