My solutions to 2018's advent of code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

day02.nim 1.1KB

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