2024 day 1

This commit is contained in:
tristan 2024-12-01 11:02:04 +00:00
parent 8cbfa778c2
commit 7dd46d2fe6
7 changed files with 1248 additions and 0 deletions

6
2024/01/example.txt Normal file
View file

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

59
2024/01/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
}

31
2024/01/flake.nix Normal file
View 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/01/input.txt Normal file

File diff suppressed because it is too large Load diff

42
2024/01/solution.nix Normal file
View file

@ -0,0 +1,42 @@
{lib, ...}: {file}: rec {
rawContent = builtins.readFile file;
content = lib.strings.removeSuffix "\n" rawContent;
lines = lib.strings.splitString "\n" content;
getLeftAndRight = s: s
|> lib.strings.splitString " "
|> map lib.strings.toInt
;
splitLists = s: s
|> map getLeftAndRight
|> builtins.foldl'
(prev: next: {
left = prev.left ++ [(builtins.elemAt next 0)];
right = prev.right ++ [ ( builtins.elemAt next 1 ) ];})
{left = []; right = [];}
;
sortLists = l: {
left = lib.lists.sort (a: b: a < b) l.left;
right = lib.lists.sort (a: b: a < b) l.right;
};
distance = l: l
|> sortLists
|> ({left, right}: lib.zipListsWith (ln: rn: abs (ln - rn)) left right)
;
total = l: builtins.foldl' (x: y: x+y) 0 l;
abs = n: if n > 0 then n else -n;
getDiff = s: s
|> splitLists
|> distance
|> total
;
similarity = l: l
|> ({left, right}: map (ln: ln * lib.count (rn: ln == rn) right) left)
;
getSimilarity = s: s
|> splitLists
|> similarity
|> total
;
part1result = getDiff lines;
part2result = getSimilarity lines;
}

100
2024/01/solution.test.nix Normal file
View file

@ -0,0 +1,100 @@
{describe, it, ...}:
let
pkgs = import <nixpkgs> {};
solution = import ./solution.nix pkgs;
example = (solution {file = ./example.txt;});
in [
(describe "part1" [
(it "gets file content" {
expected = ''
3 4
4 3
2 5
1 3
3 9
3 3'';
actual = example.content;
})
(it "splits lines" {
expected = [
"3 4"
"4 3"
"2 5"
"1 3"
"3 9"
"3 3"
];
actual = example.lines;
})
(it "splits left and right" {
expected = [
3 4
];
actual = example.getLeftAndRight "3 4";
})
(it "splits list into left and right" {
expected = {
left = [ 3 4 2 1 3 3 ];
right = [ 4 3 5 3 9 3 ];
};
actual = example.splitLists [
"3 4"
"4 3"
"2 5"
"1 3"
"3 9"
"3 3"
];
})
(it "splits list into left and right" {
expected = {
left = [ 1 2 3 3 3 4 ];
right = [ 3 3 3 4 5 9 ];
};
actual = example.sortLists {
left = [ 3 4 2 1 3 3 ];
right = [ 4 3 5 3 9 3 ];
};
})
(it "calculates the distance between each item" {
expected = [2 1 0 1 2 5];
actual = example.distance {
left = [ 3 4 2 1 3 3 ];
right = [ 4 3 5 3 9 3 ];
};
})
(it "calculates the total" {
expected = 11;
actual = example.total [2 1 0 1 2 5];
})
(it "gets diff of list of strings" {
expected = 11;
actual = example.getDiff [
"3 4"
"4 3"
"2 5"
"1 3"
"3 9"
"3 3"
];
})
(it "gets similarity" {
expected = [9 4 0 0 9 9];
actual = example.similarity {
left = [ 3 4 2 1 3 3 ];
right = [ 4 3 5 3 9 3 ];
};
})
(it "gets similarity of list of strings" {
expected = 31;
actual = example.getSimilarity [
"3 4"
"4 3"
"2 5"
"1 3"
"3 9"
"3 3"
];
})
])
]

View file

@ -66,4 +66,14 @@ if (advent) {
1. [:star: :star:](https://adventofcode.com/2023/day/1 "see puzzle")
2. [:star: :star:](https://adventofcode.com/2023/day/2 "see puzzle")
3. [:star: :star:](https://adventofcode.com/2023/day/3 "see puzzle")
4. [:star: :star:](https://adventofcode.com/2023/day/4 "see puzzle")
5. [:star: :star:](https://adventofcode.com/2023/day/5 "see puzzle")
6. [:star: :star:](https://adventofcode.com/2023/day/6 "see puzzle")
7. [:star: :star:](https://adventofcode.com/2023/day/7 "see puzzle")
8. [:star: :star:](https://adventofcode.com/2023/day/8 "see puzzle")
9. [:star: :star:](https://adventofcode.com/2023/day/9 "see puzzle")
10. [:star: :star:](https://adventofcode.com/2023/day/10 "see puzzle")
#### [2024](https://adventofcode.com/2024 "2024 puzzle calendar")
1. [:star: :star:](https://adventofcode.com/2024/day/1 "see puzzle")