Day 4 - Part 2
This commit is contained in:
		
							parent
							
								
									cb83afa64b
								
							
						
					
					
						commit
						f889ccffc0
					
				
							
								
								
									
										29
									
								
								day4.p8
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								day4.p8
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
pico-8 cartridge // http://www.pico-8.com
 | 
			
		||||
version 41
 | 
			
		||||
__lua__
 | 
			
		||||
 | 
			
		||||
local input = split([[Card 1: 47 10 77 75 94 50 93 43 27 18|73 75 32 65 98 76 71 13 50 78 54 94 18 83 77 6 79 93 45 27 87 57 51 55 43
 | 
			
		||||
Card 2: 17 9 7 91 32 97 76 39 83 88|88 25 46 50 91 18 39 76 17 22 28 82 44 66 52 7 11 56 77 9 40 83 97 32 47
 | 
			
		||||
Card 3: 32 2 10 96 30 37 60 86 88 50|64 96 58 41 24 37 86 84 80 49 30 53 83 50 42 33 2 52 88 46 19 89 32 51 5
 | 
			
		||||
| 
						 | 
				
			
			@ -200,6 +201,10 @@ Card 196: 54 71 23 38 79 55 78 67 44 10|71 17 85 93 55 72 68 14 8 11 75 86 52 41
 | 
			
		|||
Card 197: 7 65 66 60 52 23 15 27 32 3|59 4 79 97 91 41 83 36 22 89 19 38 96 81 8 29 49 86 46 12 25 6 24 16 78
 | 
			
		||||
Card 198: 89 99 29 80 68 39 38 10 2 63|88 8 92 81 23 54 1 12 45 96 67 86 37 98 47 34 71 4 58 3 27 41 75 93 66]], "\n")
 | 
			
		||||
 | 
			
		||||
printh("")
 | 
			
		||||
printh("Advent of Code 2013 - Day 4")
 | 
			
		||||
printh("")
 | 
			
		||||
 | 
			
		||||
function contains(haystack, needle)
 | 
			
		||||
  for i, v in ipairs(haystack) do
 | 
			
		||||
    if v == needle then
 | 
			
		||||
| 
						 | 
				
			
			@ -223,24 +228,42 @@ function strdsum(a, b)
 | 
			
		|||
end
 | 
			
		||||
 | 
			
		||||
local sum = "0"
 | 
			
		||||
local copies = {}
 | 
			
		||||
for i = 1, #input do
 | 
			
		||||
  copies[i] = "1"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
for i, line in ipairs(input) do
 | 
			
		||||
  printh("Card " .. i .. " - " .. copies[i] .. " copies")
 | 
			
		||||
  local numbers = split(line, ":")[2]
 | 
			
		||||
  local winning = split(split(numbers, "|")[1], " ")
 | 
			
		||||
  local mine = split(split(numbers, "|")[2], " ")
 | 
			
		||||
 | 
			
		||||
  local count = 0
 | 
			
		||||
  for i, v in ipairs(mine) do
 | 
			
		||||
  for j, v in ipairs(mine) do
 | 
			
		||||
    if contains(winning, v) then
 | 
			
		||||
      count = count + 1
 | 
			
		||||
      copies[i + count] = strdsum(copies[i + count], tostr(copies[i]))
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
  printh("  " .. count .. " winning cards")
 | 
			
		||||
  if count > 0 then
 | 
			
		||||
    count = count - 1
 | 
			
		||||
    printh(2^count)
 | 
			
		||||
    -- printh(2 ^ count)
 | 
			
		||||
    sum = strdsum(sum, tostr(2 ^ count))
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
printh(sum)
 | 
			
		||||
printh("Part 1 " .. sum)
 | 
			
		||||
 | 
			
		||||
-- Part 2
 | 
			
		||||
 | 
			
		||||
local part2 = "0"
 | 
			
		||||
for i = 1, #copies do
 | 
			
		||||
  -- printh("Card " .. i .. " - " .. copies[i] .. " copies")
 | 
			
		||||
  part2 = strdsum(part2, tostr(copies[i]))
 | 
			
		||||
end
 | 
			
		||||
printh("Part 2 " .. part2)
 | 
			
		||||
 | 
			
		||||
__gfx__
 | 
			
		||||
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user