day 2 tcl solution
This commit is contained in:
		
							parent
							
								
									f1e2dc0d24
								
							
						
					
					
						commit
						0fb7b77afe
					
				
							
								
								
									
										23
									
								
								day02/sol.tcl
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										23
									
								
								day02/sol.tcl
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
#!/usr/bin/env tclsh
 | 
			
		||||
source ../prelude.tcl
 | 
			
		||||
set input stdin
 | 
			
		||||
set total 0
 | 
			
		||||
set power 0
 | 
			
		||||
while {[gets $input line] >= 0} {
 | 
			
		||||
    regexp {Game (\d+): (.*)} $line _ num takes
 | 
			
		||||
    set min(red) 0
 | 
			
		||||
    set min(blue) 0
 | 
			
		||||
    set min(green) 0
 | 
			
		||||
    foreach t [split $takes ";"] {
 | 
			
		||||
        foreach {n color} [string map {, {}} $t] {
 | 
			
		||||
            set min($color) [max $min($color) $n]
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if {$min(red) <= 12 && $min(blue) <= 14 && $min(green) <= 13} {
 | 
			
		||||
        incr total $num
 | 
			
		||||
    }
 | 
			
		||||
    incr power [expr {$min(red) * $min(blue) * $min(green)}]
 | 
			
		||||
    #puts "${min(red)} ${min(blue)} ${min(green)}"
 | 
			
		||||
}
 | 
			
		||||
puts $total
 | 
			
		||||
puts $power
 | 
			
		||||
							
								
								
									
										16
									
								
								prelude.tcl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								prelude.tcl
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
package require Tcl 8.6
 | 
			
		||||
namespace import tcl::mathfunc::min
 | 
			
		||||
namespace import tcl::mathfunc::max
 | 
			
		||||
 | 
			
		||||
proc ladd {list} {
 | 
			
		||||
    set t 0
 | 
			
		||||
    foreach x $list { incr t $x }
 | 
			
		||||
    return $t
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
proc lmul {list} {
 | 
			
		||||
    set p 1
 | 
			
		||||
    foreach x $list { set p [eval {$p * $x}] }
 | 
			
		||||
    return $p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user