Skip to content

Commit 25f4d9d

Browse files
committed
Add 2020 solutions
Signed-off-by: AKU <[email protected]>
1 parent 1ad039b commit 25f4d9d

File tree

240 files changed

+10455
-465
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

240 files changed

+10455
-465
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
# [Day 1: Report Repair](https://adventofcode.com/2020/day/1)
22

3+
<details><summary>Script output</summary>
4+
5+
```
6+
❯ python .\python\
7+
AoC 2020: day 1 - Report Repair
8+
9+
Test cases
10+
1.1 pass
11+
2.1 pass
12+
13+
Answers
14+
Part 1: 1010884
15+
Part 2: 253928438
16+
17+
❯ go run .\go\
18+
AoC 2020: day 1 - Report Repair
19+
Go go1.15.2
20+
21+
Test cases
22+
1.1 pass
23+
2.1 pass
24+
25+
Answers
26+
Part 1: 1010884
27+
Part 2: 253928438
28+
```
29+
30+
</details>
31+

challenges/2020/01-reportRepair/benchmark.txt

-23
This file was deleted.

challenges/2020/01-reportRepair/go/challenge.go

-55
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package challenge
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
func parse(instr string) []int {
9+
inputSlice := strings.Split(strings.TrimSpace(instr), "\n")
10+
11+
var values []int
12+
for _, v := range inputSlice {
13+
str, _ := strconv.Atoi(v)
14+
values = append(values, str)
15+
}
16+
17+
return values
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package challenge
2+
3+
func PartOne(instr string) int {
4+
values := parse(instr)
5+
6+
for _, i := range values {
7+
for _, v := range values {
8+
if v+i == 2020 {
9+
return v * i
10+
}
11+
}
12+
}
13+
14+
return 0
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package challenge
2+
3+
func PartTwo(instr string) int {
4+
values := parse(instr)
5+
6+
for _, i := range values {
7+
for _, v := range values {
8+
for _, x := range values {
9+
if v+i+x == 2020 {
10+
return v * i * x
11+
}
12+
}
13+
}
14+
}
15+
16+
return 0
17+
}
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"adventOfCode/01-reportRepair/go/challenge"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"runtime"
10+
"strconv"
11+
12+
"github.com/fatih/color"
13+
)
14+
15+
const (
16+
year = "2020"
17+
day = "1"
18+
title = "Report Repair"
19+
)
20+
21+
func main() {
22+
fmt.Fprintf(color.Output, "%s: day %s - %s\n", color.YellowString("AoC "+year), color.BlueString(day), title)
23+
fmt.Fprintf(color.Output, "Go %s\n\n", color.BlueString(runtime.Version()))
24+
25+
var challengeInput string
26+
{
27+
inb, err := ioutil.ReadFile("input.txt")
28+
if err != nil {
29+
fmt.Println("Error: could not open input.txt")
30+
os.Exit(-1)
31+
}
32+
challengeInput = string(inb)
33+
}
34+
35+
runTests()
36+
37+
fmt.Println("Answers")
38+
fmt.Fprintf(color.Output, "Part %s: %s\n", color.BlueString("1"), color.BlueString(strconv.Itoa(challenge.PartOne(challengeInput))))
39+
fmt.Fprintf(color.Output, "Part %s: %s\n", color.BlueString("2"), color.BlueString(strconv.Itoa(challenge.PartTwo(challengeInput))))
40+
41+
}
42+
43+
type tc struct {
44+
Input string `json:"input"`
45+
Expected int `json:"expected"`
46+
}
47+
48+
func runTests() {
49+
50+
testCases := struct {
51+
One []tc `json:"one"`
52+
Two []tc `json:"two"`
53+
}{}
54+
55+
{
56+
inb, err := ioutil.ReadFile("testCases.json")
57+
if err != nil {
58+
fmt.Println("Error: could not open testCases.json. Skipping tests")
59+
return
60+
}
61+
err = json.Unmarshal(inb, &testCases)
62+
if err != nil {
63+
fmt.Println("Error: could not parse testCases.json. Skipping tests")
64+
return
65+
}
66+
}
67+
68+
fmt.Println("Test cases")
69+
70+
rt := func(tcs []tc, f func(string) int, n string) {
71+
for i, tc := range tcs {
72+
fmt.Fprintf(color.Output, "%s ", color.BlueString(n+"."+strconv.Itoa(i+1)))
73+
result := f(tc.Input)
74+
if result == tc.Expected {
75+
fmt.Fprintf(color.Output, "%s", color.GreenString("pass"))
76+
} else {
77+
fmt.Fprintf(color.Output, "%s (got %s, expected %s)", color.RedString("fail"), color.BlueString(strconv.Itoa(result)), color.BlueString(strconv.Itoa(tc.Expected)))
78+
}
79+
fmt.Println()
80+
}
81+
}
82+
83+
rt(testCases.One, challenge.PartOne, "1")
84+
rt(testCases.Two, challenge.PartTwo, "2")
85+
86+
fmt.Println()
87+
88+
}

challenges/2020/01-reportRepair/info.json

-17
This file was deleted.

challenges/2020/01-reportRepair/py/__init__.py

-32
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import json
2+
import sys
3+
4+
from rich import print
5+
6+
from partOne import partOne
7+
from partTwo import partTwo
8+
9+
year = "2020"
10+
day = "1"
11+
title = "Report Repair"
12+
13+
14+
def run_tests():
15+
try:
16+
test_cases = open("testCases.json").read()
17+
except FileNotFoundError:
18+
print("Info: could not open testCases.json. Skipping tests")
19+
return
20+
21+
print("Test cases")
22+
23+
test_cases = json.loads(test_cases)
24+
25+
def rt(tcs, f, n):
26+
for i, tc in enumerate(tcs):
27+
print(f"{n}.{i+1} ", end="")
28+
expectedInt = tc["expected"]
29+
result = f(str(tc["input"]))
30+
if result == expectedInt:
31+
print("[green]pass[/green]")
32+
else:
33+
print(f"[red]fail[/red] (got {result}, expected {expectedInt})")
34+
35+
rt(test_cases["one"], partOne, 1)
36+
rt(test_cases["two"], partTwo, 2)
37+
38+
print()
39+
40+
41+
if __name__ == "__main__":
42+
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}\n")
43+
44+
try:
45+
challenge_input = open("input.txt").read()
46+
except FileNotFoundError:
47+
print("Error: could not open input.txt")
48+
sys.exit(-1)
49+
50+
run_tests()
51+
52+
print("Answers")
53+
print("Part 1:", partOne(challenge_input))
54+
print("Part 2:", partTwo(challenge_input))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from typing import List
2+
3+
4+
def parse(instr: str) -> List:
5+
return [int(x) for x in instr.strip().split("\n")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from common import *
2+
3+
4+
def partOne(instr: str) -> int:
5+
values = parse(instr)
6+
7+
for i in values:
8+
for v in values:
9+
if v + i == 2020:
10+
return v * i
11+
12+
return 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from common import *
2+
3+
4+
def partTwo(instr: str) -> int:
5+
values = parse(instr)
6+
7+
for i in values:
8+
for v in values:
9+
for x in values:
10+
if v + i + x == 2020:
11+
return v * i * x
12+
13+
return 0

0 commit comments

Comments
 (0)