Commit 89f97663 authored by Romain's avatar Romain
Browse files

Import all so far

parents
# Advent of Code 2019
I uploaded all my work so far.
Soon, I will do a refactor of the architecture.
And re-upload everything cleaner.
/target
**/*.rs.bk
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day-01"
version = "0.1.0"
[package]
name = "day-01"
version = "0.1.0"
authors = ["Romain Giraud <me@romaingiraud.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
100725
63593
84738
143809
108595
94419
91617
91573
102728
143383
74613
80331
76530
139884
104607
107171
107640
87284
120827
85742
62474
97582
110668
73426
57656
70819
89848
138732
54386
116905
107954
131488
75056
97660
55295
146265
58026
94712
73636
138077
61480
148868
119364
145430
103901
134202
106759
50254
82440
117801
80263
97022
145229
57702
57460
58401
145652
127341
123585
65291
70219
147009
88728
72059
83815
99635
80913
149475
61798
110054
102505
148511
95160
50208
129867
57079
138435
75865
63185
142389
78370
108077
106438
86267
100785
101165
68501
146079
122420
121429
62608
115338
90667
131391
50260
85343
76411
94432
130126
80915
use std::io;
use std::io::prelude::*;
fn calculate_fuel(n: i32) -> i32 {
let m: i32 = (n / 3) - 2;
if m < 0 {
return 0;
}
m + calculate_fuel(m)
}
fn main() -> io::Result<()> {
let mut total = 0;
let stdin = std::io::stdin();
for line in stdin.lock().lines() {
let n = line.unwrap().as_str().parse::<i32>().unwrap();
//total += (n / 3) - 2;
total += calculate_fuel(n);
}
println!("{}", total);
Ok(())
}
/target
**/*.rs.bk
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day-02"
version = "0.1.0"
[package]
name = "day-02"
version = "0.1.0"
authors = ["Romain Giraud <me@romaingiraud.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
1,9,10,3,2,3,11,0,99,30,40,50
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,2,19,6,23,1,23,5,27,1,9,27,31,1,31,10,35,2,35,9,39,1,5,39,43,2,43,9,47,1,5,47,51,2,51,13,55,1,55,10,59,1,59,10,63,2,9,63,67,1,67,5,71,2,13,71,75,1,75,10,79,1,79,6,83,2,13,83,87,1,87,6,91,1,6,91,95,1,10,95,99,2,99,6,103,1,103,5,107,2,6,107,111,1,10,111,115,1,115,5,119,2,6,119,123,1,123,5,127,2,127,6,131,1,131,5,135,1,2,135,139,1,139,13,0,99,2,0,14,0
use std::io;
use std::io::prelude::*;
// second exercice
fn main() -> io::Result<()> {
// parse input
let stdin = std::io::stdin();
let mut buffer = String::new();
stdin.lock().read_to_string(&mut buffer).unwrap();
let tab_orig: Vec<i32> = buffer.split(',').map(|i| i.trim().parse::<i32>().unwrap()).collect();
let mut noun = 0;
let mut verb = 0;
'outer: while noun < 100 {
while verb < 100 {
let mut tab = tab_orig.clone();
tab[1] = noun;
tab[2] = verb;
// execute program
let mut index = 0;
loop {
match tab[index] {
1 => { // add
let iresult = tab[index + 3] as usize;
tab[iresult] = tab[tab[index + 1] as usize] + tab[tab[index + 2] as usize];
},
2 => { // mult
let iresult = tab[index + 3] as usize;
tab[iresult] = tab[tab[index + 1] as usize] * tab[tab[index + 2] as usize];
},
99 => {
println!("quit");
break;
},
_ => panic!("error"),
}
index += 4;
}
if tab[0] == 19690720 {
break 'outer;
}
verb += 1;
}
verb = 0;
noun += 1;
}
println!("noun: {}", noun);
println!("verb: {}", verb);
println!("result: {}", 100 * noun + verb);
Ok(())
}
// first exercice
/*
fn main() -> io::Result<()> {
// parse input
let stdin = std::io::stdin();
let mut buffer = String::new();
stdin.lock().read_to_string(&mut buffer).unwrap();
let mut tab: Vec<i32> = buffer.split(',').map(|i| i.trim().parse::<i32>().unwrap()).collect();
println!("{:?}", tab);
// replace position 1 with the value 12
tab[1] = 12;
// replace position 2 with the value 2
tab[2] = 2;
// execute program
let mut index = 0;
loop {
match tab[index] {
1 => { // add
let iresult = tab[index + 3] as usize;
tab[iresult] = tab[tab[index + 1] as usize] + tab[tab[index + 2] as usize];
},
2 => { // mult
let iresult = tab[index + 3] as usize;
tab[iresult] = tab[tab[index + 1] as usize] * tab[tab[index + 2] as usize];
},
99 => {
println!("quit");
break;
},
_ => panic!("error"),
}
index += 4;
}
println!("result: {}", tab[0]);
Ok(())
}
*/
/target
**/*.rs.bk
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day-03"
version = "0.1.0"
[package]
name = "day-03"
version = "0.1.0"
authors = ["Romain Giraud <me@romaingiraud.com>"]
edition = "2018"
default-run = "main"
[[bin]]
name = "main"
path = "src/main.rs"
[[bin]]
name = "main-nooptim"
path = "src/main-nooptim.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
R8,U5,L5,D3
U7,R6,D4,L4
answer: 6
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83
answer: 159
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7
answer: 135
R1008,U428,L339,U16,R910,U221,R53,D546,L805,U376,L19,U708,R493,D489,L443,D567,R390,D771,R270,U737,R926,U181,L306,D456,L668,D79,L922,U433,L701,U472,R914,U903,L120,U199,L273,D206,L967,U711,R976,U976,R603,U8,L882,U980,R561,D697,L224,D620,L483,U193,R317,D588,L932,D990,R658,U998,L136,U759,R463,U890,L297,U648,R163,U250,R852,U699,R236,D254,L173,U720,L259,U632,L635,U426,R235,D699,R411,U650,L258,D997,L781,D209,L697,D306,L793,U657,L936,U317,R549,D798,L951,D80,R591,D480,R835,U292,L722,U987,L775,U173,R353,U404,L250,U652,L527,D282,L365,D657,L141,D301,R128,D590,L666,U478,L85,D822,L716,U68,R253,D186,R81,U741,L316,D615,L570,U407,L734,D345,L652,U362,L360,D791,R358,U190,L823,U274,L279,D998,L16,D644,L201,D469,R213,D487,L251,D395,R130,U902,L398,U201,L56,D850,R541,D661,R921,U647,R309,D550,L307,D68,R894,U653,L510,D375,R20,U86,R357,D120,L978,D200,L45,D247,R906,U334,L242,D466,R418,U548,R698,D158,R582,U469,L968,U736,R196,U437,R87,D722,R811,U625,L425,D675,L904,D331,R693,D491,R559,U540,L120,D975,R180,U224,R610,U260,L769,D486,R93,D300,L230,U181,L60,U910,L60,D554,L527,U37,R69,D649,R768,D835,L581,U932,L746,U170,L733,U40,L497,D957,R12,U686,R85,D461,R796,D142,R664,U787,R636,D621,R824,D421,R902,D686,L202,D839,R567,D129,L77,D917,L200,D106,R3,D546,L101,D762,R780,U334,L410,D190,R431,D828,L816,D529,R648,D449,L845,U49,R750,U864,L133,D822,R46,U475,L229,U929,L676,D793,R379,U71,L243,U640,L122,U183,R528,U22,R375,D928,R292,U796,R259,U325,L921,U489,L246,D153,L384,D684,L243,U65,L342,U662,R707
L1008,D243,L602,D497,L395,U81,R589,U94,R640,D965,L397,D781,R464,U642,L130,D740,R938,D260,L106,D323,L626,U869,L495,U450,R640,D675,R602,D449,L542,U917,L244,U702,L644,D809,R902,U163,R118,U982,L867,D795,R546,U194,R397,D877,L354,D255,L477,U45,L707,D624,R806,U642,L926,D233,L800,U691,L990,D979,L431,U999,L423,D794,L238,U25,R986,U595,L167,U480,L555,U831,R496,U799,L897,D895,L993,D11,R486,U176,L90,U842,R499,U792,R787,U859,L100,U169,R170,D89,R297,D944,R362,D460,R147,U831,L45,U972,R582,D90,L934,U284,R555,U235,L138,U837,R965,U915,R928,U982,R157,D566,L953,U653,L629,U460,L335,D912,R355,D683,L710,D562,R392,D44,R707,D979,L749,D223,L776,D723,R735,D356,R49,U567,L563,D220,L868,U223,R448,D505,L411,U662,L188,D536,R55,U718,L108,D289,R435,U98,R775,U933,L127,D84,R253,D523,L2,D905,R266,U675,R758,D331,R122,U988,R215,D500,R89,U306,R833,U763,R570,D818,L985,U127,L87,D210,R355,D532,R870,U196,R695,U633,R170,D540,R506,U708,L663,U566,L633,U306,L452,U180,R463,D21,L220,D268,R608,U986,L493,D598,L957,D116,L454,D584,L405,U651,R352,U681,R807,U767,L988,U692,R474,U710,R607,U313,R711,U12,R371,D561,R72,U522,R270,U904,L49,U525,R562,U895,L232,D319,R902,D236,L601,D816,R836,U419,R610,U644,L733,U355,L836,U228,L895,D39,L44,D848,L965,U475,R56,U62,L458,U99,R236,D763,R912,U295,R515,U179,R20,D777,R511,D906,R903,U855,L507,D512,L63,D630,R442,U595,L701,U654,R238,D35,L31,D469,R6,D222,R132,D837,R921,U838,R986,D441,L950,D530,L397,U41,L81,D60,L245,D75,R620,D455,L937,D180,R215,D684,R724,U561,R479,D353,L501
use std::io;
use std::io::prelude::*;
use std::collections::HashSet;
use std::collections::LinkedList;
#[derive(Debug)]
struct Wire {
design: Vec<(char, i32)>,
index: i32,
}
impl Wire {
fn new(design: &str) -> Wire {
let mut v = Vec::new();
for m in design.split(',') {
let m = m.trim();
let c = m.chars().next().unwrap();
let nb = m[1..].parse::<i32>().unwrap();
v.push((c, nb));
}
println!("{:?}", v);
Wire {
design: v,
index: 0,
}
}
fn parse_direction(c: char) -> (i32, i32) {
match c {
'R' => ( 1, 0),
'L' => (-1, 0),
'U' => ( 0, 1),
'D' => ( 0, -1),
_ => panic!("unknown direction: {}", c),
}
}
}
impl Iterator for Wire {
// we will be counting with usize
type Item = (i32, i32);
// next() is the only required method
fn next(&mut self) -> Option<(i32, i32)> {
self.index += 1;
let mut total = 0;
let mut start = (0, 0);
for d in &mut self.design {
if self.index > (total + d.1) {
total += d.1;
let (x, y) = Self::parse_direction(d.0);
start.0 += x * d.1;
start.1 += y * d.1;
} else {
let (x, y) = Self::parse_direction(d.0);
let t = (start.0 + x * (self.index - total), start.1 + y * (self.index - total));
//println!("{:?}", t);
return Some(t);
}
}
None
}
}
// second exercice
/*
fn main() -> io::Result<()> {
// parse input
let stdin = std::io::stdin();
let mut path1 = String::new();
stdin.lock().read_line(&mut path1).unwrap();
let mut path2 = String::new();
stdin.lock().read_line(&mut path2).unwrap();
let tab1 = Wire::new(&path1).collect::<Vec::<(i32, i32)>>();
let tab2 = Wire::new(&path2).collect::<Vec::<(i32, i32)>>();
let mut length = 0;
'outer: for (i1, t1) in tab1.iter().enumerate() {
for (i2, t2) in tab2.iter().enumerate() {
if t1 == t2 {
length = i1 + i2 + 2;
break 'outer;
}
}
}
println!("{}", length);
Ok(())
}
*/
// first exercice
fn main() -> io::Result<()> {
// parse input
let stdin = std::io::stdin();
let mut path1 = String::new();
stdin.lock().read_line(&mut path1).unwrap();
let mut path2 = String::new();
stdin.lock().read_line(&mut path2).unwrap();
let tab = Wire::new(&path1).collect::<LinkedList::<(i32, i32)>>();
let tab2 = Wire::new(&path2).collect::<LinkedList::<(i32, i32)>>();
let mut length = -1;
for t in Wire::new(&path2) {
if tab.contains(&t) {
let sum = t.0.abs() + t.1.abs();
println!("res: {}, {} => {}", t.0, t.1, sum);
if length == -1 || sum < length {
length = sum;
}
}
}
println!("{}", length);
Ok(())
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment