2024 day 2 part 1
This commit is contained in:
parent
7dd46d2fe6
commit
faf1409d7b
6
2024/02/example.txt
Normal file
6
2024/02/example.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
59
2024/02/flake.lock
Normal file
59
2024/02/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/02/flake.nix
Normal file
31
2024/02/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 {file = ./example.txt;}).part1result;
|
||||
part1 = (solution {file = ./input.txt;}).part1result;
|
||||
|
||||
part2example = (solution {file = ./example.txt;}).part2result;
|
||||
part2 = (solution {file = ./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
|
||||
];
|
||||
|
||||
};
|
||||
}
|
1000
2024/02/input.txt
Normal file
1000
2024/02/input.txt
Normal file
File diff suppressed because it is too large
Load diff
38
2024/02/solution.nix
Normal file
38
2024/02/solution.nix
Normal file
|
@ -0,0 +1,38 @@
|
|||
{lib, ...}: {file}: rec {
|
||||
rawContent = builtins.readFile file;
|
||||
content = lib.strings.removeSuffix "\n" rawContent;
|
||||
|
||||
toLines = lib.strings.splitString "\n";
|
||||
toLevels = map ( line: line
|
||||
|> lib.strings.splitString " "
|
||||
|> map lib.strings.toInt
|
||||
)
|
||||
;
|
||||
isSafe = builtins.foldl'
|
||||
({prev ? null, inc ? null, safe}: next:
|
||||
if !safe then {inherit safe;} else
|
||||
if prev == null then {prev = next; inherit safe;} else
|
||||
let
|
||||
diff = next - prev;
|
||||
safeDiff = abs diff > 0 && abs diff <= 3;
|
||||
isInc = next > prev;
|
||||
in {
|
||||
prev = next;
|
||||
safe = safeDiff && ( isInc == inc || isNull inc );
|
||||
inc = isInc;
|
||||
}
|
||||
)
|
||||
{safe = true;};
|
||||
abs = n: if n > 0 then n else -n;
|
||||
|
||||
countSafety = lib.lists.count (level: level.safe);
|
||||
|
||||
getPart1Result = input: input
|
||||
|> toLines
|
||||
|> toLevels
|
||||
|> map isSafe
|
||||
|> countSafety
|
||||
;
|
||||
|
||||
part1result = getPart1Result content;
|
||||
}
|
75
2024/02/solution.test.nix
Normal file
75
2024/02/solution.test.nix
Normal file
|
@ -0,0 +1,75 @@
|
|||
{describe, it, ...}:
|
||||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
solution = import ./solution.nix pkgs;
|
||||
example = (solution {file = ./example.txt;});
|
||||
in [
|
||||
(describe "part1" [
|
||||
(it "gets file content" {
|
||||
expected = ''
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9'';
|
||||
actual = example.content;
|
||||
})
|
||||
(it "splits lines" {
|
||||
expected = [
|
||||
"7 6 4 2 1"
|
||||
"1 2 7 8 9"
|
||||
];
|
||||
actual = example.toLines ''
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9'';
|
||||
})
|
||||
(it "turns to numbers" {
|
||||
expected = [
|
||||
[1 2 3 4]
|
||||
[5 6 7 8]
|
||||
];
|
||||
actual = example.toLevels ["1 2 3 4" "5 6 7 8"];
|
||||
})
|
||||
(it "gets safety" {
|
||||
expected = [
|
||||
true
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
];
|
||||
actual = [
|
||||
[7 6 4 2 1]
|
||||
[1 2 7 8 9]
|
||||
[9 7 6 2 1]
|
||||
[1 3 2 4 5]
|
||||
[8 6 4 4 1]
|
||||
[1 3 6 7 9]
|
||||
] |> map example.isSafe
|
||||
|> map (v: v.safe);
|
||||
})
|
||||
(it "counts total safety" {
|
||||
expected = 3;
|
||||
actual = example.countSafety [
|
||||
{safe = true;}
|
||||
{safe = false;}
|
||||
{safe = true;}
|
||||
{safe = false;}
|
||||
{safe = false;}
|
||||
{safe = true;}
|
||||
];
|
||||
})
|
||||
(it "gets the right result" {
|
||||
expected = 2;
|
||||
actual = example.getPart1Result ''
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9'';
|
||||
})
|
||||
])
|
||||
]
|
Loading…
Reference in a new issue