1
0
Fork 0
mirror of https://github.com/Findus23/AdventOfCode2019.git synced 2024-08-27 19:52:12 +02:00

initial commit

This commit is contained in:
Lukas Winkler 2019-12-05 11:35:38 +01:00
commit e3ecc0429f
Signed by: lukas
GPG key ID: 54DE4D798D244853
13 changed files with 351 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.mypy_cache/
.idea/
__pycache__/

26
.travis.yml Normal file
View file

@ -0,0 +1,26 @@
jobs:
allow_failures:
- rust: nightly
include:
- language: python
python: 3.8
before_script:
- cd python
script:
- pytest
- language: rust
rust: stable
cache: cargo
before_script:
- cd rust
- language: rust
rust: beta
cache: cargo
before_script:
- cd rust
- language: rust
rust: nightly
cache: cargo
before_script:
- cd rust

41
python/1/day1.py Normal file
View file

@ -0,0 +1,41 @@
def calculate_fuel(mass: int) -> int:
return mass // 3 - 2
def part1() -> int:
total = 0
with open("1/input.txt") as f:
for line in f:
m = int(line)
fuel = calculate_fuel(m)
total += fuel
return total
def advanced_fuel(mass: int) -> int:
total = 0
while True:
fuel = calculate_fuel(mass)
if fuel > 0:
total += fuel
mass = fuel
else:
break
return total
def part2() -> int:
total = 0
with open("1/input.txt") as f:
for line in f:
m = int(line)
fuel = advanced_fuel(m)
total += fuel
return total
if __name__ == '__main__':
print(part1())
print(part2())

100
python/1/input.txt Normal file
View file

@ -0,0 +1,100 @@
103910
133712
82560
91679
98354
89007
93288
132363
91373
83666
55958
90387
100869
98127
120197
86931
60370
143999
71541
115662
51287
81624
58307
60408
141664
89781
127772
132353
101220
104001
140488
58072
75764
120003
82386
77603
130604
86672
120987
80334
67674
52918
98041
102541
97612
50436
129998
84854
101867
82039
108966
80708
54588
86854
89607
71869
126093
89460
86558
77651
53295
132188
137266
97370
114620
86691
147199
147299
72616
142654
88610
104030
64256
54867
76532
145081
102335
72987
72684
148155
59739
85954
141001
125171
107764
141622
89536
92435
69038
84518
119700
119801
81677
125317
72683
128905
93666
75633
117361
82295

21
python/1/test_day1.py Normal file
View file

@ -0,0 +1,21 @@
from day1 import calculate_fuel, part1, advanced_fuel, part2
def test_calculate_fuel():
assert calculate_fuel(12) == 2
assert calculate_fuel(14) == 2
assert calculate_fuel(1969) == 654
assert calculate_fuel(100756) == 33583
def test_part1():
assert part1() == 3226488
def test_advanced_fuel():
assert advanced_fuel(14) == 2
assert advanced_fuel(1969) == 966
assert advanced_fuel(100756) == 50346
def test_part2():
assert part2()==4836845

55
python/2/day2.py Normal file
View file

@ -0,0 +1,55 @@
from typing import List
intcode = List[int]
def opcode_parse(text: str) -> intcode:
return list(map(int, text.split(",")))
def run_intcode(codelist: intcode) -> intcode:
p = 0 # pointer
while True:
code = codelist[p]
if code == 99:
break
from1 = codelist[p + 1]
from2 = codelist[p + 2]
to = codelist[p + 3]
if code == 1:
codelist[to] = codelist[from1] + codelist[from2]
elif code == 2:
codelist[to] = codelist[from1] * codelist[from2]
else:
raise ValueError(f"invalid intcode: {code}")
p += 4
return codelist
def part1() -> int:
with open("2/input.txt") as f:
cl = opcode_parse(f.read())
cl[1] = 12
cl[2] = 2
cl = run_intcode(cl)
return cl[0]
def part2() -> int:
with open("2/input.txt") as f:
initial_cl = opcode_parse(f.read())
for noun in range(99):
for verb in range(99):
cl = initial_cl[:] # make a copy
cl[1] = noun
cl[2] = verb
cl = run_intcode(cl)
if cl[0] == 19690720:
return 100 * noun + verb
if __name__ == '__main__':
print(part1())
print(part2())

1
python/2/input.txt Normal file
View file

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,5,19,23,2,10,23,27,1,27,5,31,2,9,31,35,1,35,5,39,2,6,39,43,1,43,5,47,2,47,10,51,2,51,6,55,1,5,55,59,2,10,59,63,1,63,6,67,2,67,6,71,1,71,5,75,1,13,75,79,1,6,79,83,2,83,13,87,1,87,6,91,1,10,91,95,1,95,9,99,2,99,13,103,1,103,6,107,2,107,6,111,1,111,2,115,1,115,13,0,99,2,0,14,0

20
python/2/test_day2.py Normal file
View file

@ -0,0 +1,20 @@
from day2 import opcode_parse, run_intcode, part1, part2
def test_opcode_parse():
assert opcode_parse("2,2,3,45,5") == [2, 2, 3, 45, 5]
def test_run_intcode():
assert run_intcode([1, 0, 0, 0, 99]) == [2, 0, 0, 0, 99]
assert run_intcode([2, 3, 0, 3, 99]) == [2, 3, 0, 6, 99]
assert run_intcode([2, 4, 4, 5, 99, 0]) == [2, 4, 4, 5, 99, 9801]
assert run_intcode([1, 1, 1, 4, 99, 5, 6, 0, 99]) == [30, 1, 1, 4, 2, 5, 6, 0, 99]
def test_part1():
assert part1() == 4714701
def test_part2():
assert part2() == 5121

2
rust/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target/
**/*.rs.bk

6
rust/Cargo.lock generated Normal file
View file

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "adventofcode"
version = "0.1.0"

9
rust/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "adventofcode"
version = "0.1.0"
authors = ["Lukas Winkler <git@lw1.at>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

58
rust/src/day1.rs Normal file
View file

@ -0,0 +1,58 @@
use std::fs;
fn calculate_fuel(mass: i32) -> i32 {
return mass / 3 - 2;
}
fn advanced_fuel(mass: i32) -> i32 {
let mut mass = mass;
let mut total = 0;
let mut fuel;
loop {
fuel = calculate_fuel(mass);
if fuel > 0 {
total += fuel;
mass = fuel;
} else {
break;
}
}
return total;
}
pub fn part1() -> i32 {
let data = fs::read_to_string("../python/1/input.txt").expect("Unable to read file");
let ints = data
.lines()
.map(|line| calculate_fuel(line.parse().expect("error when parsing line as integer")))
.sum();
return ints;
}
pub fn part2() -> i32 {
return 1;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_calculate_fuel() {
assert_eq!(calculate_fuel(12), 2);
assert_eq!(calculate_fuel(14), 2);
assert_eq!(calculate_fuel(1969), 654);
assert_eq!(calculate_fuel(100756), 33583);
}
#[test]
fn test_advanced_fuel() {
assert_eq!(advanced_fuel(14), 2);
assert_eq!(advanced_fuel(1969), 966);
assert_eq!(advanced_fuel(100756), 50346);
}
#[test]
fn test_part1() {
assert_eq!(part1(), 3226488)
}
}

9
rust/src/main.rs Normal file
View file

@ -0,0 +1,9 @@
mod day1;
fn main() {
let res1 = day1::part1();
let res2 = day1::part2();
println!("part 1: {}", res1);
println!("part 1: {}", res2);
}