Advertisement
Guest User

puzzle15 c#

a guest
Dec 15th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace PuzzleNS {
  7. class Puzzle {
  8. static int solve(int target, List<int> nums)
  9. {
  10. Dictionary<int, int> tab = new Dictionary<int, int>();
  11. for (int i = 0; i < nums.Count()-1; i++)
  12. tab.Add(nums[i], i+1);
  13. int last_num = nums[nums.Count()-1];
  14. for (int t = nums.Count()+1; t <= target; t++) {
  15. if (!tab.ContainsKey(last_num)) { /* not found */
  16. tab[last_num] = t-1;
  17. last_num = 0;
  18. } else {
  19. int newvalue = t-1 - tab[last_num];
  20. tab[last_num] = t-1;
  21. last_num = newvalue;
  22. }
  23. }
  24. return last_num;
  25. }
  26.  
  27. static void Main(string[] args)
  28. {
  29. List<int> nums = File.ReadAllLines("input15.txt")[0].Split(',').ToList().Select(x => int.Parse(x)).ToList();
  30. Console.WriteLine("part 1: {0}", solve(2020, nums));
  31. Console.WriteLine("part 2: {0}", solve(30000000, nums));
  32. }
  33. }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement