mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2025-09-03 13:50:40 +00:00
move to 2020 fodler
This commit is contained in:
12
2020/solutions/1/README.md
Normal file
12
2020/solutions/1/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# 1
|
||||
|
||||
This is quite simple, just iterate and find.
|
||||
|
||||
|
||||
<details>
|
||||
<summary>Solutions</summary>
|
||||
<ol>
|
||||
<li>1224 and 796 -> 974304</li>
|
||||
<li>332, 858 and 830 -> 236430480</li>
|
||||
</ol>
|
||||
</details>
|
55
2020/solutions/1/go/main.go
Normal file
55
2020/solutions/1/go/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const target uint64 = 2020
|
||||
|
||||
func findTwo(list []uint64) {
|
||||
for _, a := range list {
|
||||
for _, b := range list {
|
||||
if a+b == target {
|
||||
fmt.Printf("The numbers: %v and %v.\tSolution: %v\n", a, b, a*b)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func findThree(list []uint64) {
|
||||
for _, a := range list {
|
||||
for _, b := range list {
|
||||
for _, c := range list {
|
||||
if a+b+c == target {
|
||||
fmt.Printf("The numbers: %v, %v and %v.\tSolution: %v\n", a, b, c, a*b*c)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
data, err := ioutil.ReadFile("./solutions/1/data.txt")
|
||||
if err != nil {
|
||||
fmt.Println("File reading error", err)
|
||||
return
|
||||
}
|
||||
|
||||
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
|
||||
intLines := []uint64{}
|
||||
for _, i := range lines {
|
||||
num, _ := strconv.ParseUint(i, 10, 64)
|
||||
intLines = append(intLines, num)
|
||||
}
|
||||
|
||||
// fmt.Println("Result: ", findTwo(intLines))
|
||||
// fmt.Println("Result: ", findThree(intLines))
|
||||
findTwo(intLines)
|
||||
findThree(intLines)
|
||||
}
|
17
2020/solutions/1/python/main.py
Normal file
17
2020/solutions/1/python/main.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from typing import List
|
||||
from itertools import product
|
||||
from os.path import join, dirname
|
||||
|
||||
target = 2020
|
||||
data = join(dirname(__file__), '../data.txt')
|
||||
with open(data) as f:
|
||||
numbers: List[int] = list(map(int, f.readlines()))
|
||||
for a, b in product(numbers, numbers):
|
||||
if a + b == target:
|
||||
print(f'The numbers: {a} and {b}.\tSolution: {a*b}')
|
||||
break
|
||||
|
||||
for a, b, c in product(numbers, numbers, numbers):
|
||||
if a + b + c == target:
|
||||
print(f'The numbers: {a}, {b} and {c}.\tSolution: {a*b*c}')
|
||||
break
|
Reference in New Issue
Block a user