Skip to content

Commit 169c7d6

Browse files
authored
Merge pull request #94 from Arxcis/day10/deno
Solve day 10 in deno.ts
2 parents 7b17e6c + a1097da commit 169c7d6

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

days/day-10/io/arxcis.input

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
54
2+
91
3+
137
4+
156
5+
31
6+
70
7+
143
8+
51
9+
50
10+
18
11+
1
12+
149
13+
129
14+
151
15+
95
16+
148
17+
41
18+
144
19+
7
20+
125
21+
155
22+
14
23+
114
24+
108
25+
57
26+
118
27+
147
28+
24
29+
25
30+
73
31+
26
32+
8
33+
115
34+
44
35+
12
36+
47
37+
106
38+
120
39+
132
40+
121
41+
35
42+
105
43+
60
44+
9
45+
6
46+
65
47+
111
48+
133
49+
38
50+
138
51+
101
52+
126
53+
39
54+
78
55+
92
56+
53
57+
119
58+
136
59+
154
60+
140
61+
52
62+
15
63+
90
64+
30
65+
40
66+
64
67+
67
68+
139
69+
76
70+
32
71+
98
72+
113
73+
80
74+
13
75+
104
76+
86
77+
27
78+
61
79+
157
80+
79
81+
122
82+
59
83+
150
84+
89
85+
158
86+
107
87+
77
88+
112
89+
5
90+
83
91+
58
92+
21
93+
2
94+
66

days/day-10/io/arxcis.output

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2046
2+
1157018619904

days/day-10/solutions/day10.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const matches = (await Deno.readTextFile("/dev/stdin"))
2+
.matchAll(/\d+/g)
3+
4+
const joltages = [...matches]
5+
.map(([match]) => parseInt(match))
6+
.sort((a, b) => a - b)
7+
8+
const boundedJoltages = [0, ...joltages, joltages[joltages.length - 1] + 3]
9+
10+
let joltageDiffCount = { [1]: 0, [3]: 0 }
11+
12+
/** --- part 1 --- */
13+
for (let i = 1; i < boundedJoltages.length; ++i) {
14+
const joltageDiff: 1 | 3 = boundedJoltages[i] - boundedJoltages[i - 1] as 1 | 3;
15+
joltageDiffCount[joltageDiff] = joltageDiffCount[joltageDiff] + 1
16+
}
17+
console.log(`${joltageDiffCount[1] * joltageDiffCount[3]}`)
18+
19+
/** --- part 2 --- */
20+
let combinations = 1;
21+
for (let i = 1; i < boundedJoltages.length; ++i) {
22+
23+
// Compute combinations per +1-group: (4,5,6,7,8) -> 1+(0+1+2+3+4) = 10 combinations
24+
let plusOneGroupCombinations = 1
25+
for (let j = 0; boundedJoltages[i] - boundedJoltages[i-1] === 1; ++j, ++i) {
26+
plusOneGroupCombinations += j
27+
}
28+
29+
// Multiply +1-groups together, to get the total combinations:
30+
//
31+
// comb(1) * comb(4,5,6,7,8) * comb(11,12,13) -> (1)*(10)*(2) = 20
32+
combinations *= plusOneGroupCombinations
33+
}
34+
35+
console.log(`${combinations}`)

days/day-10/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ D=$(dirname $(realpath $0))
55

66
echo ""
77
echo "--- Day 10: --- Adapter Combinations ---"
8-
$D/../../lang/sml.sh "$D/solutions/*.sml" "$D/io/*"
98
$D/../../lang/python.sh "$D/solutions/*.py" "$D/io/*"
9+
$D/../../lang/deno.sh "$D/solutions/*.ts" "$D/io/*"
10+
$D/../../lang/sml.sh "$D/solutions/*.sml" "$D/io/*"
1011
echo ""

0 commit comments

Comments
 (0)