parent
							
								
									1c77865ad3
								
							
						
					
					
						commit
						4191470b86
					
				@ -0,0 +1,63 @@
 | 
				
			||||
#!/usr/bin/env julia
 | 
				
			||||
 | 
				
			||||
using Test
 | 
				
			||||
 | 
				
			||||
struct Instruction
 | 
				
			||||
  cycles :: UInt
 | 
				
			||||
  add    :: Int
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
function parse_instruction_line(line)
 | 
				
			||||
  parts = split(line, limit=2)
 | 
				
			||||
  if parts[1] == "noop"
 | 
				
			||||
    return Instruction(1, 0)
 | 
				
			||||
  elseif parts[1] == "addx"
 | 
				
			||||
    return Instruction(2, parse(Int, parts[2]))
 | 
				
			||||
  else
 | 
				
			||||
    throw(DomainError("Unknown instruction: " * line))
 | 
				
			||||
  end
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
function execute_program(io; end_cycle=220)
 | 
				
			||||
  X :: Int = 1
 | 
				
			||||
  cycle :: Int = 1
 | 
				
			||||
  signal_strength_sum :: Int = 0
 | 
				
			||||
  next_test_cycle = 20
 | 
				
			||||
  for line in eachline(io)
 | 
				
			||||
    if mod(cycle - 20, 40) == 0 && cycle <= end_cycle
 | 
				
			||||
      println(cycle, " ", X)
 | 
				
			||||
      signal_strength_sum += cycle * X
 | 
				
			||||
      next_test_cycle += 40
 | 
				
			||||
    end
 | 
				
			||||
    inst = parse_instruction_line(line)
 | 
				
			||||
    new_cycle = cycle + inst.cycles
 | 
				
			||||
    if new_cycle > next_test_cycle
 | 
				
			||||
      signal_strength_sum += next_test_cycle * X
 | 
				
			||||
      next_test_cycle += 40
 | 
				
			||||
    end
 | 
				
			||||
    cycle = new_cycle
 | 
				
			||||
    X += inst.add
 | 
				
			||||
  end
 | 
				
			||||
  println("last inst cycle ", cycle)
 | 
				
			||||
  signal_strength_sum += X * sum(filter(x -> x >= cycle, 20:40:end_cycle))
 | 
				
			||||
  return signal_strength_sum
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
function test()
 | 
				
			||||
  @testset "signal strength sum" verbose=true begin
 | 
				
			||||
    @test execute_program("example.txt") == 13140
 | 
				
			||||
    @test execute_program("input.txt") == 13220
 | 
				
			||||
  end
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
function main()
 | 
				
			||||
  if size(ARGS, 1) == 0
 | 
				
			||||
    test()
 | 
				
			||||
  else
 | 
				
			||||
    infile = ARGS[1]
 | 
				
			||||
    println("infile = ", infile)
 | 
				
			||||
    println("signal strength sum = ", execute_program(infile))
 | 
				
			||||
  end
 | 
				
			||||
end
 | 
				
			||||
 | 
				
			||||
main()
 | 
				
			||||
@ -0,0 +1,146 @@
 | 
				
			||||
addx 15
 | 
				
			||||
addx -11
 | 
				
			||||
addx 6
 | 
				
			||||
addx -3
 | 
				
			||||
addx 5
 | 
				
			||||
addx -1
 | 
				
			||||
addx -8
 | 
				
			||||
addx 13
 | 
				
			||||
addx 4
 | 
				
			||||
noop
 | 
				
			||||
addx -1
 | 
				
			||||
addx 5
 | 
				
			||||
addx -1
 | 
				
			||||
addx 5
 | 
				
			||||
addx -1
 | 
				
			||||
addx 5
 | 
				
			||||
addx -1
 | 
				
			||||
addx 5
 | 
				
			||||
addx -1
 | 
				
			||||
addx -35
 | 
				
			||||
addx 1
 | 
				
			||||
addx 24
 | 
				
			||||
addx -19
 | 
				
			||||
addx 1
 | 
				
			||||
addx 16
 | 
				
			||||
addx -11
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 21
 | 
				
			||||
addx -15
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx -3
 | 
				
			||||
addx 9
 | 
				
			||||
addx 1
 | 
				
			||||
addx -3
 | 
				
			||||
addx 8
 | 
				
			||||
addx 1
 | 
				
			||||
addx 5
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx -36
 | 
				
			||||
noop
 | 
				
			||||
addx 1
 | 
				
			||||
addx 7
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
addx 6
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 7
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
addx -13
 | 
				
			||||
addx 13
 | 
				
			||||
addx 7
 | 
				
			||||
noop
 | 
				
			||||
addx 1
 | 
				
			||||
addx -33
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 8
 | 
				
			||||
noop
 | 
				
			||||
addx -1
 | 
				
			||||
addx 2
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
addx 17
 | 
				
			||||
addx -9
 | 
				
			||||
addx 1
 | 
				
			||||
addx 1
 | 
				
			||||
