My solutions to 2018's advent of code
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import sequtils, strutils
  2. func checksum(ids: seq[string]): int =
  3. var pairCount, tripCount: int
  4. for id in ids:
  5. var pairs, trips: bool
  6. for c in id:
  7. var count = id.count(c)
  8. if count == 2 and not pairs:
  9. pairs = true
  10. pairCount.inc
  11. if count == 3 and not trips:
  12. trips = true
  13. tripCount.inc
  14. if pairs and trips:
  15. break
  16. pairCount * tripCount
  17. func difference(pairs: seq[tuple[a: char, b: char]]): string =
  18. var difference: bool
  19. for pair in pairs:
  20. if pair.a != pair.b:
  21. if difference:
  22. return ""
  23. else:
  24. difference = true
  25. else:
  26. result &= pair.a
  27. func max_common(ids: seq[string]): string =
  28. for i, id1 in ids:
  29. for id2 in ids[i+1 .. ids.high]:
  30. let difference = zip(id1, id2).difference
  31. if difference != "":
  32. return difference
  33. let ids = readFile("data/02.txt").strip.splitlines
  34. echo ids.checksum
  35. echo ids.max_common