Skip to content

Commit 11d0e2a

Browse files
2019 day 1 and 2 and 2020 day 1
1 parent 031ba76 commit 11d0e2a

File tree

6 files changed

+300
-67
lines changed

6 files changed

+300
-67
lines changed

2019/day1/js/part2.mjs

+6-8
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ let input = fs.readFileSync("./input.txt", "utf8");
55

66
const inputList = input.split("\r\n");
77

8-
const calcFuel = mass => {
9-
const devidedByThree = Math.floor(mass / 3);
8+
const sum = list => list.reduce((acc, cur) => acc + cur);
109

11-
if (devidedByThree <= 2) return 0;
12-
13-
const fuel = devidedByThree - 2;
14-
return fuel + calcFuel(fuel);
10+
const calculateFuel = mass => {
11+
const fuel = Math.floor(mass / 3) - 2;
12+
return fuel > 0 ? fuel + calculateFuel(fuel) : 0;
1513
};
1614

17-
const fuelList = inputList.map(calcFuel);
15+
const fuelList = inputList.map(calculateFuel);
1816

19-
const sumFuelRequirement = fuelList.reduce((acc, cur) => acc + cur);
17+
const sumFuelRequirement = sum(fuelList);
2018

2119
console.log(sumFuelRequirement);

2019/day2/part1.mjs

+26-23
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
import fs from 'fs';
1+
import fs from "fs";
22

3-
const input = fs.readFileSync('./input.txt').toString().split(',').map(value => +value);
3+
const input = fs
4+
.readFileSync("./input.txt", "utf8")
5+
.split(",")
6+
.map(value => +value);
47

58
input[1] = 12;
69
input[2] = 2;
710

811
const processOpcode = (input, position) => {
9-
const output = [...input];
10-
const opcode = output[position];
11-
const first = output[output[position + 1]];
12-
const second = output[output[position + 2]];
13-
const placementPosition = output[position + 3];
14-
switch(opcode) {
15-
case 1: {
16-
const result = first + second;
17-
output[placementPosition] = result;
18-
return processOpcode(output, position + 4);
19-
}
20-
case 2: {
21-
const result = first * second;
22-
output[placementPosition] = result;
23-
return processOpcode(output, position + 4);
24-
}
25-
case 99:
26-
default:
27-
return input;
12+
const output = [...input];
13+
const opcode = output[position];
14+
const first = output[output[position + 1]];
15+
const second = output[output[position + 2]];
16+
const placementPosition = output[position + 3];
17+
switch (opcode) {
18+
case 1: {
19+
const result = first + second;
20+
output[placementPosition] = result;
21+
return processOpcode(output, position + 4);
2822
}
29-
}
23+
case 2: {
24+
const result = first * second;
25+
output[placementPosition] = result;
26+
return processOpcode(output, position + 4);
27+
}
28+
case 99:
29+
default:
30+
return input;
31+
}
32+
};
3033

31-
console.log(processOpcode(input, 0));
34+
console.log(processOpcode(input, 0));

2019/day2/part2.mjs

+39-36
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
1-
import fs from 'fs';
1+
import fs from "fs";
22

3-
const input = fs.readFileSync('./input.txt').toString().split(',').map(value => +value);
3+
const input = fs
4+
.readFileSync("./input.txt", "utf8")
5+
.split(",")
6+
.map(value => +value);
47

58
const program = (input, noun, verb) => {
6-
const memory = [...input];
7-
memory[1] = noun;
8-
memory[2] = verb;
9+
const memory = [...input];
10+
memory[1] = noun;
11+
memory[2] = verb;
912

10-
const processOpcode = (input, position) => {
11-
const output = [...input];
12-
const opcode = output[position];
13-
const first = output[output[position + 1]];
14-
const second = output[output[position + 2]];
15-
const placementPosition = output[position + 3];
16-
switch(opcode) {
17-
case 1: {
18-
const result = first + second;
19-
output[placementPosition] = result;
20-
return processOpcode(output, position + 4);
21-
}
22-
case 2: {
23-
const result = first * second;
24-
output[placementPosition] = result;
25-
return processOpcode(output, position + 4);
26-
}
27-
case 99:
28-
default:
29-
return input;
30-
}
13+
const processOpcode = (input, position) => {
14+
const output = [...input];
15+
const opcode = output[position];
16+
const first = output[output[position + 1]];
17+
const second = output[output[position + 2]];
18+
const placementPosition = output[position + 3];
19+
switch (opcode) {
20+
case 1: {
21+
const result = first + second;
22+
output[placementPosition] = result;
23+
return processOpcode(output, position + 4);
24+
}
25+
case 2: {
26+
const result = first * second;
27+
output[placementPosition] = result;
28+
return processOpcode(output, position + 4);
29+
}
30+
case 99:
31+
default:
32+
return input;
3133
}
34+
};
3235

33-
const result = processOpcode(memory, 0);
34-
return result[0];
35-
}
36+
const result = processOpcode(memory, 0);
37+
return result[0];
38+
};
3639

37-
for(let noun = 0; noun < 100; noun++) {
38-
for(let verb = 0; verb < 100; verb++) {
39-
const output = program(input, noun, verb);
40-
if(output === 19690720) {
41-
console.log(100 * noun + verb);
42-
}
40+
for (let noun = 0; noun < 100; noun++) {
41+
for (let verb = 0; verb < 100; verb++) {
42+
const output = program(input, noun, verb);
43+
if (output === 19690720) {
44+
console.log(100 * noun + verb);
4345
}
44-
}
46+
}
47+
}

2020/day1/input.txt

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
1935
2+
1956
3+
1991
4+
1425
5+
1671
6+
1537
7+
1984
8+
1569
9+
1873
10+
1840
11+
1720
12+
1937
13+
1823
14+
1625
15+
1727
16+
1812
17+
1714
18+
1900
19+
1939
20+
1931
21+
1951
22+
1756
23+
1942
24+
1611
25+
1979
26+
1930
27+
1996
28+
2000
29+
1544
30+
1780
31+
1687
32+
1760
33+
1836
34+
1814
35+
1691
36+
1817
37+
1964
38+
1899
39+
1577
40+
1547
41+
866
42+
1560
43+
1988
44+
1601
45+
1970
46+
1738
47+
1507
48+
1667
49+
1851
50+
1933
51+
1515
52+
1856
53+
1969
54+
1860
55+
1801
56+
2007
57+
1866
58+
1800
59+
1749
60+
1843
61+
1711
62+
1495
63+
1905
64+
763
65+
1672
66+
1858
67+
1987
68+
1492
69+
1849
70+
1993
71+
1737
72+
1874
73+
1658
74+
1810
75+
1665
76+
1768
77+
1950
78+
1879
79+
1816
80+
1868
81+
1995
82+
1763
83+
1783
84+
1833
85+
1968
86+
1847
87+
1748
88+
1725
89+
1891
90+
1755
91+
286
92+
1976
93+
1977
94+
1655
95+
1808
96+
1986
97+
1779
98+
1861
99+
1953
100+
1888
101+
1792
102+
1811
103+
1872
104+
1790
105+
1839
106+
1985
107+
1827
108+
1842
109+
1925
110+
1735
111+
1635
112+
1821
113+
1820
114+
1973
115+
1531
116+
1770
117+
59
118+
1846
119+
1932
120+
1907
121+
1730
122+
933
123+
1395
124+
1753
125+
1751
126+
361
127+
1530
128+
1782
129+
1087
130+
1589
131+
1929
132+
1795
133+
1815
134+
1732
135+
1765
136+
1877
137+
1722
138+
526
139+
1709
140+
1789
141+
1892
142+
1913
143+
1662
144+
1809
145+
1670
146+
1947
147+
1835
148+
1587
149+
1758
150+
1982
151+
2009
152+
1757
153+
670
154+
1983
155+
1524
156+
1878
157+
1796
158+
1952
159+
566
160+
1922
161+
1882
162+
1870
163+
1799
164+
1731
165+
1724
166+
1805
167+
2003
168+
1596
169+
1566
170+
1853
171+
1911
172+
1857
173+
1739
174+
1744
175+
1627
176+
1729
177+
1745
178+
1845
179+
1582
180+
1884
181+
1883
182+
1941
183+
1764
184+
1685
185+
1791
186+
1837
187+
1697
188+
1742
189+
1781
190+
1948
191+
1876
192+
1989
193+
1643
194+
1871
195+
1906
196+
1726
197+
1958
198+
1502
199+
1927
200+
1946

2020/day1/part1.mjs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fs from "fs";
2+
3+
const input = fs
4+
.readFileSync("./input.txt", "utf8")
5+
.split("\n")
6+
.reduce((acc, cur) => {
7+
const found = acc.find((x) => x === 2020 - cur);
8+
if (found) {
9+
console.log(found, cur, found * cur);
10+
}
11+
acc.push(+cur);
12+
return acc;
13+
}, []);

0 commit comments

Comments
 (0)