addx -3
 | 
				
			||||
addx 11
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx -13
 | 
				
			||||
addx -19
 | 
				
			||||
addx 1
 | 
				
			||||
addx 3
 | 
				
			||||
addx 26
 | 
				
			||||
addx -30
 | 
				
			||||
addx 12
 | 
				
			||||
addx -1
 | 
				
			||||
addx 3
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx -9
 | 
				
			||||
addx 18
 | 
				
			||||
addx 1
 | 
				
			||||
addx 2
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 9
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx -1
 | 
				
			||||
addx 2
 | 
				
			||||
addx -37
 | 
				
			||||
addx 1
 | 
				
			||||
addx 3
 | 
				
			||||
noop
 | 
				
			||||
addx 15
 | 
				
			||||
addx -21
 | 
				
			||||
addx 22
 | 
				
			||||
addx -6
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
addx -10
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 20
 | 
				
			||||
addx 1
 | 
				
			||||
addx 2
 | 
				
			||||
addx 2
 | 
				
			||||
addx -6
 | 
				
			||||
addx -11
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
@ -0,0 +1,137 @@
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 5
 | 
				
			||||
addx 3
 | 
				
			||||
addx -2
 | 
				
			||||
noop
 | 
				
			||||
addx 5
 | 
				
			||||
addx 4
 | 
				
			||||
noop
 | 
				
			||||
addx 3
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
addx -17
 | 
				
			||||
addx 18
 | 
				
			||||
addx 3
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
addx 5
 | 
				
			||||
noop
 | 
				
			||||
addx 1
 | 
				
			||||
addx 2
 | 
				
			||||
addx 5
 | 
				
			||||
addx -40
 | 
				
			||||
noop
 | 
				
			||||
addx 5
 | 
				
			||||
addx 2
 | 
				
			||||
addx 3
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
addx 3
 | 
				
			||||
addx -2
 | 
				
			||||
addx 2
 | 
				
			||||
addx 2
 | 
				
			||||
noop
 | 
				
			||||
addx 3
 | 
				
			||||
addx 5
 | 
				
			||||
addx 2
 | 
				
			||||
addx 3
 | 
				
			||||
addx -2
 | 
				
			||||
addx 2
 | 
				
			||||
addx -24
 | 
				
			||||
addx 31
 | 
				
			||||
addx 2
 | 
				
			||||
addx -33
 | 
				
			||||
addx -6
 | 
				
			||||
addx 5
 | 
				
			||||
addx 2
 | 
				
			||||
addx 3
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
addx 3
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
addx -1
 | 
				
			||||
addx 6
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 1
 | 
				
			||||
addx 4
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx -15
 | 
				
			||||
addx 20
 | 
				
			||||
noop
 | 
				
			||||
addx -23
 | 
				
			||||
addx 27
 | 
				
			||||
noop
 | 
				
			||||
addx -35
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 5
 | 
				
			||||
addx 11
 | 
				
			||||
addx -10
 | 
				
			||||
addx 4
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
addx 2
 | 
				
			||||
noop
 | 
				
			||||
addx 3
 | 
				
			||||
noop
 | 
				
			||||
addx 3
 | 
				
			||||
addx 2
 | 
				
			||||
noop
 | 
				
			||||
addx 3
 | 
				
			||||
addx 2
 | 
				
			||||
addx 11
 | 
				
			||||
addx -4
 | 
				
			||||
addx 2
 | 
				
			||||
addx -38
 | 
				
			||||
addx -1
 | 
				
			||||
addx 2
 | 
				
			||||
noop
 | 
				
			||||
addx 3
 | 
				
			||||
addx 5
 | 
				
			||||
addx 2
 | 
				
			||||
addx -7
 | 
				
			||||
addx 8
 | 
				
			||||
addx 2
 | 
				
			||||
addx 2
 | 
				
			||||
noop
 | 
				
			||||
addx 3
 | 
				
			||||
addx 5
 | 
				
			||||
addx 2
 | 
				
			||||
addx -25
 | 
				
			||||
addx 26
 | 
				
			||||
addx 2
 | 
				
			||||
addx 8
 | 
				
			||||
addx -1
 | 
				
			||||
addx 2
 | 
				
			||||
addx -2
 | 
				
			||||
addx -37
 | 
				
			||||
addx 5
 | 
				
			||||
addx 3
 | 
				
			||||
addx -1
 | 
				
			||||
addx 5
 | 
				
			||||
noop
 | 
				
			||||
addx 22
 | 
				
			||||
addx -21
 | 
				
			||||
addx 2
 | 
				
			||||
addx 5
 | 
				
			||||
addx 2
 | 
				
			||||
addx 13
 | 
				
			||||
addx -12
 | 
				
			||||
addx 4
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 5
 | 
				
			||||
addx 1
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
addx 2
 | 
				
			||||
noop
 | 
				
			||||
addx 3
 | 
				
			||||
noop
 | 
				
			||||
noop
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue