Skip to content

Commit deb3267

Browse files
committed
[2020] Day10 Ada
1 parent bc92b18 commit deb3267

File tree

12 files changed

+378
-0
lines changed

12 files changed

+378
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: 2020-day10-ada
2+
on:
3+
push:
4+
branches:
5+
- master
6+
7+
jobs:
8+
build:
9+
name: AoC-2020-day10-Ada
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2.3.4
13+
- uses: cachix/install-nix-action@v12
14+
with:
15+
nix_path: nixpkgs=channel:nixos-unstable
16+
- run: |
17+
cd 2020/day10
18+
./build.sh
19+
./day10

2020/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,17 @@ Docker:
207207
### Time
208208

209209
`bash -c 'time ./day09'`
210+
211+
## Day 10 - Ada
212+
213+
### Build
214+
215+
`./build.sh`
216+
217+
### Run
218+
219+
`./day10`
220+
221+
### Time
222+
223+
`bash -c 'time ./day10'`

2020/day10/.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use nix

2020/day10/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
day10
2+
day10.ali
3+
day10.o

2020/day10/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
nix-shell --run 'gnatmake day10.adb'

2020/day10/day10.adb

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
with Ada.Containers; use Ada.Containers;
2+
with Ada.Containers.Ordered_Sets;
3+
with Ada.Text_IO; use Ada.Text_IO;
4+
5+
procedure Day10 is
6+
7+
package Integer_Sets is new Ada.Containers.Ordered_Sets
8+
(Element_Type => Integer);
9+
10+
use Integer_Sets;
11+
subtype Long is Long_Integer;
12+
13+
File_Name : constant String := "input.txt";
14+
--File_Name : constant String := "sample.txt";
15+
--File_Name : constant String := "sample2.txt";
16+
17+
File : File_Type;
18+
Plugs : Set;
19+
Result_A : Integer;
20+
Result_B : Long;
21+
22+
procedure Read_Input is
23+
begin
24+
Open(File => File,
25+
Mode => In_File,
26+
Name => File_Name);
27+
28+
while not End_Of_File(File) loop
29+
declare
30+
Line: String := Get_Line(File);
31+
begin
32+
Plugs.Include(Integer'Value(Line));
33+
end;
34+
end loop;
35+
--Put_Line("Plugs " & Count_Type'Image(Plugs.Length));
36+
37+
Close(File);
38+
end Read_Input;
39+
40+
function Solve_A return Integer is
41+
Diff_1 : Natural := 0;
42+
Diff_3 : Natural := 0;
43+
Prev_Plug : Natural := 0;
44+
begin
45+
for Plug of Plugs loop
46+
case Plug - Prev_Plug is
47+
when 1 =>
48+
Diff_1 := Diff_1 + 1;
49+
when 3 =>
50+
Diff_3 := Diff_3 + 1;
51+
when others =>
52+
null;
53+
end case;
54+
Prev_Plug := Plug;
55+
end loop;
56+
--Put_Line("1 " & Integer'Image(Diff_1) & " 3 " & Integer'Image(Diff_3));
57+
58+
-- Target device is always 3 jolts higher than the last adapter
59+
Diff_3 := Diff_3 + 1;
60+
return Diff_1 * Diff_3;
61+
end Solve_A;
62+
63+
function Solve_B return Long is
64+
Combinations : Long := 1;
65+
-- How many ones in a row
66+
Ones : Natural := 0;
67+
Prev_Plug : Natural := 0;
68+
69+
function Calculate_Combinations return Long is
70+
begin
71+
case Ones is
72+
when 0 | 1 =>
73+
-- No number can be deleted - one path
74+
return Combinations;
75+
when 2 =>
76+
-- One number can be deleted - 2 paths
77+
return Combinations * 2;
78+
when 3 =>
79+
-- Two nubmers can be deleted - 4 paths
80+
return Combinations * 4;
81+
when 4 =>
82+
-- Three nubmers can be deleted - 7 paths
83+
return Combinations * 7;
84+
when others =>
85+
return Combinations;
86+
end case;
87+
end Calculate_Combinations;
88+
89+
begin
90+
for Plug of Plugs loop
91+
case Plug - Prev_Plug is
92+
when 1 =>
93+
Ones := Ones + 1;
94+
when 3 =>
95+
Combinations := Calculate_Combinations;
96+
Ones := 0;
97+
when others =>
98+
null;
99+
end case;
100+
Prev_Plug := Plug;
101+
end loop;
102+
-- Target device is always 3 jolts higher than the last adapter
103+
return Calculate_Combinations;
104+
end Solve_B;
105+
106+
107+
begin
108+
Read_Input;
109+
110+
-- 2376
111+
Put_Line("Solving Day10A...");
112+
Result_A := Solve_A;
113+
Put_Line(Integer'Image(Result_A));
114+
115+
-- 129586085429248
116+
Put_Line("Solving Day10B...");
117+
Result_B := Solve_B;
118+
Put_Line(Long'Image(Result_B));
119+
end Day10;

2020/day10/nix/sources.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"niv": {
3+
"branch": "master",
4+
"description": "Easy dependency management for Nix projects",
5+
"homepage": "https://github.com/nmattia/niv",
6+
"owner": "nmattia",
7+
"repo": "niv",
8+
"rev": "ba57d5a29b4e0f2085917010380ef3ddc3cf380f",
9+
"sha256": "1kpsvc53x821cmjg1khvp1nz7906gczq8mp83664cr15h94sh8i4",
10+
"type": "tarball",
11+
"url": "https://github.com/nmattia/niv/archive/ba57d5a29b4e0f2085917010380ef3ddc3cf380f.tar.gz",
12+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
13+
},
14+
"nixpkgs": {
15+
"branch": "nixpkgs-unstable",
16+
"description": "Nix Packages collection",
17+
"homepage": "",
18+
"owner": "NixOS",
19+
"repo": "nixpkgs",
20+
"rev": "13efba98613303971401b6872b92bfe8a8ed8612",
21+
"sha256": "0m989ba3nav3dp4a8nj86733xsbybjprcas00qmhp7msld5fbmwg",
22+
"type": "tarball",
23+
"url": "https://github.com/NixOS/nixpkgs/archive/13efba98613303971401b6872b92bfe8a8ed8612.tar.gz",
24+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
25+
}
26+
}

2020/day10/nix/sources.nix

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# This file has been generated by Niv.
2+
3+
let
4+
5+
#
6+
# The fetchers. fetch_<type> fetches specs of type <type>.
7+
#
8+
9+
fetch_file = pkgs: spec:
10+
if spec.builtin or true then
11+
builtins_fetchurl { inherit (spec) url sha256; }
12+
else
13+
pkgs.fetchurl { inherit (spec) url sha256; };
14+
15+
fetch_tarball = pkgs: spec:
16+
if spec.builtin or true then
17+
builtins_fetchTarball { inherit (spec) url sha256; }
18+
else
19+
pkgs.fetchzip { inherit (spec) url sha256; };
20+
21+
fetch_git = spec:
22+
builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
23+
24+
fetch_builtin-tarball = spec:
25+
builtins.trace
26+
''
27+
WARNING:
28+
The niv type "builtin-tarball" will soon be deprecated. You should
29+
instead use `builtin = true`.
30+
31+
$ niv modify <package> -a type=tarball -a builtin=true
32+
''
33+
builtins_fetchTarball { inherit (spec) url sha256; };
34+
35+
fetch_builtin-url = spec:
36+
builtins.trace
37+
''
38+
WARNING:
39+
The niv type "builtin-url" will soon be deprecated. You should
40+
instead use `builtin = true`.
41+
42+
$ niv modify <package> -a type=file -a builtin=true
43+
''
44+
(builtins_fetchurl { inherit (spec) url sha256; });
45+
46+
#
47+
# Various helpers
48+
#
49+
50+
# The set of packages used when specs are fetched using non-builtins.
51+
mkPkgs = sources:
52+
let
53+
sourcesNixpkgs =
54+
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
55+
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
56+
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
57+
in
58+
if builtins.hasAttr "nixpkgs" sources
59+
then sourcesNixpkgs
60+
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
61+
import <nixpkgs> {}
62+
else
63+
abort
64+
''
65+
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
66+
add a package called "nixpkgs" to your sources.json.
67+
'';
68+
69+
# The actual fetching function.
70+
fetch = pkgs: name: spec:
71+
72+
if ! builtins.hasAttr "type" spec then
73+
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
74+
else if spec.type == "file" then fetch_file pkgs spec
75+
else if spec.type == "tarball" then fetch_tarball pkgs spec
76+
else if spec.type == "git" then fetch_git spec
77+
else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec
78+
else if spec.type == "builtin-url" then fetch_builtin-url spec
79+
else
80+
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
81+
82+
# Ports of functions for older nix versions
83+
84+
# a Nix version of mapAttrs if the built-in doesn't exist
85+
mapAttrs = builtins.mapAttrs or (
86+
f: set: with builtins;
87+
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
88+
);
89+
90+
# fetchTarball version that is compatible between all the versions of Nix
91+
builtins_fetchTarball = { url, sha256 }@attrs:
92+
let
93+
inherit (builtins) lessThan nixVersion fetchTarball;
94+
in
95+
if lessThan nixVersion "1.12" then
96+
fetchTarball { inherit url; }
97+
else
98+
fetchTarball attrs;
99+
100+
# fetchurl version that is compatible between all the versions of Nix
101+
builtins_fetchurl = { url, sha256 }@attrs:
102+
let
103+
inherit (builtins) lessThan nixVersion fetchurl;
104+
in
105+
if lessThan nixVersion "1.12" then
106+
fetchurl { inherit url; }
107+
else
108+
fetchurl attrs;
109+
110+
# Create the final "sources" from the config
111+
mkSources = config:
112+
mapAttrs (
113+
name: spec:
114+
if builtins.hasAttr "outPath" spec
115+
then abort
116+
"The values in sources.json should not have an 'outPath' attribute"
117+
else
118+
spec // { outPath = fetch config.pkgs name spec; }
119+
) config.sources;
120+
121+
# The "config" used by the fetchers
122+
mkConfig =
123+
{ sourcesFile ? ./sources.json
124+
, sources ? builtins.fromJSON (builtins.readFile sourcesFile)
125+
, pkgs ? mkPkgs sources
126+
}: rec {
127+
# The sources, i.e. the attribute set of spec name to spec
128+
inherit sources;
129+
130+
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
131+
inherit pkgs;
132+
};
133+
in
134+
mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }

2020/day10/sample.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
16
2+
10
3+
15
4+
5
5+
1
6+
11
7+
7
8+
19
9+
6
10+
12
11+
4

2020/day10/sample2.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
28
2+
33
3+
18
4+
42
5+
31
6+
14
7+
46
8+
20
9+
48
10+
47
11+
24
12+
23
13+
49
14+
45
15+
19
16+
38
17+
39
18+
11
19+
1
20+
32
21+
25
22+
35
23+
8
24+
17
25+
7
26+
9
27+
4
28+
2
29+
34
30+
10
31+
3

0 commit comments

Comments
 (0)