Sorceress

2018-04

Dec 4th, 2018
740
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DEFLNG A-Z
  2. DIM raw(1200) AS STRING
  3. DIM t AS INTEGER
  4. DIM m(32, 60) AS INTEGER
  5. DIM gid(32) AS INTEGER
  6. DIM w$(fake)
  7.  
  8. OPEN "c:\aoc\2018\input04.txt" FOR INPUT AS #1
  9. WHILE NOT EOF(1)
  10.   INPUT #1, r$: t = t + 1: raw(t) = r$
  11. WEND
  12. CLOSE #1
  13.  
  14. FOR i = 1 TO t - 1
  15.   FOR j = 1 TO t - i
  16.     IF raw(j) > raw(j + 1) THEN r$ = raw(j): raw(j) = raw(j + 1): raw(j + 1) = r$
  17.   NEXT
  18. NEXT
  19.  
  20. FOR i = 1 TO t
  21.   a$ = raw(i): GOSUB strsplit
  22.   SELECT CASE w$(6)
  23.     CASE "Guard": id = VAL(w$(7)): GOSUB hGuard
  24.     CASE "falls": t0 = VAL(w$(5))
  25.     CASE "wakes": t1 = VAL(w$(5)): FOR j = t0 TO t1 - 1: m(h, j) = m(h, j) + 1: NEXT
  26.   END SELECT
  27. NEXT
  28.  
  29. 'pt1
  30. FOR h = 1 TO gc
  31.   sum = 0: FOR j = 0 TO 59: sum = sum + m(h, j): NEXT
  32.   IF sum > bigsum THEN bigsum = sum: bigh = h
  33. NEXT
  34. FOR j = 0 TO 59
  35.   mm = m(bigh, j): IF mm > bigmm THEN bigmm = mm: bigj = j
  36. NEXT
  37. PRINT "part 1:"; gid(bigh) * bigj; " ( guard"; gid(bigh); "x minute"; bigj; ")"
  38.  
  39. 'pt2
  40. bigh = 0: bigsum = 0
  41. FOR h = 1 TO gc
  42.   FOR j = 0 TO 59
  43.     IF m(h, j) > bigsum THEN bigsum = m(h, j): bigh = h: bigj = j
  44.   NEXT
  45. NEXT
  46. PRINT "part 2:"; gid(bigh) * bigj; " ( guard"; gid(bigh); "x minute"; bigj; ")"
  47.  
  48. END
  49.  
  50.  
  51. '-------------------------------------------
  52.  
  53. hGuard:
  54. h = 0
  55. FOR j = 1 TO gc
  56.   IF gid(j) = id THEN h = j
  57. NEXT
  58. IF h = 0 THEN gc = gc + 1: gid(gc) = id: h = gc
  59. RETURN
  60.  
  61. strsplit:
  62. REDIM w$(16)
  63. DL$ = "[]-:# ": ii = 1: ok = 0
  64. FOR jj = 1 TO LEN(a$)
  65.   b$ = MID$(a$, jj, 1)
  66.   splitme = 0
  67.   FOR kk = 1 TO LEN(DL$)
  68.     IF b$ = MID$(DL$, kk, 1) THEN splitme = 1
  69.   NEXT
  70.   IF splitme = 1 THEN
  71.     IF ok = 1 THEN ii = ii + 1: ok = 0
  72.   ELSE
  73.     IF b$ > " " THEN w$(ii) = w$(ii) + b$: ok = 1
  74.   END IF
  75. NEXT
  76. 'FOR i = 1 TO ii: PRINT i; ")  "; w$(i): NEXT: SLEEP 'test
  77. RETURN
RAW Paste Data