Advent of Code 2019 Day 1

  從同事那邊得知了這個到聖誕節一天會公開一道題目的活動,雖然到現在我也只寫到第六題,也不曉得能不能慢慢補進度寫完,但總之既然寫了就得留下點什麼,所以記錄在這裡。
  聽說網路上是建議可以用這些題目來練習新語言,但因為我可以花在寫題目的時間的時間實在太零碎了,最後還是選擇用最熟悉的C#來寫。
  題目:Advent of Code 2019 Day 1
  使用語言:C#

  

  第一天的Part1題目相當單純,就只是把一串數字陣列丟進公式計算並取總和。
  Part2則是一個數字重複公式遞迴計算,雖然直覺用了遞迴,但果然平常沒在用就是不熟練,稍微小卡了一下。
  最後的答案附在底下,因為題目很單純所以就沒有特別把Part1和Part2分開紀錄。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{
// Puzzle Input
List<int> input = new List<int>()
{
125050,115884,132344,67441,119823,86204,111093,99489,67860,51288,62815,65263,
56540,81380,96101,116351,56330,123123,133969,115050,137851,136900,71254,53458,
139976,140218,117085,52241,71251,136110,103784,132893,140216,85568,94327,85200,
136753,110917,147197,120161,81684,56987,143452,94728,138355,54577,59898,69123,133769,
118418,93530,50297,71543,113383,135203,140129,70977,58566,129593,137456,130100,130915,
88872,96014,62746,127048,89522,62021,85363,143611,135995,65836,146022,119911,127381,121007,
71577,129637,90271,54640,117213,116151,114022,107683,102079,94388,135676,69019,104056,124799,
107998,148696,122793,135417,52981,122890,142491,88137,57609,54921
};

var ans = input.Select(x=>CalculateFuel(x)).Sum();

Console.WriteLine(ans);
}

private static int CalculateFuel(int fuel)
{
int result = (fuel / 3) - 2;

if(result < 1)
return 0;

result += CalculateFuel(result);

return result;
}
}
Advent-of-Code-2019-Day-2 .NET Core 學習筆記(1)

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×